Skip to content

Commit

Permalink
Create archive and active plants
Browse files Browse the repository at this point in the history
  • Loading branch information
AnjeZenda committed Dec 3, 2024
1 parent b4c2bd4 commit 2e2bb4c
Show file tree
Hide file tree
Showing 13 changed files with 964 additions and 142 deletions.
26 changes: 26 additions & 0 deletions server/api/plants/v1/plants.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ service PlantsAPI {
body: "*"
};
};

rpc GetActivePlantsV1 (GetActivePlantsV1Request) returns (GetActivePlantsV1Response) {
option (google.api.http) = {
get: "/api/plants/active/{userId}"
};
};

rpc GetArchivedPlantsV1 (GetArchivedPlantsV1Request) returns (GetArchivedPlantsV1Response) {
option (google.api.http) = {
get: "/api/plants/archive/{userId}"
};
};
}

message Filter {
Expand Down Expand Up @@ -237,4 +249,18 @@ message Plant {
float price = 4;
google.protobuf.Timestamp createdAt = 5;
string place = 6;
}

message GetActivePlantsV1Request{
string userId = 1;
}
message GetActivePlantsV1Response{
repeated Plant plants = 1;
}

message GetArchivedPlantsV1Request{
string userId = 1;
}
message GetArchivedPlantsV1Response{
repeated Plant plants = 1;
}
8 changes: 0 additions & 8 deletions server/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,3 @@ func main() {
panic(err)
}
}

// {
// "buyerId": "5f2d8c1d1d8e2f1a1a2b3c7c",
// "plantId": "5f1d7c1d1d8e2f1a1a2b3c4e",
// "price": 1500,
// "sellerId": "5f2d8c1d1d8e2f1a1a2b3c7a",
// "species": "Алоэ"
// }
121 changes: 121 additions & 0 deletions server/gen/docs/data/v1/data.swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"swagger": "2.0",
"info": {
"title": "data/v1/data.proto",
"version": "version not set"
},
"tags": [
{
"name": "DataAPI"
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/api/data": {
"get": {
"operationId": "DataAPI_ExportDBV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1ExportDBV1Response"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"tags": [
"DataAPI"
]
},
"post": {
"operationId": "DataAPI_ImportDBV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1ImportDBV1Response"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/v1ImportDBV1Request"
}
}
],
"tags": [
"DataAPI"
]
}
}
},
"definitions": {
"protobufAny": {
"type": "object",
"properties": {
"@type": {
"type": "string"
}
},
"additionalProperties": {}
},
"rpcStatus": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"details": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/protobufAny"
}
}
}
},
"v1ExportDBV1Response": {
"type": "object",
"properties": {
"db": {
"type": "string"
}
}
},
"v1ImportDBV1Request": {
"type": "object",
"properties": {
"db": {
"type": "string"
}
}
},
"v1ImportDBV1Response": {
"type": "object"
}
}
}
84 changes: 84 additions & 0 deletions server/gen/docs/plants/v1/plants.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@
]
}
},
"/api/plants/active/{userId}": {
"get": {
"operationId": "PlantsAPI_GetActivePlantsV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1GetActivePlantsV1Response"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"type": "string"
}
],
"tags": [
"PlantsAPI"
]
}
},
"/api/plants/add": {
"post": {
"operationId": "PlantsAPI_CreatePlantV1",
Expand Down Expand Up @@ -214,6 +244,36 @@
]
}
},
"/api/plants/archive/{userId}": {
"get": {
"operationId": "PlantsAPI_GetArchivedPlantsV1",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/v1GetArchivedPlantsV1Response"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"type": "string"
}
],
"tags": [
"PlantsAPI"
]
}
},
"/api/plants/buy": {
"post": {
"operationId": "PlantsAPI_BuyPlantV1",
Expand Down Expand Up @@ -587,6 +647,30 @@
}
}
},
"v1GetActivePlantsV1Response": {
"type": "object",
"properties": {
"plants": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/v1Plant"
}
}
}
},
"v1GetArchivedPlantsV1Response": {
"type": "object",
"properties": {
"plants": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/v1Plant"
}
}
}
},
"v1GetBoughtPlantsV1Response": {
"type": "object",
"properties": {
Expand Down
35 changes: 35 additions & 0 deletions server/internal/handlers/plants/get_active_plants_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package plants

import (
"context"

api "plants/internal/pkg/pb/plants/v1"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)

func (h *Handler) GetActivePlantsV1(
ctx context.Context,
req *api.GetActivePlantsV1Request,
) (*api.GetActivePlantsV1Response, error) {
plants, err := h.storage.GetPlantsByUserId(ctx, req.UserId, false)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot find plants due to %v", err)
}
result := make([]*api.Plant, len(plants))
for i, p := range plants {
result[i] = &api.Plant{
Id: p.ID.Hex(),
Image: p.Image,
Species: p.Species,
Price: p.Price,
CreatedAt: timestamppb.New(p.CreatedAt),
Place: p.Place,
}
}
return &api.GetActivePlantsV1Response{
Plants: result,
}, nil
}
35 changes: 35 additions & 0 deletions server/internal/handlers/plants/get_archived_plants_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package plants

import (
"context"

api "plants/internal/pkg/pb/plants/v1"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)

func (h *Handler) GetArchivedPlantsV1(
ctx context.Context,
req *api.GetArchivedPlantsV1Request,
) (*api.GetArchivedPlantsV1Response, error) {
plants, err := h.storage.GetPlantsByUserId(ctx, req.UserId, true)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot find plants due to %v", err)
}
result := make([]*api.Plant, len(plants))
for i, p := range plants {
result[i] = &api.Plant{
Id: p.ID.Hex(),
Image: p.Image,
Species: p.Species,
Price: p.Price,
CreatedAt: timestamppb.New(p.CreatedAt),
Place: p.Place,
}
}
return &api.GetArchivedPlantsV1Response{
Plants: result,
}, nil
}
1 change: 1 addition & 0 deletions server/internal/handlers/plants/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Storage interface {
GetUserTrades(ctx context.Context, id string) ([]primitive.ObjectID, error)
GetPlantsByIds(ctx context.Context, ids []string, fltr *models.Filter) ([]*models.Plant, error)
AddTradeToUser(ctx context.Context, userId, tradeId primitive.ObjectID) error
GetPlantsByUserId(ctx context.Context, userId string, isSold bool) ([]*models.Plant, error)
}

type Handler struct {
Expand Down
4 changes: 2 additions & 2 deletions server/internal/pkg/pb/data/v1/data.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/internal/pkg/pb/data/v1/data_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e2bb4c

Please sign in to comment.