From 41727da864e5ca96fbf88ca14172bb2badf3c0be Mon Sep 17 00:00:00 2001 From: syth0le Date: Sat, 11 Nov 2023 23:54:30 +0300 Subject: [PATCH] added extra logic --- go.mod | 1 + go.sum | 2 ++ internal/service/user/core.go | 13 +++++++------ internal/storage/postgres/user.go | 2 +- internal/storage/storage.go | 2 +- internal/utils/id.go | 9 +++++++++ 6 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 internal/utils/id.go diff --git a/go.mod b/go.mod index 250a770..2d3d60b 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-chi/chi/v5 v5.0.10 github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a github.com/golang-jwt/jwt/v5 v5.1.0 + github.com/google/uuid v1.4.0 github.com/ilyakaznacheev/cleanenv v1.5.0 github.com/jmoiron/sqlx v1.3.5 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index c4ad8e7..a0c518c 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/golang-jwt/jwt/v5 v5.1.0 h1:UGKbA/IPjtS6zLcdB7i5TyACMgSbOTiR8qzXgw8HWQU= github.com/golang-jwt/jwt/v5 v5.1.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= diff --git a/internal/service/user/core.go b/internal/service/user/core.go index f72d190..89afbfb 100644 --- a/internal/service/user/core.go +++ b/internal/service/user/core.go @@ -3,6 +3,7 @@ package user import ( "context" "fmt" + "time" "social-network/internal/model" "social-network/internal/storage" @@ -32,7 +33,7 @@ func (s *ServiceImpl) Login(ctx context.Context, params *LoginParams) (*model.To return nil, fmt.Errorf("hash password: %w", err) } - token, err := s.Storage.User().LoginUser(ctx, model.UserLogin{ + token, err := s.Storage.User().LoginUser(ctx, &model.UserLogin{ Username: params.Username, HashedPassword: hashedPassword, }) @@ -50,7 +51,7 @@ type RegisterParams struct { SecondName string Age int Sex string - Birthdate string + Birthdate time.Time Biography string City string } @@ -61,19 +62,19 @@ func (s *ServiceImpl) Register(ctx context.Context, params *RegisterParams) (*mo return nil, fmt.Errorf("hash password: %w", err) } - user, err := s.Storage.User().RegisterUser(ctx, model.UserRegister{ + user, err := s.Storage.User().CreateUser(ctx, &model.UserRegister{ + ID: utils.GenerateUUID(), Username: params.Username, HashedPassword: hashedPassword, FirstName: params.FirstName, SecondName: params.SecondName, - Age: params.Age, Sex: params.Sex, Birthdate: params.Birthdate, Biography: params.Biography, City: params.City, }) if err != nil { - return nil, fmt.Errorf("register user: %w", err) + return nil, fmt.Errorf("create user: %w", err) } generatedToken, err := s.TokenGenerator.GenerateToken(user) @@ -81,7 +82,7 @@ func (s *ServiceImpl) Register(ctx context.Context, params *RegisterParams) (*mo return nil, fmt.Errorf("generate token: %w", err) } - token, err := s.Storage.Token().CreateToken(ctx, model.Token{ + token, err := s.Storage.Token().CreateToken(ctx, &model.Token{ UserID: user.UserID, Token: generatedToken, }) diff --git a/internal/storage/postgres/user.go b/internal/storage/postgres/user.go index 0e51311..7de32a2 100644 --- a/internal/storage/postgres/user.go +++ b/internal/storage/postgres/user.go @@ -14,7 +14,7 @@ func (s *Storage) LoginUser(ctx context.Context, userLogin *model.UserLogin) (*m return nil, nil } -func (s *Storage) RegisterUser(ctx context.Context, params *model.UserRegister) (*model.User, error) { +func (s *Storage) CreateUser(ctx context.Context, params *model.UserRegister) (*model.User, error) { err := params.Validate() if err != nil { return nil, fmt.Errorf("params validate: %w", err) diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 95bbad9..f483d7c 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -12,7 +12,7 @@ type Storage interface { type UserRepository interface { LoginUser(ctx context.Context, userLogin *model.UserLogin) (*model.User, error) - RegisterUser(ctx context.Context, user *model.UserRegister) (*model.User, error) + CreateUser(ctx context.Context, user *model.UserRegister) (*model.User, error) GetUserByID(ctx context.Context, id model.UserID) (*model.User, error) } diff --git a/internal/utils/id.go b/internal/utils/id.go new file mode 100644 index 0000000..d0e1dbe --- /dev/null +++ b/internal/utils/id.go @@ -0,0 +1,9 @@ +package utils + +import "github.com/google/uuid" + +const serviceNamePrefix = "snw" + +func GenerateUUID() string { + return serviceNamePrefix + uuid.New().String() +}