add create waste route
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2024-11-25 16:38:03 +07:00
parent 675c2884b7
commit 26ca649e8e
7 changed files with 112 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import (
"git.3crabs.ru/save_my_money/smm_core/internal/services/budget"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
"git.3crabs.ru/save_my_money/smm_core/internal/services/waste"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jackc/pgx/v5/pgxpool"
"google.golang.org/grpc"
@ -40,6 +41,7 @@ func main() {
categoryService := category.NewCategoryService(dbpool)
userService := user.NewUserService(dbpool)
budgetService := budget.NewBudgetService(dbpool, categoryService)
wasteService := waste.NewWasteService(dbpool, categoryService)
// Create a gRPC server object
s := grpc.NewServer(
@ -69,6 +71,7 @@ func main() {
categoryService,
userService,
budgetService,
wasteService,
),
)
// Serve gRPC server

View File

@ -4,6 +4,7 @@ import (
"git.3crabs.ru/save_my_money/smm_core/internal/services/budget"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
"git.3crabs.ru/save_my_money/smm_core/internal/services/waste"
proto "git.3crabs.ru/save_my_money/smm_core/proto"
)
@ -40,3 +41,12 @@ func mapCategories(categories []*category.CategoryEntity) []*proto.Category {
}
return res
}
func mapWaste(waste *waste.WasteEntity) *proto.Waste {
return &proto.Waste{
Id: int32(waste.Id),
Name: waste.Name,
Price: int32(waste.Price),
Amount: waste.Amount,
}
}

View File

@ -6,6 +6,7 @@ import (
"git.3crabs.ru/save_my_money/smm_core/internal/services/budget"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
"git.3crabs.ru/save_my_money/smm_core/internal/services/waste"
proto "git.3crabs.ru/save_my_money/smm_core/proto"
)
@ -14,17 +15,20 @@ type Server struct {
categoryService *category.CategoryService
userService *user.UserService
budgetService *budget.BudgetService
wasteService *waste.WasteService
}
func NewServer(
categoryService *category.CategoryService,
userService *user.UserService,
budgetService *budget.BudgetService,
wasteService *waste.WasteService,
) proto.SmmCoreServer {
return &Server{
categoryService: categoryService,
userService: userService,
budgetService: budgetService,
wasteService: wasteService,
}
}
@ -136,8 +140,24 @@ func (s *Server) GetBudgetCategories(ctx context.Context, req *proto.GetBudgetCa
}, nil
}
// AddWaste implements proto.SmmCoreServer.
func (s *Server) AddWaste(context.Context, *proto.AddWasteReq) (*proto.Waste, error) {
func (s *Server) AddWaste(ctx context.Context, req *proto.AddWasteReq) (*proto.Waste, error) {
waste, err := s.wasteService.AddWaste(
ctx,
&waste.WasteEntity{
Name: req.Name,
Price: int(req.Price),
Amount: req.Amount,
CategoryId: int(req.CategoryId),
},
)
if err != nil {
return nil, err
}
return mapWaste(waste), nil
}
// GetCategoriesStat implements proto.SmmCoreServer.
func (s *Server) GetCategoriesStat(context.Context, *proto.GetCategoriesStatReq) (*proto.CategoriesStat, error) {
panic("unimplemented")
}
@ -161,11 +181,6 @@ func (s *Server) GetBudgetUsers(context.Context, *proto.GetBudgetUsersReq) (*pro
panic("unimplemented")
}
// GetCategoriesStat implements proto.SmmCoreServer.
func (s *Server) GetCategoriesStat(context.Context, *proto.GetCategoriesStatReq) (*proto.CategoriesStat, error) {
panic("unimplemented")
}
// RemoveUserFromBudget implements proto.SmmCoreServer.
func (s *Server) RemoveUserFromBudget(context.Context, *proto.RemoveUserFromBudgetReq) (*proto.Budget, error) {
panic("unimplemented")

View File

@ -41,3 +41,25 @@ func (s *CategoryService) AddCategory(ctx context.Context, category *CategoryEnt
}
return category, nil
}
func (s *CategoryService) GetCategory(ctx context.Context, categoryId int) (*CategoryEntity, error) {
query := `SELECT id, name, budget_id, favorite, monthly_limit FROM categories WHERE id = @id`
args := pgx.NamedArgs{
"id": categoryId,
}
rows, err := s.db.Query(ctx, query, args)
if err != nil {
return nil, err
}
categories := []*CategoryEntity{}
defer rows.Close()
for rows.Next() {
category := &CategoryEntity{}
err = rows.Scan(&category.Id, &category.Name, &category.BudgetId, &category.Favorite, &category.MonthlyLimit)
if err != nil {
return nil, err
}
categories = append(categories, category)
}
return categories[0], nil
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,54 @@
package waste
import (
"context"
"fmt"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type WasteEntity struct {
Id int
Name string
Price int
Amount float32
BudgetId int
CategoryId int
}
type WasteService struct {
db *pgxpool.Pool
categoryService *category.CategoryService
}
func NewWasteService(
db *pgxpool.Pool,
categoryService *category.CategoryService,
) *WasteService {
return &WasteService{
db: db,
categoryService: categoryService,
}
}
func (s *WasteService) AddWaste(ctx context.Context, waste *WasteEntity) (*WasteEntity, error) {
category, err := s.categoryService.GetCategory(ctx, waste.CategoryId)
if err != nil {
return nil, err
}
query := `INSERT INTO wastes (name, price, amount, budget_id, category_id) VALUES (@name, @price, @amount, @budget_id, @category_id) RETURNING id`
args := pgx.NamedArgs{
"name": waste.Name,
"price": waste.Price,
"amount": waste.Amount,
"budget_id": category.BudgetId,
"category_id": waste.CategoryId,
}
if err := s.db.QueryRow(ctx, query, args).Scan(&waste.Id); err != nil {
return nil, fmt.Errorf("unable to insert row: %w", err)
}
return waste, nil
}