add cache for groups and clients, http sending message remains to be add
This commit is contained in:
+44
-4
@@ -22,13 +22,13 @@ func InitDatabase(ctx context.Context) {
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(60) NOT NULL,
|
||||
color VARCHAR(3) DEFAULT NULL,
|
||||
color VARCHAR(3) DEFAULT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(48) NOT NULL,
|
||||
creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE SET NULL,
|
||||
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE SET NULL,
|
||||
creator_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
owner_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
enable_user_colors BOOLEAN NOT NULL DEFAULT true,
|
||||
group_color VARCHAR(3),
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW()
|
||||
@@ -43,7 +43,9 @@ func InitDatabase(ctx context.Context) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
dbConnection = conn
|
||||
InitCache()
|
||||
}
|
||||
|
||||
func AddNewUser(ctx context.Context, user *User) (uint32, error) {
|
||||
@@ -65,7 +67,7 @@ func AddNewUser(ctx context.Context, user *User) (uint32, error) {
|
||||
}
|
||||
user.Password = string(hashed)
|
||||
}
|
||||
if len(user.Color) != 1 && len(user.Color) != 3 {
|
||||
if user.Color == ([3]byte{}) {
|
||||
return 0, errors.New("color invalid")
|
||||
}
|
||||
err = dbConnection.QueryRow(ctx, `
|
||||
@@ -86,6 +88,24 @@ func isPassValid(ctx context.Context, id uint32, plainPassword string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(controlHash), []byte(plainPassword)) == nil
|
||||
}
|
||||
|
||||
func GetAllUsers(ctx context.Context) ([]User, error) {
|
||||
rows, err := dbConnection.Query(ctx, "SELECT id, name, color FROM users")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var users []User
|
||||
for rows.Next() {
|
||||
var user User
|
||||
if err := rows.Scan(&user.Id, &user.Name, &user.Color); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
return users, rows.Err()
|
||||
}
|
||||
|
||||
func GetUserDataById(ctx context.Context, id uint32) (User, error) {
|
||||
var user User
|
||||
err := dbConnection.QueryRow(ctx, "SELECT id, name, pass_hash, color FROM users WHERE id = $1", id).
|
||||
@@ -120,9 +140,29 @@ func CreateChatGroupWithoutMembers(ctx context.Context, group *ChatGroup) (uint3
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id
|
||||
`, group.Name, group.CreatorId, group.OwnerId, group.CreatedAt).Scan(&id)
|
||||
|
||||
AddOrUpdateGroupToCache(&mu, *group)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func GetAllChatGroups(ctx context.Context) ([]ChatGroup, error) {
|
||||
rows, err := dbConnection.Query(ctx, "SELECT id, name, creator_id, owner_id, enable_user_colors, group_color, created_at FROM chat_groups")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var groups []ChatGroup
|
||||
for rows.Next() {
|
||||
var group ChatGroup
|
||||
if err := rows.Scan(&group.Id, &group.Name, &group.CreatorId, &group.OwnerId, &group.EnableUserColors, &group.Color, &group.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups = append(groups, group)
|
||||
}
|
||||
return groups, rows.Err()
|
||||
}
|
||||
|
||||
func GetChatGroupWithoutMembers(ctx context.Context, id uint32) (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`,
|
||||
|
||||
Reference in New Issue
Block a user