@@ -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[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user