add create user method

This commit is contained in:
2024-11-20 15:32:43 +07:00
parent 074a202dc2
commit bd6b5fe4a8
10 changed files with 467 additions and 87 deletions
+26
View File
@@ -2,21 +2,28 @@ package app
import (
"context"
"errors"
"git.3crabs.ru/save_my_money/smm_core/internal/services/category"
"git.3crabs.ru/save_my_money/smm_core/internal/services/user"
proto "git.3crabs.ru/save_my_money/smm_core/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Server struct {
proto.UnsafeSmmCoreServer
categoryService *category.CategoryService
userService *user.UserService
}
func NewServer(
categoryService *category.CategoryService,
userService *user.UserService,
) proto.SmmCoreServer {
return &Server{
categoryService: categoryService,
userService: userService,
}
}
@@ -24,6 +31,25 @@ func (s *Server) Ping(_ context.Context, _ *proto.PingReq) (*proto.PingRsp, erro
return &proto.PingRsp{}, nil
}
func (s *Server) AddUser(ctx context.Context, req *proto.CreateUserReq) (*proto.User, error) {
res, err := s.userService.AddUser(
ctx,
&user.UserEntity{
Username: req.Username,
},
)
if err != nil {
if errors.Is(err, &user.UsernameAlreadyExistsErr{}) {
return nil, status.Error(codes.AlreadyExists, "Пользователь с таким username уже существует")
}
return nil, err
}
return &proto.User{
Id: int32(res.Id),
Username: res.Username,
}, nil
}
func (s *Server) AddCategory(ctx context.Context, req *proto.CreateCategoryReq) (*proto.Category, error) {
res, err := s.categoryService.AddCategory(
ctx,
+1
View File
@@ -0,0 +1 @@
[{"kind":1,"language":"markdown","value":"# Добавление пользователя","outputs":[]},{"kind":2,"language":"rest-book","value":"POST http://localhost:8090/users\n\n{\n \"username\": \"foo\"\n}","outputs":[{"mime":"x-application/rest-book","value":{"status":409,"statusText":"Conflict","headers":{"Date":"Wed, 20 Nov 2024 08:30:32 GMT","Content-Type":"application/json","Content-Length":"111"},"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":18}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost:8090/users","timeout":10000,"headers":{"User-Agent":"rest-book"},"data":{"username":"bar"}},"data":{"code":6,"message":"Пользователь с таким username уже существует","details":[]}}},{"mime":"text/x-json","value":{"status":409,"statusText":"Conflict","headers":{"Date":"Wed, 20 Nov 2024 08:30:32 GMT","Content-Type":"application/json","Content-Length":"111"},"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":18}},"request":{"method":"POST","httpVersion":"1.1","responseUrl":"http://localhost:8090/users","timeout":10000,"headers":{"User-Agent":"rest-book"},"data":{"username":"bar"}},"data":{"code":6,"message":"Пользователь с таким username уже существует","details":[]}}},{"mime":"text/html","value":"[object Object]"}]}]
+51
View File
@@ -0,0 +1,51 @@
package user
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
type UserEntity struct {
Id int
Username string
}
type UsernameAlreadyExistsErr struct{}
func (e *UsernameAlreadyExistsErr) Error() string {
return "username already exists error"
}
type UserService struct {
db *pgxpool.Pool
}
func NewUserService(
db *pgxpool.Pool,
) *UserService {
return &UserService{
db: db,
}
}
func (s *UserService) AddUser(ctx context.Context, user *UserEntity) (*UserEntity, error) {
query := `INSERT INTO users (username) VALUES (@username) RETURNING id`
args := pgx.NamedArgs{
"username": user.Username,
}
if err := s.db.QueryRow(ctx, query, args).Scan(&user.Id); err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == "23505" && pgErr.ConstraintName == "users_username_key" { // unique_violation
return nil, &UsernameAlreadyExistsErr{}
}
}
return nil, fmt.Errorf("unable to insert row: %w", err)
}
return user, nil
}