smm_core/internal/app/server.go

89 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"context"
"errors"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"git.3crabs.ru/save_my_money/smm_core/internal/services/context_utils"
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
proto "git.3crabs.ru/save_my_money/smm_core/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Server struct {
proto.UnsafeSmmCoreServer
categoryService *category.CategoryService
userService *user.UserService
}
func NewServer(
categoryService *category.CategoryService,
userService *user.UserService,
) proto.SmmCoreServer {
return &Server{
categoryService: categoryService,
userService: userService,
}
}
func (s *Server) Ping(_ context.Context, _ *proto.PingReq) (*proto.PingRsp, error) {
return &proto.PingRsp{}, nil
}
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
}
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 {
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, "Категория с таким именеи уже существует")
}
return nil, err
}
return &proto.Category{
Id: int32(res.Id),
Name: res.Name,
Favorite: res.Favorite,
MonthlyLimit: req.MonthlyLimit,
}, nil
}
// 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")
}