fix db
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-11-21 00:47:44 +07:00
parent 0d6bd456d6
commit f4523de5a4
12 changed files with 152 additions and 71 deletions
+14 -1
View File
@@ -2,6 +2,8 @@ package user
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
@@ -13,6 +15,7 @@ import (
type UserEntity struct {
Id int
Username string
Password string
}
type UsernameAlreadyExistsErr struct{}
@@ -34,9 +37,10 @@ func NewUserService(
}
func (s *UserService) AddUser(ctx context.Context, user *UserEntity) (*UserEntity, error) {
query := `INSERT INTO users (username) VALUES (@username) RETURNING id`
query := `INSERT INTO users (username, password) VALUES (@username, @password) RETURNING id`
args := pgx.NamedArgs{
"username": user.Username,
"password": hashPassword(user.Username, user.Password, "crab"),
}
if err := s.db.QueryRow(ctx, query, args).Scan(&user.Id); err != nil {
var pgErr *pgconn.PgError
@@ -49,3 +53,12 @@ func (s *UserService) AddUser(ctx context.Context, user *UserEntity) (*UserEntit
}
return user, nil
}
func hashPassword(username string, password string, salt string) string {
return getMD5Hash(username + password + salt)
}
func getMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}