feat(api): add display_name to member

This commit is contained in:
Sam 2022-11-20 21:09:29 +01:00
parent c4ddde73ec
commit 73c5c9fc67
7 changed files with 61 additions and 39 deletions

View File

@ -16,6 +16,7 @@ type Member struct {
ID xid.ID
UserID xid.ID
Name string
DisplayName *string
Bio *string
AvatarURLs []string `db:"avatar_urls"`
Links []string
@ -79,10 +80,10 @@ func (db *DB) UserMembers(ctx context.Context, userID xid.ID) (ms []Member, err
}
// CreateMember creates a member.
func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name, bio string, links []string) (m Member, err error) {
func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name string, displayName *string, bio string, links []string) (m Member, err error) {
sql, args, err := sq.Insert("members").
Columns("user_id", "id", "name", "bio", "links").
Values(userID, xid.New(), name, bio, links).
Columns("user_id", "id", "name", "display_name", "bio", "links").
Values(userID, xid.New(), name, displayName, bio, links).
Suffix("RETURNING *").ToSql()
if err != nil {
return m, errors.Wrap(err, "building sql")

View File

@ -13,6 +13,7 @@ import (
type CreateMemberRequest struct {
Name string `json:"name"`
DisplayName *string `json:"display_name"`
Bio string `json:"bio"`
Avatar string `json:"avatar"`
Links []string `json:"links"`
@ -54,7 +55,12 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error
if cmr.Name == "" {
return server.APIError{
Code: server.ErrBadRequest,
Details: "name may not be empty",
Details: "Name may not be empty",
}
} else if len(cmr.Name) > 100 {
return server.APIError{
Code: server.ErrBadRequest,
Details: "Name may not be longer than 100 characters",
}
}
@ -76,25 +82,29 @@ func (s *Server) createMember(w http.ResponseWriter, r *http.Request) (err error
}
defer tx.Rollback(ctx)
m, err := s.DB.CreateMember(ctx, tx, claims.UserID, cmr.Name, cmr.Bio, cmr.Links)
m, err := s.DB.CreateMember(ctx, tx, claims.UserID, cmr.Name, cmr.DisplayName, cmr.Bio, cmr.Links)
if err != nil {
if errors.Cause(err) == db.ErrMemberNameInUse {
return server.APIError{Code: server.ErrMemberNameInUse}
}
return err
}
// set names, pronouns, fields
err = s.DB.SetMemberNames(ctx, tx, claims.UserID, cmr.Names)
err = s.DB.SetMemberNames(ctx, tx, m.ID, cmr.Names)
if err != nil {
log.Errorf("setting names for user %v: %v", claims.UserID, err)
log.Errorf("setting names for member %v: %v", m.ID, err)
return err
}
err = s.DB.SetMemberPronouns(ctx, tx, claims.UserID, cmr.Pronouns)
err = s.DB.SetMemberPronouns(ctx, tx, m.ID, cmr.Pronouns)
if err != nil {
log.Errorf("setting pronouns for user %v: %v", claims.UserID, err)
log.Errorf("setting pronouns for member %v: %v", m.ID, err)
return err
}
err = s.DB.SetMemberFields(ctx, tx, claims.UserID, cmr.Fields)
err = s.DB.SetMemberFields(ctx, tx, m.ID, cmr.Fields)
if err != nil {
log.Errorf("setting fields for user %v: %v", claims.UserID, err)
log.Errorf("setting fields for member %v: %v", m.ID, err)
return err
}

View File

@ -14,6 +14,7 @@ import (
type GetMemberResponse struct {
ID xid.ID `json:"id"`
Name string `json:"name"`
DisplayName *string `json:"display_name"`
Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"`
@ -29,6 +30,7 @@ func dbMemberToMember(u db.User, m db.Member, names []db.Name, pronouns []db.Pro
return GetMemberResponse{
ID: m.ID,
Name: m.Name,
DisplayName: m.DisplayName,
Bio: m.Bio,
AvatarURLs: m.AvatarURLs,
Links: m.Links,

View File

@ -13,6 +13,7 @@ import (
type memberListResponse struct {
ID xid.ID `json:"id"`
Name string `json:"name"`
DisplayName *string `json:"display_name"`
Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"`

View File

@ -90,6 +90,7 @@ const (
// Member-related error codes
ErrMemberNotFound = 3001
ErrMemberLimitReached = 3002
ErrMemberNameInUse = 3003
// General request error codes
ErrRequestTooBig = 4001
@ -118,6 +119,7 @@ var errCodeMessages = map[int]string{
ErrMemberNotFound: "Member not found",
ErrMemberLimitReached: "Member limit reached",
ErrMemberNameInUse: "Member name already in use",
ErrRequestTooBig: "Request too big (max 2 MB)",
}
@ -145,6 +147,7 @@ var errCodeStatuses = map[int]int{
ErrMemberNotFound: http.StatusNotFound,
ErrMemberLimitReached: http.StatusBadRequest,
ErrMemberNameInUse: http.StatusBadRequest,
ErrRequestTooBig: http.StatusBadRequest,
}

View File

@ -19,6 +19,7 @@ export interface User {
export interface PartialMember {
id: string;
name: string;
display_name: string | null;
avatar_urls: string[] | null;
}

View File

@ -0,0 +1,4 @@
-- +migrate Up
-- 2022-11-20: add display name to members
alter table members add column display_name text;