before rewrite
This commit is contained in:
+33
-10
@@ -48,8 +48,8 @@ func InitDatabase(ctx context.Context) {
|
||||
InitCache()
|
||||
}
|
||||
|
||||
func AddNewUser(ctx context.Context, user *User) (uint32, error) {
|
||||
var id uint32
|
||||
func AddNewUser(ctx context.Context, user *User) (uint64, error) {
|
||||
var id uint64
|
||||
var err error
|
||||
|
||||
if len(user.Name) == 0 || len(user.Name) > 20 {
|
||||
@@ -68,17 +68,18 @@ func AddNewUser(ctx context.Context, user *User) (uint32, error) {
|
||||
user.Password = string(hashed)
|
||||
}
|
||||
if user.Color == ([3]byte{}) {
|
||||
return 0, errors.New("color invalid")
|
||||
user.Color = [3]byte{'x', 'x', 'x'}
|
||||
}
|
||||
c := string(user.Color[:])
|
||||
err = dbConnection.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, color)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id
|
||||
`, user.Name, user.Password, user.Color).Scan(&id)
|
||||
`, user.Name, user.Password, c).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func isPassValid(ctx context.Context, id uint32, plainPassword string) bool {
|
||||
func isPassValid(ctx context.Context, id uint64, plainPassword string) bool {
|
||||
var controlHash string
|
||||
err := dbConnection.QueryRow(ctx, "SELECT pass_hash FROM users WHERE id = $1", id).Scan(&controlHash)
|
||||
if err != nil {
|
||||
@@ -106,7 +107,7 @@ func GetAllUsers(ctx context.Context) ([]User, error) {
|
||||
return users, rows.Err()
|
||||
}
|
||||
|
||||
func GetUserDataById(ctx context.Context, id uint32) (User, error) {
|
||||
func GetUserDataById(ctx context.Context, id uint64) (User, error) {
|
||||
var user User
|
||||
err := dbConnection.QueryRow(ctx, "SELECT id, name, pass_hash, color FROM users WHERE id = $1", id).
|
||||
Scan(&user.Id, &user.Name, &user.Password, &user.Color)
|
||||
@@ -127,7 +128,7 @@ func GetUserDataByName(ctx context.Context, name string) (User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func CreateChatGroupWithoutMembers(ctx context.Context, group *ChatGroup) (uint32, error) {
|
||||
func CreateChatGroupWithoutMembers(ctx context.Context, group *ChatGroup) (uint64, error) {
|
||||
if len(group.Name) < 1 {
|
||||
return 0, errors.New("group name too short")
|
||||
}
|
||||
@@ -135,12 +136,16 @@ func CreateChatGroupWithoutMembers(ctx context.Context, group *ChatGroup) (uint3
|
||||
return 0, errors.New("group name too long")
|
||||
}
|
||||
|
||||
var id uint32
|
||||
var id uint64
|
||||
err := dbConnection.QueryRow(ctx, `INSERT INTO chat_groups (name, creator_id, owner_id, created_at )
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id
|
||||
`, group.Name, group.CreatorId, group.OwnerId, group.CreatedAt).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
AddOrUpdateGroupToCache(&mu, *group)
|
||||
return id, err
|
||||
}
|
||||
@@ -163,14 +168,32 @@ func GetAllChatGroups(ctx context.Context) ([]ChatGroup, error) {
|
||||
return groups, rows.Err()
|
||||
}
|
||||
|
||||
func GetChatGroupWithoutMembers(ctx context.Context, id uint32) (ChatGroup, error) {
|
||||
func GetChatGroupWithoutMembers(ctx context.Context, id uint64) (ChatGroup, error) {
|
||||
var group ChatGroup
|
||||
err := dbConnection.QueryRow(ctx, `SELECT name, creator_id, owner_id, enable_user_colors, group_color, created_at FROM chat_groups WHERE id = $1`,
|
||||
id).Scan(&group.Name, &group.CreatorId, &group.OwnerId, &group.EnableUserColors, &group.Color, &group.CreatedAt)
|
||||
return group, err
|
||||
}
|
||||
|
||||
func GetChatGroupMembers(ctx context.Context, groupId uint32) ([]User, error) {
|
||||
func GetUserMemberGroupIds(ctx context.Context, userId uint64) ([]uint64, error) {
|
||||
rows, err := dbConnection.Query(ctx, "SELECT group_id FROM chat_group_members WHERE user_id = $1", userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var groupIds []uint64
|
||||
for rows.Next() {
|
||||
var groupId uint64
|
||||
if err := rows.Scan(&groupId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupIds = append(groupIds, groupId)
|
||||
}
|
||||
return groupIds, rows.Err()
|
||||
}
|
||||
|
||||
func GetChatGroupMembers(ctx context.Context, groupId uint64) ([]User, error) {
|
||||
rows, err := dbConnection.Query(ctx, `
|
||||
SELECT usr.id, usr.name, usr.color FROM users usr
|
||||
JOIN chat_group_members members ON usr.id = members.user_id
|
||||
|
||||
Reference in New Issue
Block a user