diff --git a/migrations/20230725191302-add-names-to-users.sql b/migrations/20230725191302-add-names-to-users.sql new file mode 100644 index 0000000..6e572ef --- /dev/null +++ b/migrations/20230725191302-add-names-to-users.sql @@ -0,0 +1,9 @@ +-- +migrate Up + +ALTER TABLE users + ADD COLUMN full_name TEXT NOT NULL DEFAULT ''; + +-- +migrate Down + +ALTER TABLE users + DROP COLUMN full_name; diff --git a/model/users.go b/model/users.go index bc0440b..0beae99 100644 --- a/model/users.go +++ b/model/users.go @@ -8,6 +8,7 @@ import ( type User struct { gorm.Model + FullName string // FIXME: Use nullable string Username string LoginProvider userenums.LoginProvider } diff --git a/params/users/create.go b/params/users/create.go index bea2072..3dd9a4d 100644 --- a/params/users/create.go +++ b/params/users/create.go @@ -9,6 +9,7 @@ import ( ) type Create struct { + Name string `json:"name"` Username string `json:"username"` LoginProvider string `json:"provider"` } @@ -37,6 +38,7 @@ func (params *Create) ToModel() *model.User { panic(errors.New("Illegal path - invalid provider")) } return &model.User{ + FullName: params.Name, Username: params.Username, LoginProvider: provider, } diff --git a/view/users/view.go b/view/users/view.go index bf40725..6a52711 100644 --- a/view/users/view.go +++ b/view/users/view.go @@ -4,13 +4,19 @@ import "github.com/source-academy/stories-backend/model" type View struct { ID uint `json:"id"` + Name string `json:"name"` Username string `json:"username"` LoginProvider string `json:"provider"` } func SingleFrom(user model.User) View { + name := user.FullName + if name == "" { + name = user.Username + } userView := View{ ID: user.ID, + Name: name, Username: user.Username, LoginProvider: user.LoginProvider.ToString(), }