feat(backend): always return empty arrays instead of null

This commit is contained in:
Sam 2023-03-12 01:31:31 +01:00
parent b8a7e7443d
commit 0e72097346
No known key found for this signature in database
GPG Key ID: B4EF20DDE721CAA1
6 changed files with 61 additions and 35 deletions

View File

@ -95,10 +95,15 @@ func (s *Server) discordCallback(w http.ResponseWriter, r *http.Request) error {
return errors.Wrap(err, "saving token to database")
}
fields, err := s.DB.UserFields(ctx, u.ID)
if err != nil {
return errors.Wrap(err, "querying fields")
}
render.JSON(w, r, discordCallbackResponse{
HasAccount: true,
Token: token,
User: dbUserToUserResponse(u),
User: dbUserToUserResponse(u, fields),
})
return nil
@ -230,7 +235,7 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error {
// return user
render.JSON(w, r, signupResponse{
User: *dbUserToUserResponse(u),
User: *dbUserToUserResponse(u, nil),
Token: token,
})
return nil

View File

@ -26,19 +26,25 @@ type userResponse struct {
Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
Fields []db.Field `json:"fields"`
Discord *string `json:"discord"`
DiscordUsername *string `json:"discord_username"`
}
func dbUserToUserResponse(u db.User) *userResponse {
func dbUserToUserResponse(u db.User, fields []db.Field) *userResponse {
return &userResponse{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
Bio: u.Bio,
AvatarURLs: u.AvatarURLs,
Links: u.Links,
AvatarURLs: db.NotNull(u.AvatarURLs),
Links: db.NotNull(u.Links),
Names: db.NotNull(u.Names),
Pronouns: db.NotNull(u.Pronouns),
Fields: db.NotNull(fields),
Discord: u.Discord,
DiscordUsername: u.DiscordUsername,
}

View File

@ -32,18 +32,18 @@ func dbMemberToMember(u db.User, m db.Member, fields []db.Field) GetMemberRespon
Name: m.Name,
DisplayName: m.DisplayName,
Bio: m.Bio,
AvatarURLs: m.AvatarURLs,
Links: m.Links,
AvatarURLs: db.NotNull(m.AvatarURLs),
Links: db.NotNull(m.Links),
Names: m.Names,
Pronouns: m.Pronouns,
Fields: fields,
Names: db.NotNull(m.Names),
Pronouns: db.NotNull(m.Pronouns),
Fields: db.NotNull(fields),
User: PartialUser{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
AvatarURLs: u.AvatarURLs,
AvatarURLs: db.NotNull(u.AvatarURLs),
},
}
}

View File

@ -17,6 +17,8 @@ type memberListResponse struct {
Bio *string `json:"bio"`
AvatarURLs []string `json:"avatar_urls"`
Links []string `json:"links"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
}
func membersToMemberList(ms []db.Member) []memberListResponse {
@ -26,8 +28,10 @@ func membersToMemberList(ms []db.Member) []memberListResponse {
ID: ms[i].ID,
Name: ms[i].Name,
Bio: ms[i].Bio,
AvatarURLs: ms[i].AvatarURLs,
Links: ms[i].Links,
AvatarURLs: db.NotNull(ms[i].AvatarURLs),
Links: db.NotNull(ms[i].Links),
Names: db.NotNull(ms[i].Names),
Pronouns: db.NotNull(ms[i].Pronouns),
}
}

View File

@ -68,6 +68,13 @@ func (s *Server) patchMember(w http.ResponseWriter, r *http.Request) error {
}
}
if req.Name != nil && *req.Name == "" {
return server.APIError{
Code: server.ErrBadRequest,
Details: "Name must not be empty",
}
}
// validate display name/bio
if req.Name != nil && len(*req.Name) > db.MaxMemberNameLength {
return server.APIError{

View File

@ -36,6 +36,8 @@ type PartialMember struct {
Name string `json:"name"`
DisplayName *string `json:"display_name"`
AvatarURLs []string `json:"avatar_urls"`
Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"`
}
func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUserResponse {
@ -44,11 +46,11 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser
Username: u.Username,
DisplayName: u.DisplayName,
Bio: u.Bio,
AvatarURLs: u.AvatarURLs,
Links: u.Links,
Names: u.Names,
Pronouns: u.Pronouns,
Fields: fields,
AvatarURLs: db.NotNull(u.AvatarURLs),
Links: db.NotNull(u.Links),
Names: db.NotNull(u.Names),
Pronouns: db.NotNull(u.Pronouns),
Fields: db.NotNull(fields),
}
resp.Members = make([]PartialMember, len(members))
@ -57,7 +59,9 @@ func dbUserToResponse(u db.User, fields []db.Field, members []db.Member) GetUser
ID: members[i].ID,
Name: members[i].Name,
DisplayName: members[i].DisplayName,
AvatarURLs: members[i].AvatarURLs,
AvatarURLs: db.NotNull(members[i].AvatarURLs),
Names: db.NotNull(members[i].Names),
Pronouns: db.NotNull(members[i].Pronouns),
}
}