package store import ( "context" "database/sql" "errors" "strings" ) // Role is the type of a role. type Role string const ( // RoleHost is the HOST role. RoleHost Role = "HOST" // RoleAdmin is the ADMIN role. RoleAdmin Role = "ADMIN" // RoleUser is the USER role. RoleUser Role = "USER" ) func (e Role) String() string { switch e { case RoleHost: return "HOST" case RoleAdmin: return "ADMIN" case RoleUser: return "USER" } return "USER" } type User struct { ID int // Standard fields RowStatus RowStatus CreatedTs int64 UpdatedTs int64 // Domain specific fields Username string Role Role Email string Nickname string PasswordHash string OpenID string AvatarURL string } type UpdateUser struct { ID int UpdatedTs *int64 RowStatus *RowStatus Username *string `json:"username"` Email *string `json:"email"` Nickname *string `json:"nickname"` Password *string `json:"password"` ResetOpenID *bool `json:"resetOpenId"` AvatarURL *string `json:"avatarUrl"` PasswordHash *string OpenID *string } type FindUser struct { ID *int RowStatus *RowStatus Username *string Role *Role Email *string Nickname *string OpenID *string } type DeleteUser struct { ID int } func (s *Store) CreateUser(ctx context.Context, create *User) (*User, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() query := ` INSERT INTO user ( username, role, email, nickname, password_hash, open_id ) VALUES (?, ?, ?, ?, ?, ?) RETURNING id, avatar_url, created_ts, updated_ts, row_status ` if err := tx.QueryRowContext(ctx, query, create.Username, create.Role, create.Email, create.Nickname, create.PasswordHash, create.OpenID, ).Scan( &create.ID, &create.AvatarURL, &create.CreatedTs, &create.UpdatedTs, &create.RowStatus, ); err != nil { return nil, err } if err := tx.Commit(); err != nil { return nil, err } user := create s.userCache.Store(user.ID, user) return user, nil } func (s *Store) UpdateUser(ctx context.Context, update *UpdateUser) (*User, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() set, args := []string{}, []any{} if v := update.UpdatedTs; v != nil { set, args = append(set, "updated_ts = ?"), append(args, *v) } if v := update.RowStatus; v != nil { set, args = append(set, "row_status = ?"), append(args, *v) } if v := update.Username; v != nil { set, args = append(set, "username = ?"), append(args, *v) } if v := update.Email; v != nil { set, args = append(set, "email = ?"), append(args, *v) } if v := update.Nickname; v != nil { set, args = append(set, "nickname = ?"), append(args, *v) } if v := update.AvatarURL; v != nil { set, args = append(set, "avatar_url = ?"), append(args, *v) } if v := update.PasswordHash; v != nil { set, args = append(set, "password_hash = ?"), append(args, *v) } if v := update.OpenID; v != nil { set, args = append(set, "open_id = ?"), append(args, *v) } args = append(args, update.ID) query := ` UPDATE user SET ` + strings.Join(set, ", ") + ` WHERE id = ? RETURNING id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status ` user := &User{} if err := tx.QueryRowContext(ctx, query, args...).Scan( &user.ID, &user.Username, &user.Role, &user.Email, &user.Nickname, &user.PasswordHash, &user.OpenID, &user.AvatarURL, &user.CreatedTs, &user.UpdatedTs, &user.RowStatus, ); err != nil { return nil, err } if err := tx.Commit(); err != nil { return nil, err } s.userCache.Store(user.ID, user) return user, nil } func (s *Store) ListUsers(ctx context.Context, find *FindUser) ([]*User, error) { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() list, err := listUsers(ctx, tx, find) if err != nil { return nil, err } if err := tx.Commit(); err != nil { return nil, err } for _, user := range list { s.userCache.Store(user.ID, user) } return list, nil } func (s *Store) GetUser(ctx context.Context, find *FindUser) (*User, error) { if find.ID != nil { if cache, ok := s.userCache.Load(*find.ID); ok { return cache.(*User), nil } } tx, err := s.db.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() list, err := listUsers(ctx, tx, find) if err != nil { return nil, err } if len(list) == 0 { return nil, nil } if err := tx.Commit(); err != nil { return nil, err } user := list[0] s.userCache.Store(user.ID, user) return user, nil } func (s *Store) DeleteUser(ctx context.Context, delete *DeleteUser) error { tx, err := s.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() result, err := tx.ExecContext(ctx, ` DELETE FROM user WHERE id = ? `, delete.ID) if err != nil { return err } rows, err := result.RowsAffected() if err != nil { return err } if rows == 0 { return errors.New("user not found") } if err := s.vacuumImpl(ctx, tx); err != nil { return err } if err := tx.Commit(); err != nil { return err } s.userCache.Delete(delete.ID) return nil } func listUsers(ctx context.Context, tx *sql.Tx, find *FindUser) ([]*User, error) { where, args := []string{"1 = 1"}, []any{} if v := find.ID; v != nil { where, args = append(where, "id = ?"), append(args, *v) } if v := find.Username; v != nil { where, args = append(where, "username = ?"), append(args, *v) } if v := find.Role; v != nil { where, args = append(where, "role = ?"), append(args, *v) } if v := find.Email; v != nil { where, args = append(where, "email = ?"), append(args, *v) } if v := find.Nickname; v != nil { where, args = append(where, "nickname = ?"), append(args, *v) } if v := find.OpenID; v != nil { where, args = append(where, "open_id = ?"), append(args, *v) } query := ` SELECT id, username, role, email, nickname, password_hash, open_id, avatar_url, created_ts, updated_ts, row_status FROM user WHERE ` + strings.Join(where, " AND ") + ` ORDER BY created_ts DESC, row_status DESC ` rows, err := tx.QueryContext(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() list := make([]*User, 0) for rows.Next() { var user User if err := rows.Scan( &user.ID, &user.Username, &user.Role, &user.Email, &user.Nickname, &user.PasswordHash, &user.OpenID, &user.AvatarURL, &user.CreatedTs, &user.UpdatedTs, &user.RowStatus, ); err != nil { return nil, err } list = append(list, &user) } if err := rows.Err(); err != nil { return nil, err } return list, nil }