Skip to content

Commit

Permalink
added extra logic
Browse files Browse the repository at this point in the history
  • Loading branch information
syth0le committed Nov 11, 2023
1 parent b77484d commit 41727da
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 7 additions & 6 deletions internal/service/user/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
"fmt"
"time"

"social-network/internal/model"
"social-network/internal/storage"
Expand Down Expand Up @@ -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,
})
Expand All @@ -50,7 +51,7 @@ type RegisterParams struct {
SecondName string
Age int
Sex string
Birthdate string
Birthdate time.Time
Biography string
City string
}
Expand All @@ -61,27 +62,27 @@ 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)
if err != nil {
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,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/postgres/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
9 changes: 9 additions & 0 deletions internal/utils/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

import "github.com/google/uuid"

const serviceNamePrefix = "snw"

func GenerateUUID() string {
return serviceNamePrefix + uuid.New().String()
}

0 comments on commit 41727da

Please sign in to comment.