fix(niki): linter errors fixed

This commit is contained in:
mohammad mahdi rezaei 2024-01-11 10:43:37 +03:30
parent ce762ac29c
commit 052f062cca
58 changed files with 48 additions and 256 deletions

View File

@ -16,3 +16,6 @@ format:
@gci write $(ROOT)
@which golangci-lint || (go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0)
@golangci-lint run --fix
build:
go build main.go

View File

@ -0,0 +1,14 @@
---
type: yml
auth:
sign_key: jwt_secret
http_server:
port: 8080
mysql:
port: 3308
host: localhost
db_name: niki_db
username: niki
password: nikit0lk2o20

View File

@ -2,7 +2,7 @@ package adminkindboxhandler
import (
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/user"
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
)

View File

@ -2,7 +2,7 @@ package adminkindboxreqhandler
import (
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/user"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
)

View File

@ -1,7 +1,7 @@
package benefactorkindboxhandler
import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/user"
benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box"
benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box"
)

View File

@ -1,7 +1,7 @@
package benefactorkindboxreqhandler
import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/user"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
)

View File

@ -18,13 +18,3 @@ services:
- MYSQL_DATABASE=niki
#set mysql in ubuntu
#//https://www.tecmint.com/fix-mysql-error-1819-hy000/
#
# sudo mysql_secure_installation
# sudo systemctl start mysql
# mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'g#Y!298mKwz85';
# mysql> create database orginfo;
# mysql> grant all privileges on orginfo.* to user@localhost;
# mysql> set password for user@localhost = password("g#Y!298mKwz85");

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package adminparam

View File

@ -1 +0,0 @@
package superadminparam

View File

@ -1 +0,0 @@
package superadminparam

View File

@ -1 +0,0 @@
package superadminparam

View File

@ -1 +0,0 @@
package superadminparam

View File

@ -1 +0,0 @@
package superadminparam

View File

@ -1,7 +0,0 @@
package benefactorparam
type BenefactorProfileRequest struct{}
type BenefactorProfileResponse struct {
Benefactor entity.Benefactor
}

View File

@ -1,7 +0,0 @@
package benefactorparam
type BenefactorLoginRequest struct{}
type BenefactorLoginResponse struct {
Benefactor entity.Benefactor
}

View File

@ -1,9 +0,0 @@
package benefactorparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
type BenefactorRegisterRequest struct{}
type BenefactorRegisterResponse struct {
Benefactor entity.Benefactor
}

View File

@ -1 +0,0 @@
package benefactorparam

View File

@ -1 +0,0 @@
package staffparam

View File

@ -1 +0,0 @@
package staffparam

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package adminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -1 +0,0 @@
package superadminservice

View File

@ -0,0 +1 @@
package admin

View File

@ -1,4 +1,4 @@
package authservice
package admin
import (
"time"

View File

@ -0,0 +1 @@
package user

View File

@ -0,0 +1 @@
package user

View File

@ -0,0 +1,23 @@
package user
import (
"time"
)
type Config struct {
SignKey string `koanf:"sign_key"`
AccessExpirationTime time.Duration `koanf:"access_expiration_time"`
RefreshExpirationTime time.Duration `koanf:"refresh_expiration_time"`
AccessSubject string `koanf:"access_subject"`
RefreshSubject string `koanf:"refresh_subject"`
}
type Service struct {
config Config
}
func New(cfg Config) Service {
return Service{
config: cfg,
}
}

View File

@ -1 +0,0 @@
package benefactorservice

View File

@ -1,45 +0,0 @@
package benefactorservice
import (
"fmt"
"gameapp/param"
"gameapp/pkg/richerror"
)
func (s Service) Login(req param.LoginRequest) (param.LoginResponse, error) {
const op = "benefactorservice.Login"
// TODO - it would be better to user two separate method for existence check and getUserByPhoneNumber
user, err := s.repo.GetUserByPhoneNumber(req.PhoneNumber)
if err != nil {
return param.LoginResponse{}, richerror.New(op).WithErr(err).
WithMeta(map[string]interface{}{"phone_number": req.PhoneNumber})
}
if user.Password != getMD5Hash(req.Password) {
return param.LoginResponse{}, fmt.Errorf("username or password isn't correct")
}
accessToken, err := s.auth.CreateAccessToken(user)
if err != nil {
return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err)
}
refreshToken, err := s.auth.CreateRefreshToken(user)
if err != nil {
return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err)
}
return param.LoginResponse{
User: param.UserInfo{
ID: user.ID,
PhoneNumber: user.PhoneNumber,
Name: user.Name,
},
Tokens: param.Tokens{
AccessToken: accessToken,
RefreshToken: refreshToken,
},
}, nil
}

View File

@ -1,36 +0,0 @@
package benefactorservice
import (
"fmt"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
)
// rol
// login(sms)-> {rigester}() -> next
func (s Service) Register(req param.RegisterRequest) (param.RegisterResponse, error) {
// TODO - we should verify phone number by verification code
// TODO - replace md5 with bcrypt
user := entity.Benefactor{
ID: 0,
PhoneNumber: req.PhoneNumber,
Name: req.Name,
Password: getMD5Hash(req.Password),
Role: entity.BenefactorRole,
}
// create new user in storage
createdUser, err := s.repo.Register(user)
if err != nil {
return param.RegisterResponse{}, fmt.Errorf("unexpected error: %w", err)
}
// return created user
return param.RegisterResponse{User: param.UserInfo{
ID: createdUser.ID,
PhoneNumber: createdUser.Name,
Name: createdUser.PhoneNumber,
}}, nil
}

View File

@ -1,27 +0,0 @@
package benefactorservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type Repository interface {
Register(benefactor entity.Benefactor) (entity.Benefactor, error)
GetUserByPhoneNumber(phoneNumber string) (entity.Benefactor, error)
GetUserByID(ctx context.Context, userID uint) (entity.Benefactor, error)
}
type AuthGenerator interface {
CreateAccessToken(benefactor entity.Benefactor) (string, error)
CreateRefreshToken(benefactor entity.Benefactor) (string, error)
}
type Service struct {
auth AuthGenerator
repo Repository
}
func New(authGenerator AuthGenerator, repo Repository) Service {
return Service{auth: authGenerator, repo: repo}
}

View File

@ -1 +0,0 @@
package benefactorservice

View File

@ -1 +0,0 @@
package staffservice

View File

@ -1,45 +0,0 @@
package staffservice
import (
"fmt"
"gameapp/param"
"gameapp/pkg/richerror"
)
func (s Service) Login(req param.LoginRequest) (param.LoginResponse, error) {
const op = "benefactorservice.Login"
// TODO - it would be better to user two separate method for existence check and getUserByPhoneNumber
user, err := s.repo.GetUserByPhoneNumber(req.PhoneNumber)
if err != nil {
return param.LoginResponse{}, richerror.New(op).WithErr(err).
WithMeta(map[string]interface{}{"phone_number": req.PhoneNumber})
}
if user.Password != getMD5Hash(req.Password) {
return param.LoginResponse{}, fmt.Errorf("username or password isn't correct")
}
accessToken, err := s.auth.CreateAccessToken(user)
if err != nil {
return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err)
}
refreshToken, err := s.auth.CreateRefreshToken(user)
if err != nil {
return param.LoginResponse{}, fmt.Errorf("unexpected error: %w", err)
}
return param.LoginResponse{
User: param.UserInfo{
ID: user.ID,
PhoneNumber: user.PhoneNumber,
Name: user.Name,
},
Tokens: param.Tokens{
AccessToken: accessToken,
RefreshToken: refreshToken,
},
}, nil
}

View File

@ -1,27 +0,0 @@
package staffservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
)
type Repository interface {
Register(benefactor entity.Benefactor) (entity.Benefactor, error)
GetUserByPhoneNumber(phoneNumber string) (entity.Benefactor, error)
GetUserByID(ctx context.Context, userID uint) (entity.Benefactor, error)
}
type AuthGenerator interface {
CreateAccessToken(benefactor entity.Benefactor) (string, error)
CreateRefreshToken(benefactor entity.Benefactor) (string, error)
}
type Service struct {
auth AuthGenerator
repo Repository
}
func New(authGenerator AuthGenerator, repo Repository) Service {
return Service{auth: authGenerator, repo: repo}
}