Skip to content

Commit

Permalink
Corrected handlers and added filters
Browse files Browse the repository at this point in the history
  • Loading branch information
AnjeZenda committed Dec 3, 2024
1 parent 2e2bb4c commit 7affdc8
Show file tree
Hide file tree
Showing 9 changed files with 821 additions and 447 deletions.
23 changes: 19 additions & 4 deletions server/api/plants/v1/plants.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import "google/protobuf/timestamp.proto";
service PlantsAPI {
rpc GetPlantsWithCareRulesV1 (GetPlantsWithCareRulesV1Request) returns (GetPlantsWithCareRulesV1Response) {
option (google.api.http) = {
get: "/api/care"
post: "/api/care/{page}/{size}"
body: "*"
};
};

Expand Down Expand Up @@ -108,11 +109,11 @@ message CreatePlantV1Request {
string lightCondition = 5;
string wateringFrequency = 6;
string temperatureRegime = 7;
string care_complexity = 8;
string careComplexity = 8;
string description = 9;
string type = 10;
string species = 11;
string care_rules = 12;
string careRules = 12;
google.protobuf.Timestamp createdAt = 13;
string userId = 14;
}
Expand Down Expand Up @@ -155,11 +156,25 @@ message CreateNewCareRuleV1Request {
string species = 1;
string userId = 2;
string descriptionAddition = 3;
string type = 4;
string lightCondition = 5;
string temperatureRegime = 6;
string image = 7;
}

message CreateNewCareRuleV1Response {}

message GetPlantsWithCareRulesV1Request {}
message GetPlantsWithCareRulesV1Request {
message Filter {
string species = 1;
string type = 2;
repeated string lightCondition = 3;
repeated string temperatureRegime = 4;
}
Filter filter = 1;
int64 page = 2;
int64 size = 3;
}

message GetPlantsWithCareRulesV1Response {
message PlantInfo {
Expand Down
199 changes: 133 additions & 66 deletions server/gen/docs/plants/v1/plants.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"application/json"
],
"paths": {
"/api/care": {
"get": {
"operationId": "PlantsAPI_GetPlantsWithCareRulesV1",
"/api/care/new": {
"post": {
"operationId": "PlantsAPI_CreateNewCareRuleV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1GetPlantsWithCareRulesV1Response"
"$ref": "#/definitions/v1CreateNewCareRuleV1Response"
}
},
"default": {
Expand All @@ -33,19 +33,29 @@
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1CreateNewCareRuleV1Request"
}
}
],
"tags": [
"PlantsAPI"
]
}
},
"/api/care/new": {
"/api/care/{page}/{size}": {
"post": {
"operationId": "PlantsAPI_CreateNewCareRuleV1",
"operationId": "PlantsAPI_GetPlantsWithCareRulesV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1CreateNewCareRuleV1Response"
"$ref": "#/definitions/v1GetPlantsWithCareRulesV1Response"
}
},
"default": {
Expand All @@ -56,12 +66,26 @@
}
},
"parameters": [
{
"name": "page",
"in": "path",
"required": true,
"type": "string",
"format": "int64"
},
{
"name": "size",
"in": "path",
"required": true,
"type": "string",
"format": "int64"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1CreateNewCareRuleV1Request"
"$ref": "#/definitions/PlantsAPIGetPlantsWithCareRulesV1Body"
}
}
],
Expand Down Expand Up @@ -416,7 +440,7 @@
"type": "object",
"properties": {
"filter": {
"$ref": "#/definitions/v1Filter"
"$ref": "#/definitions/plantsv1Filter"
}
}
},
Expand All @@ -427,15 +451,78 @@
"type": "boolean"
},
"filter": {
"$ref": "#/definitions/v1Filter"
"$ref": "#/definitions/plantsv1Filter"
}
}
},
"PlantsAPIGetPlantsWithCareRulesV1Body": {
"type": "object",
"properties": {
"filter": {
"$ref": "#/definitions/v1GetPlantsWithCareRulesV1RequestFilter"
}
}
},
"PlantsAPIGetTradedPlantsV1Body": {
"type": "object",
"properties": {
"filter": {
"$ref": "#/definitions/v1Filter"
"$ref": "#/definitions/plantsv1Filter"
}
}
},
"plantsv1Filter": {
"type": "object",
"properties": {
"species": {
"type": "string"
},
"type": {
"type": "string"
},
"size": {
"type": "array",
"items": {
"type": "string"
}
},
"lightCondition": {
"type": "array",
"items": {
"type": "string"
}
},
"wateringFrequency": {
"type": "array",
"items": {
"type": "string"
}
},
"temperatureRegime": {
"type": "array",
"items": {
"type": "string"
}
},
"careComplexity": {
"type": "array",
"items": {
"type": "string"
}
},
"description": {
"type": "string"
},
"place": {
"type": "string"
},
"priceFrom": {
"type": "number",
"format": "float"
},
"priceTo": {
"type": "number",
"format": "float"
}
}
},
Expand Down Expand Up @@ -534,6 +621,18 @@
},
"descriptionAddition": {
"type": "string"
},
"type": {
"type": "string"
},
"lightCondition": {
"type": "string"
},
"temperatureRegime": {
"type": "string"
},
"image": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -592,61 +691,6 @@
"v1CreatePlantV1Response": {
"type": "object"
},
"v1Filter": {
"type": "object",
"properties": {
"species": {
"type": "string"
},
"type": {
"type": "string"
},
"size": {
"type": "array",
"items": {
"type": "string"
}
},
"lightCondition": {
"type": "array",
"items": {
"type": "string"
}
},
"wateringFrequency": {
"type": "array",
"items": {
"type": "string"
}
},
"temperatureRegime": {
"type": "array",
"items": {
"type": "string"
}
},
"careComplexity": {
"type": "array",
"items": {
"type": "string"
}
},
"description": {
"type": "string"
},
"place": {
"type": "string"
},
"priceFrom": {
"type": "number",
"format": "float"
},
"priceTo": {
"type": "number",
"format": "float"
}
}
},
"v1GetActivePlantsV1Response": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -804,6 +848,29 @@
}
}
},
"v1GetPlantsWithCareRulesV1RequestFilter": {
"type": "object",
"properties": {
"species": {
"type": "string"
},
"type": {
"type": "string"
},
"lightCondition": {
"type": "array",
"items": {
"type": "string"
}
},
"temperatureRegime": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"v1GetPlantsWithCareRulesV1Response": {
"type": "object",
"properties": {
Expand Down
11 changes: 11 additions & 0 deletions server/internal/handlers/plants/create_new_care_rule_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (h *Handler) CreateNewCareRuleV1(
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid user id")
}
user, err := h.storage.GetUserById(ctx, req.UserId)
if err != nil {
return nil, status.Errorf(codes.NotFound, "user not found %v", err)
}
err = h.storage.CreateNewCareRule(ctx, &models.CareRules{
Species: req.Species,
CreatedAt: time.Now().UTC(),
Expand All @@ -29,8 +33,15 @@ func (h *Handler) CreateNewCareRuleV1(
UserID: userId,
DescriptionAddition: req.DescriptionAddition,
CreatedAt: time.Now().UTC(),
UserName: user.Name,
UserSurname: user.Surname,
UserFatherName: user.FatherName,
},
},
LightCondition: req.LightCondition,
Image: req.Image,
Type: req.Type,
TemperatureRegime: req.TemperatureRegime,
})
if err != nil {
return nil, status.Error(codes.Internal, "Internal error occured")
Expand Down
Loading

0 comments on commit 7affdc8

Please sign in to comment.