add add user to budget route
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Владимир Фёдоров 2024-11-24 22:24:44 +07:00
parent 95fe8b530e
commit 130a3236e0
9 changed files with 483 additions and 413 deletions

View File

@ -57,9 +57,10 @@ service SmmCore {
}
// budget users
rpc AddUserToBudget(AddUserToBudgetReq) returns (Budget) {
rpc AddUserToBudget(AddUserToBudgetReq) returns (OK) {
option (google.api.http) = {
put: "/budgets/{budget_id}/users"
put: "/budgets/{budget_id}/users",
body: "*"
};
}
rpc GetBudgetUsers(GetBudgetUsersReq) returns (Users) {
@ -121,6 +122,8 @@ message PingReq {}
message PingRsp {}
message OK {}
message AddUserReq {
string username = 1;
string password = 2;

23
internal/app/mappers.go Normal file
View File

@ -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),
}
}

View File

@ -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")

View File

@ -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

File diff suppressed because it is too large Load Diff

View File

@ -257,14 +257,14 @@ func local_request_SmmCore_DeleteBudget_0(ctx context.Context, marshaler runtime
}
var (
filter_SmmCore_AddUserToBudget_0 = &utilities.DoubleArray{Encoding: map[string]int{"budget_id": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}}
)
func request_SmmCore_AddUserToBudget_0(ctx context.Context, marshaler runtime.Marshaler, client SmmCoreClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq AddUserToBudgetReq
var metadata runtime.ServerMetadata
if err := marshaler.NewDecoder(req.Body).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
var (
val string
ok bool
@ -282,13 +282,6 @@ func request_SmmCore_AddUserToBudget_0(ctx context.Context, marshaler runtime.Ma
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "budget_id", err)
}
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_SmmCore_AddUserToBudget_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.AddUserToBudget(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
@ -298,6 +291,10 @@ func local_request_SmmCore_AddUserToBudget_0(ctx context.Context, marshaler runt
var protoReq AddUserToBudgetReq
var metadata runtime.ServerMetadata
if err := marshaler.NewDecoder(req.Body).Decode(&protoReq); err != nil && err != io.EOF {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
var (
val string
ok bool
@ -315,13 +312,6 @@ func local_request_SmmCore_AddUserToBudget_0(ctx context.Context, marshaler runt
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "budget_id", err)
}
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_SmmCore_AddUserToBudget_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.AddUserToBudget(ctx, &protoReq)
return msg, metadata, err

View File

@ -53,7 +53,7 @@ type SmmCoreClient interface {
GetBudgets(ctx context.Context, in *GetBudgetsReq, opts ...grpc.CallOption) (*Budgets, error)
DeleteBudget(ctx context.Context, in *DeleteBudgetReq, opts ...grpc.CallOption) (*Budget, error)
// budget users
AddUserToBudget(ctx context.Context, in *AddUserToBudgetReq, opts ...grpc.CallOption) (*Budget, error)
AddUserToBudget(ctx context.Context, in *AddUserToBudgetReq, opts ...grpc.CallOption) (*OK, error)
GetBudgetUsers(ctx context.Context, in *GetBudgetUsersReq, opts ...grpc.CallOption) (*Users, error)
RemoveUserFromBudget(ctx context.Context, in *RemoveUserFromBudgetReq, opts ...grpc.CallOption) (*Budget, error)
// categories
@ -146,9 +146,9 @@ func (c *smmCoreClient) DeleteBudget(ctx context.Context, in *DeleteBudgetReq, o
return out, nil
}
func (c *smmCoreClient) AddUserToBudget(ctx context.Context, in *AddUserToBudgetReq, opts ...grpc.CallOption) (*Budget, error) {
func (c *smmCoreClient) AddUserToBudget(ctx context.Context, in *AddUserToBudgetReq, opts ...grpc.CallOption) (*OK, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(Budget)
out := new(OK)
err := c.cc.Invoke(ctx, SmmCore_AddUserToBudget_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
@ -261,7 +261,7 @@ type SmmCoreServer interface {
GetBudgets(context.Context, *GetBudgetsReq) (*Budgets, error)
DeleteBudget(context.Context, *DeleteBudgetReq) (*Budget, error)
// budget users
AddUserToBudget(context.Context, *AddUserToBudgetReq) (*Budget, error)
AddUserToBudget(context.Context, *AddUserToBudgetReq) (*OK, error)
GetBudgetUsers(context.Context, *GetBudgetUsersReq) (*Users, error)
RemoveUserFromBudget(context.Context, *RemoveUserFromBudgetReq) (*Budget, error)
// categories
@ -305,7 +305,7 @@ func (UnimplementedSmmCoreServer) GetBudgets(context.Context, *GetBudgetsReq) (*
func (UnimplementedSmmCoreServer) DeleteBudget(context.Context, *DeleteBudgetReq) (*Budget, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteBudget not implemented")
}
func (UnimplementedSmmCoreServer) AddUserToBudget(context.Context, *AddUserToBudgetReq) (*Budget, error) {
func (UnimplementedSmmCoreServer) AddUserToBudget(context.Context, *AddUserToBudgetReq) (*OK, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddUserToBudget not implemented")
}
func (UnimplementedSmmCoreServer) GetBudgetUsers(context.Context, *GetBudgetUsersReq) (*Users, error) {

View File

@ -77,7 +77,7 @@
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/smm_coreBudget"
"$ref": "#/definitions/smm_coreOK"
}
},
"default": {
@ -96,11 +96,12 @@
"format": "int32"
},
{
"name": "userId",
"in": "query",
"required": false,
"type": "integer",
"format": "int32"
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/SmmCoreAddUserToBudgetBody"
}
}
],
"tags": [
@ -582,6 +583,15 @@
}
},
"definitions": {
"SmmCoreAddUserToBudgetBody": {
"type": "object",
"properties": {
"userId": {
"type": "integer",
"format": "int32"
}
}
},
"SmmCoreUpdateBudgetBody": {
"type": "object",
"properties": {
@ -782,6 +792,9 @@
}
}
},
"smm_coreOK": {
"type": "object"
},
"smm_corePingRsp": {
"type": "object"
},