smm_core/internal/app/server.go

89 lines
2.5 KiB
Go
Raw Normal View History

2024-11-09 11:18:51 +00:00
package app
import (
"context"
2024-11-20 08:32:43 +00:00
"errors"
2024-11-09 11:18:51 +00:00
2024-11-19 15:45:31 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
2024-11-20 16:26:51 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/context_utils"
2024-11-20 08:32:43 +00:00
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
2024-11-09 11:18:51 +00:00
proto "git.3crabs.ru/save_my_money/smm_core/proto"
2024-11-20 08:32:43 +00:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2024-11-09 11:18:51 +00:00
)
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-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-19 15:45:31 +00:00
) proto.SmmCoreServer {
return &Server{
categoryService: categoryService,
2024-11-20 08:32:43 +00:00
userService: userService,
2024-11-19 15:45:31 +00:00
}
2024-11-09 11:18:51 +00:00
}
func (s *Server) Ping(_ context.Context, _ *proto.PingReq) (*proto.PingRsp, error) {
return &proto.PingRsp{}, nil
}
2024-11-19 08:16:01 +00:00
2024-11-20 08:32:43 +00:00
func (s *Server) AddUser(ctx context.Context, req *proto.CreateUserReq) (*proto.User, error) {
res, err := s.userService.AddUser(
ctx,
&user.UserEntity{
Username: req.Username,
},
)
if err != nil {
if errors.Is(err, &user.UsernameAlreadyExistsErr{}) {
return nil, status.Error(codes.AlreadyExists, "Пользователь с таким username уже существует")
}
return nil, err
}
return &proto.User{
Id: int32(res.Id),
Username: res.Username,
}, nil
}
2024-11-19 15:45:31 +00:00
func (s *Server) AddCategory(ctx context.Context, req *proto.CreateCategoryReq) (*proto.Category, error) {
res, err := s.categoryService.AddCategory(
ctx,
&category.CategoryEntity{
Name: req.Name,
Favorite: req.Favorite,
MonthlyLimit: int(req.MonthlyLimit),
},
)
if err != nil {
2024-11-20 16:26:51 +00:00
if errors.Is(err, &context_utils.UnauthorizedErr{}) {
return nil, status.Error(codes.Unauthenticated, "Клиент не авторизован")
}
if errors.Is(err, &category.CategoryAlreadyExistsErr{}) {
return nil, status.Error(codes.AlreadyExists, "Категория с таким именеи уже существует")
}
2024-11-19 15:45:31 +00:00
return nil, err
}
return &proto.Category{
Id: int32(res.Id),
Name: res.Name,
Favorite: res.Favorite,
MonthlyLimit: req.MonthlyLimit,
}, nil
2024-11-19 08:16:01 +00:00
}
// GetCategories implements proto.SmmCoreServer.
func (s *Server) GetCategories(context.Context, *proto.CategoryFilterReq) (*proto.Categories, error) {
panic("unimplemented")
}
// UpdateCategory implements proto.SmmCoreServer.
func (s *Server) UpdateCategory(context.Context, *proto.UpdateCategoryReq) (*proto.Category, error) {
panic("unimplemented")
}