smm_core/internal/app/server.go

200 lines
5.2 KiB
Go
Raw Normal View History

2024-11-09 11:18:51 +00:00
package app
import (
"context"
2024-11-24 09:02:13 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/budget"
2024-11-19 15:45:31 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
2024-11-20 08:32:43 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
2024-11-25 09:38:03 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/waste"
2024-11-09 11:18:51 +00:00
proto "git.3crabs.ru/save_my_money/smm_core/proto"
)
type Server struct {
proto.UnsafeSmmCoreServer
2024-11-19 15:45:31 +00:00
categoryService *category.CategoryService
2024-11-20 08:32:43 +00:00
userService *user.UserService
2024-11-24 09:02:13 +00:00
budgetService *budget.BudgetService
2024-11-25 09:38:03 +00:00
wasteService *waste.WasteService
2024-11-09 11:18:51 +00:00
}
2024-11-19 15:45:31 +00:00
func NewServer(
categoryService *category.CategoryService,
2024-11-20 08:32:43 +00:00
userService *user.UserService,
2024-11-24 09:02:13 +00:00
budgetService *budget.BudgetService,
2024-11-25 09:38:03 +00:00
wasteService *waste.WasteService,
2024-11-19 15:45:31 +00:00
) proto.SmmCoreServer {
return &Server{
categoryService: categoryService,
2024-11-20 08:32:43 +00:00
userService: userService,
2024-11-24 09:02:13 +00:00
budgetService: budgetService,
2024-11-25 09:38:03 +00:00
wasteService: wasteService,
2024-11-19 15:45:31 +00:00
}
2024-11-09 11:18:51 +00:00
}
2024-11-21 08:11:21 +00:00
func (s *Server) Ping(context.Context, *proto.PingReq) (*proto.PingRsp, error) {
2024-11-09 11:18:51 +00:00
return &proto.PingRsp{}, nil
}
2024-11-19 08:16:01 +00:00
2024-11-21 09:02:36 +00:00
func (s *Server) Login(ctx context.Context, req *proto.LoginReq) (*proto.User, error) {
user, err := s.userService.Login(
ctx,
&user.UserEntity{
Username: req.Username,
Password: req.Password,
},
)
if err != nil {
return nil, err
}
2024-11-24 09:53:52 +00:00
return mapUser(user), nil
}
2024-11-24 09:02:13 +00:00
func (s *Server) AddBudget(ctx context.Context, req *proto.AddBudgetReq) (*proto.Budget, error) {
budget, err := s.budgetService.AddBudget(
ctx,
&budget.BudgetEntity{
Name: req.Name,
StartDay: int(req.StartDay),
MonthlyLimit: int(req.MonthlyLimit),
},
)
if err != nil {
return nil, err
}
2024-11-24 09:53:52 +00:00
return mapBudget(budget), nil
}
func (s *Server) GetBudgets(ctx context.Context, req *proto.GetBudgetsReq) (*proto.Budgets, error) {
budgets, err := s.budgetService.GetBudgets(
ctx,
)
if err != nil {
return nil, err
}
res := make([]*proto.Budget, 0, len(budgets))
for _, budget := range budgets {
res = append(res, mapBudget(budget))
}
return &proto.Budgets{
Budgets: res,
}, nil
}
2024-11-24 15:24:44 +00:00
func (s *Server) AddUserToBudget(ctx context.Context, req *proto.AddUserToBudgetReq) (*proto.OK, error) {
_, err := s.budgetService.AddUserToBudget(
ctx,
int(req.BudgetId),
int(req.UserId),
)
if err != nil {
return nil, err
2024-11-24 09:53:52 +00:00
}
2024-11-24 15:24:44 +00:00
return &proto.OK{}, nil
2024-11-21 08:11:21 +00:00
}
2024-11-25 06:36:14 +00:00
func (s *Server) AddCategory(ctx context.Context, req *proto.AddCategoryReq) (*proto.Category, error) {
category, err := s.categoryService.AddCategory(
ctx,
&category.CategoryEntity{
Name: req.Name,
BudgetId: int(req.BudgetId),
Favorite: req.Favorite,
MonthlyLimit: int(req.MonthlyLimit),
},
)
if err != nil {
return nil, err
}
return mapCategory(category), nil
}
func (s *Server) GetBudgetCategories(ctx context.Context, req *proto.GetBudgetCategoriesReq) (*proto.Categories, error) {
categories, err := s.budgetService.GetBudgetCategories(
ctx,
int(req.BudgetId),
)
if err != nil {
return nil, err
}
res := make([]*proto.Category, 0, len(categories))
for _, category := range categories {
res = append(res, mapCategory(category))
}
return &proto.Categories{
Categories: res,
}, nil
2024-11-21 08:11:21 +00:00
}
2024-11-25 09:38:03 +00:00
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),
2024-11-27 23:43:05 +00:00
BudgetId: int(req.BudgetId),
},
)
if err != nil {
return nil, err
}
return mapWaste(waste), nil
}
func (s *Server) AddWasteByText(ctx context.Context, req *proto.AddWasteTextReq) (*proto.Waste, error) {
waste, err := s.wasteService.AddWasteByText(
ctx,
&waste.WasteTextEntity{
Text: req.Text,
CategoryId: int(req.CategoryId),
BudgetId: int(req.BudgetId),
2024-11-25 09:38:03 +00:00
},
)
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) {
2024-11-21 08:11:21 +00:00
panic("unimplemented")
}
// DeleteBudget implements proto.SmmCoreServer.
func (s *Server) DeleteBudget(context.Context, *proto.DeleteBudgetReq) (*proto.Budget, error) {
panic("unimplemented")
}
// DeleteCategories implements proto.SmmCoreServer.
func (s *Server) DeleteCategories(context.Context, *proto.DeleteCategoriesReq) (*proto.Category, error) {
panic("unimplemented")
}
// DeleteWaste implements proto.SmmCoreServer.
func (s *Server) DeleteWaste(context.Context, *proto.DeleteWasteReq) (*proto.Waste, error) {
panic("unimplemented")
}
// GetBudgetUsers implements proto.SmmCoreServer.
func (s *Server) GetBudgetUsers(context.Context, *proto.GetBudgetUsersReq) (*proto.Users, error) {
panic("unimplemented")
}
// RemoveUserFromBudget implements proto.SmmCoreServer.
func (s *Server) RemoveUserFromBudget(context.Context, *proto.RemoveUserFromBudgetReq) (*proto.Budget, error) {
panic("unimplemented")
}
// UpdateBudget implements proto.SmmCoreServer.
func (s *Server) UpdateBudget(context.Context, *proto.UpdateBudgetReq) (*proto.Budget, error) {
2024-11-19 08:16:01 +00:00
panic("unimplemented")
}
// UpdateCategory implements proto.SmmCoreServer.
func (s *Server) UpdateCategory(context.Context, *proto.UpdateCategoryReq) (*proto.Category, error) {
panic("unimplemented")
}