add get budgets method
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2024-11-24 16:53:52 +07:00
parent bf85f31056
commit 95fe8b530e
5 changed files with 72 additions and 13 deletions

14
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"sqltools.connections": [
{
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"driver": "PostgreSQL",
"name": "smm",
"database": "smm-core",
"username": "crab",
"password": "crab"
}
]
}

View File

@ -43,10 +43,7 @@ func (s *Server) AddUser(ctx context.Context, req *proto.AddUserReq) (*proto.Use
if err != nil {
return nil, err
}
return &proto.User{
Id: int32(user.Id),
Username: req.Username,
}, nil
return mapUser(user), nil
}
func (s *Server) Login(ctx context.Context, req *proto.LoginReq) (*proto.User, error) {
@ -60,10 +57,14 @@ func (s *Server) Login(ctx context.Context, req *proto.LoginReq) (*proto.User, e
if err != nil {
return nil, err
}
return mapUser(user), nil
}
func mapUser(user *user.UserEntity) *proto.User {
return &proto.User{
Id: int32(user.Id),
Username: req.Username,
}, nil
Username: user.Username,
}
}
func (s *Server) AddBudget(ctx context.Context, req *proto.AddBudgetReq) (*proto.Budget, error) {
@ -78,12 +79,32 @@ func (s *Server) AddBudget(ctx context.Context, req *proto.AddBudgetReq) (*proto
if err != nil {
return nil, err
}
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
}
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),
}, nil
}
}
// AddCategory implements proto.SmmCoreServer.
@ -121,11 +142,6 @@ func (s *Server) GetBudgetUsers(context.Context, *proto.GetBudgetUsersReq) (*pro
panic("unimplemented")
}
// GetBudgets implements proto.SmmCoreServer.
func (s *Server) GetBudgets(context.Context, *proto.GetBudgetsReq) (*proto.Budgets, error) {
panic("unimplemented")
}
// GetCategories implements proto.SmmCoreServer.
func (s *Server) GetCategories(context.Context, *proto.GetCategoriesReq) (*proto.Categories, error) {
panic("unimplemented")

View File

@ -64,3 +64,31 @@ func (s *BudgetService) AddBudget(ctx context.Context, budget *BudgetEntity) (*B
}
return budget, nil
}
func (s *BudgetService) GetBudgets(ctx context.Context) ([]*BudgetEntity, error) {
userId, err := context_utils.GetUserId(ctx)
if err != nil {
return nil, err
}
query := `SELECT b.id, b.name, b.start_day, b.monthly_limit FROM budgets b JOIN users_budgets ub ON ub.budget_id = b.id WHERE ub.user_id = @user_id`
args := pgx.NamedArgs{
"user_id": userId,
}
rows, err := s.db.Query(ctx, query, args)
if err != nil {
return nil, err
}
budgets := []*BudgetEntity{}
defer rows.Close()
for rows.Next() {
budget := &BudgetEntity{}
err = rows.Scan(&budget.Id, &budget.Name, &budget.StartDay, &budget.MonthlyLimit)
if err != nil {
return nil, err
}
budgets = append(budgets, budget)
}
return budgets, nil
}

File diff suppressed because one or more lines are too long

1
smm.session.sql Normal file
View File

@ -0,0 +1 @@
SELECT b.id, b.name, b.start_day, b.monthly_limit FROM budgets b JOIN users_budgets ub ON ub.budget_id = b.id WHERE ub.user_id = 1;