add category insert

This commit is contained in:
2024-11-19 22:45:31 +07:00
parent 20652b127c
commit 718d2e5e98
11 changed files with 164 additions and 64 deletions
@@ -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]"}]}]