add category insert
This commit is contained in:
+27
-5
@@ -3,24 +3,46 @@ package app
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
|
||||
proto "git.3crabs.ru/save_my_money/smm_core/proto"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
proto.UnsafeSmmCoreServer
|
||||
categoryService *category.CategoryService
|
||||
}
|
||||
|
||||
func NewServer() proto.SmmCoreServer {
|
||||
return &Server{}
|
||||
func NewServer(
|
||||
categoryService *category.CategoryService,
|
||||
) proto.SmmCoreServer {
|
||||
return &Server{
|
||||
categoryService: categoryService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Ping(_ context.Context, _ *proto.PingReq) (*proto.PingRsp, error) {
|
||||
return &proto.PingRsp{}, nil
|
||||
}
|
||||
|
||||
// AddCategory implements proto.SmmCoreServer.
|
||||
func (s *Server) AddCategory(context.Context, *proto.CreateCategoryReq) (*proto.Category, error) {
|
||||
panic("unimplemented")
|
||||
func (s *Server) AddCategory(ctx context.Context, req *proto.CreateCategoryReq) (*proto.Category, error) {
|
||||
res, err := s.categoryService.AddCategory(
|
||||
ctx,
|
||||
&category.CategoryEntity{
|
||||
Name: req.Name,
|
||||
UserId: int(req.UserId),
|
||||
Favorite: req.Favorite,
|
||||
MonthlyLimit: int(req.MonthlyLimit),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.Category{
|
||||
Id: int32(res.Id),
|
||||
Name: res.Name,
|
||||
Favorite: res.Favorite,
|
||||
MonthlyLimit: req.MonthlyLimit,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetCategories implements proto.SmmCoreServer.
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package category
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type CategoryEntity struct {
|
||||
Id int
|
||||
Name string
|
||||
UserId int
|
||||
Favorite bool
|
||||
MonthlyLimit int
|
||||
}
|
||||
|
||||
type CategoryService struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewCategoryService(
|
||||
db *pgxpool.Pool,
|
||||
) *CategoryService {
|
||||
return &CategoryService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CategoryService) AddCategory(ctx context.Context, category *CategoryEntity) (*CategoryEntity, error) {
|
||||
query := `INSERT INTO categories (name, user_id, favorite, monthly_limit) VALUES (@name, @user_id, @favorite, @monthly_limit)`
|
||||
args := pgx.NamedArgs{
|
||||
"name": category.Name,
|
||||
"user_id": 1,
|
||||
"favorite": category.Favorite,
|
||||
"monthly_limit": category.MonthlyLimit,
|
||||
}
|
||||
_, err := s.db.Exec(ctx, query, args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to insert row: %w", err)
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
[{"kind":1,"language":"markdown","value":"# Добавление категории","outputs":[]},{"kind":2,"language":"rest-book","value":"POST http://localhost:8090/categories\n\n{\n \"name\": \"Продукты питания\",\n \"favorite\": true,\n \"monthlyLimit\": 5000\n}","outputs":[{"mime":"x-application/rest-book","value":{"status":200,"statusText":"OK","headers":{"Date":"Tue, 19 Nov 2024 15:43:01 GMT","Content-Type":"application/json","Content-Length":"88"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"rest-book","Content-Length":78}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost:8090/categories","timeout":10000,"headers":{"User-Agent":"rest-book"},"data":{"name":"Продукты питания","favorite":true,"monthlyLimit":5000}},"data":{"id":0,"name":"Продукты питания","favorite":true,"monthlyLimit":5000}}},{"mime":"text/x-json","value":{"status":200,"statusText":"OK","headers":{"Date":"Tue, 19 Nov 2024 15:43:01 GMT","Content-Type":"application/json","Content-Length":"88"},"config":{"timeout":10000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","User-Agent":"rest-book","Content-Length":78}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost:8090/categories","timeout":10000,"headers":{"User-Agent":"rest-book"},"data":{"name":"Продукты питания","favorite":true,"monthlyLimit":5000}},"data":{"id":0,"name":"Продукты питания","favorite":true,"monthlyLimit":5000}}},{"mime":"text/html","value":"[object Object]"}]}]
|
||||
Reference in New Issue
Block a user