This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"git.3crabs.ru/save_my_money/smm_core/internal/services/budget"
|
||||
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
|
||||
proto "git.3crabs.ru/save_my_money/smm_core/proto"
|
||||
)
|
||||
|
||||
func mapUser(user *user.UserEntity) *proto.User {
|
||||
return &proto.User{
|
||||
Id: int32(user.Id),
|
||||
Username: user.Username,
|
||||
}
|
||||
}
|
||||
|
||||
func mapBudget(budget *budget.BudgetEntity) *proto.Budget {
|
||||
return &proto.Budget{
|
||||
Id: int32(budget.Id),
|
||||
Name: budget.Name,
|
||||
StartDay: int32(budget.StartDay),
|
||||
MonthlyLimit: int32(budget.MonthlyLimit),
|
||||
}
|
||||
}
|
||||
+9
-18
@@ -60,13 +60,6 @@ func (s *Server) Login(ctx context.Context, req *proto.LoginReq) (*proto.User, e
|
||||
return mapUser(user), nil
|
||||
}
|
||||
|
||||
func mapUser(user *user.UserEntity) *proto.User {
|
||||
return &proto.User{
|
||||
Id: int32(user.Id),
|
||||
Username: user.Username,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) AddBudget(ctx context.Context, req *proto.AddBudgetReq) (*proto.Budget, error) {
|
||||
budget, err := s.budgetService.AddBudget(
|
||||
ctx,
|
||||
@@ -98,13 +91,16 @@ func (s *Server) GetBudgets(ctx context.Context, req *proto.GetBudgetsReq) (*pro
|
||||
}, nil
|
||||
}
|
||||
|
||||
func mapBudget(budget *budget.BudgetEntity) *proto.Budget {
|
||||
return &proto.Budget{
|
||||
Id: int32(budget.Id),
|
||||
Name: budget.Name,
|
||||
StartDay: int32(budget.StartDay),
|
||||
MonthlyLimit: int32(budget.MonthlyLimit),
|
||||
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
|
||||
}
|
||||
return &proto.OK{}, nil
|
||||
}
|
||||
|
||||
// AddCategory implements proto.SmmCoreServer.
|
||||
@@ -112,11 +108,6 @@ func (s *Server) AddCategory(context.Context, *proto.AddCategoryReq) (*proto.Cat
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// AddUserToBudget implements proto.SmmCoreServer.
|
||||
func (s *Server) AddUserToBudget(context.Context, *proto.AddUserToBudgetReq) (*proto.Budget, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// AddWaste implements proto.SmmCoreServer.
|
||||
func (s *Server) AddWaste(context.Context, *proto.AddWasteReq) (*proto.Waste, error) {
|
||||
panic("unimplemented")
|
||||
|
||||
@@ -50,13 +50,9 @@ func (s *BudgetService) AddBudget(ctx context.Context, budget *BudgetEntity) (*B
|
||||
return nil, fmt.Errorf("unable to insert row: %w", err)
|
||||
}
|
||||
|
||||
query = `INSERT INTO users_budgets (user_id, budget_id) VALUES (@user_id, @budget_id) RETURNING id`
|
||||
args = pgx.NamedArgs{
|
||||
"user_id": userId,
|
||||
"budget_id": budget.Id,
|
||||
}
|
||||
if err := s.db.QueryRow(ctx, query, args).Scan(&budget.Id); err != nil {
|
||||
return nil, fmt.Errorf("unable to insert row: %w", err)
|
||||
_, err = s.AddUserToBudget(ctx, budget.Id, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
@@ -92,3 +88,20 @@ func (s *BudgetService) GetBudgets(ctx context.Context) ([]*BudgetEntity, error)
|
||||
|
||||
return budgets, nil
|
||||
}
|
||||
|
||||
func (s *BudgetService) AddUserToBudget(
|
||||
ctx context.Context,
|
||||
budgetId int,
|
||||
userId int,
|
||||
) (int, error) {
|
||||
query := `INSERT INTO users_budgets (user_id, budget_id) VALUES (@user_id, @budget_id) RETURNING id`
|
||||
args := pgx.NamedArgs{
|
||||
"user_id": userId,
|
||||
"budget_id": budgetId,
|
||||
}
|
||||
id := 0
|
||||
if err := s.db.QueryRow(ctx, query, args).Scan(&id); err != nil {
|
||||
return 0, fmt.Errorf("unable to insert row: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user