From ca85e574ab645c460489fe65cd2fdc34ab101af2 Mon Sep 17 00:00:00 2001 From: donaderoyan Date: Mon, 27 May 2024 14:08:15 +0700 Subject: [PATCH] add CoursePreferences, MusicalInformation model --- controllers/user/profile/input.go | 28 +++++++++++------- controllers/user/profile/service.go | 4 +-- models/course_preferences.go | 7 +++++ models/musical_information.go | 15 ++++++++++ models/registry.go | 2 ++ models/user.go | 45 +++++++++++++++-------------- 6 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 models/course_preferences.go create mode 100644 models/musical_information.go diff --git a/controllers/user/profile/input.go b/controllers/user/profile/input.go index 4774fad..985777e 100644 --- a/controllers/user/profile/input.go +++ b/controllers/user/profile/input.go @@ -1,15 +1,21 @@ package profile -import model "github.com/donaderoyan/talentgrowth-be/models" - type UpdateProfileInput struct { - FirstName string `json:"firstName" validate:"required,alpha" updateValidation:"omitempty,alpha"` - LastName string `json:"lastName" validate:"required,alpha" updateValidation:"omitempty,alpha"` - Phone string `json:"phone" validate:"required,e164" updateValidation:"omitempty,e164"` - Address model.Address `json:"address" validate:"omitempty" updateValidation:"omitempty"` - Birthday string `json:"birthday" validate:"omitempty,customdate,datebeforetoday" updateValidation:"omitempty,customdate,datebeforetoday"` - Gender string `json:"gender" validate:"omitempty,oneof=male female" updateValidation:"omitempty,oneof=male female"` - Nationality string `json:"nationality" validate:"omitempty" updateValidation:"omitempty"` - Bio string `json:"bio" validate:"omitempty" updateValidation:"omitempty"` - ProfilePicture string `json:"profilePicture" validate:"omitempty,url" updateValidation:"omitempty,url"` + FirstName string `json:"firstName" validate:"required,alpha" updateValidation:"omitempty,alpha"` + LastName string `json:"lastName" validate:"required,alpha" updateValidation:"omitempty,alpha"` + Phone string `json:"phone" validate:"required,e164" updateValidation:"omitempty,e164"` + Address Address `json:"address" validate:"omitempty" updateValidation:"omitempty"` + Birthday string `json:"birthday" validate:"omitempty,customdate,datebeforetoday" updateValidation:"omitempty,customdate,datebeforetoday"` + Gender string `json:"gender" validate:"omitempty,oneof=male female" updateValidation:"omitempty,oneof=male female"` + Nationality string `json:"nationality" validate:"omitempty" updateValidation:"omitempty"` + Bio string `json:"bio" validate:"omitempty" updateValidation:"omitempty"` + ProfilePicture string `json:"profilePicture" validate:"omitempty,url" updateValidation:"omitempty,url"` +} + +type Address struct { + Street string `json:"street,omitempty" validate:"omitempty" updateValidation:"omitempty"` + City string `json:"city" validate:"required" updateValidation:"omitempty"` + State string `json:"state" validate:"required" updateValidation:"omitempty"` + PostalCode string `json:"postalCode" validate:"required" updateValidation:"omitempty"` + Country string `json:"country" validate:"required" updateValidation:"omitempty"` } diff --git a/controllers/user/profile/service.go b/controllers/user/profile/service.go index 2d9c738..beeddaa 100644 --- a/controllers/user/profile/service.go +++ b/controllers/user/profile/service.go @@ -36,7 +36,7 @@ func (s *service) UpdateProfileService(userID string, input *UpdateProfileInput) } // Handle address update if present - if input.Address != (model.Address{}) { + if input.Address != (Address{}) { correctFieldNames["address.street"] = input.Address.Street correctFieldNames["address.city"] = input.Address.City correctFieldNames["address.state"] = input.Address.State @@ -48,7 +48,7 @@ func (s *service) UpdateProfileService(userID string, input *UpdateProfileInput) for key, value := range correctFieldNames { if value == nil || (reflect.TypeOf(value).Kind() == reflect.String && value == "") { delete(correctFieldNames, key) - } else if val, ok := value.(model.Address); ok && val == (model.Address{}) { + } else if val, ok := value.(Address); ok && val == (Address{}) { delete(correctFieldNames, key) } } diff --git a/models/course_preferences.go b/models/course_preferences.go new file mode 100644 index 0000000..b3bcc02 --- /dev/null +++ b/models/course_preferences.go @@ -0,0 +1,7 @@ +package model + +type CoursePreferences struct { + PreferredLearningMode string `bson:"preferred_learning_mode" json:"preferred_learning_mode"` + Availability []string `bson:"availability" json:"availability"` + PreferredInstructors []string `bson:"preferred_instructors,omitempty" json:"preferred_instructors,omitempty"` // this may change when Instructor model is implemented +} diff --git a/models/musical_information.go b/models/musical_information.go new file mode 100644 index 0000000..7614184 --- /dev/null +++ b/models/musical_information.go @@ -0,0 +1,15 @@ +package model + +import "go.mongodb.org/mongo-driver/bson/primitive" + +// MusicalInformation struct for user's musical details +type MusicalInformation struct { + ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` + UserID primitive.ObjectID `bson:"user_id" json:"user_id"` // Reference to the User + SkillLevel string `bson:"skill_level" json:"skill_level"` + PrimaryInstrument string `bson:"primary_instrument" json:"primary_instrument"` + SecondaryInstruments []string `bson:"secondary_instruments,omitempty" json:"secondary_instruments,omitempty"` + Genres []string `bson:"genres" json:"genres"` + FavoriteArtists []string `bson:"favorite_artists,omitempty" json:"favorite_artists,omitempty"` + LearningGoals []string `bson:"learning_goals,omitempty" json:"learning_goals,omitempty"` +} diff --git a/models/registry.go b/models/registry.go index 070e12b..a1e56cd 100644 --- a/models/registry.go +++ b/models/registry.go @@ -7,6 +7,8 @@ type Model struct { func RegisterModels() []string { return []string{ "User", + "MusicalInformation", + "CoursePreferences", // "Product", etc ... } } diff --git a/models/user.go b/models/user.go index b7d195a..c17561a 100644 --- a/models/user.go +++ b/models/user.go @@ -7,34 +7,35 @@ import ( ) type User struct { - ID primitive.ObjectID `bson:"_id,omitempty"` - FirstName string `bson:"firstName" validate:"alpha"` - LastName string `bson:"lastName" validate:"alpha"` - Email string `bson:"email" validate:"required,email"` - Password string `bson:"password" validate:"required"` - RememberToken string `bson:"rememberToken,omitempty"` - Phone string `bson:"phone" validate:"e164"` - Birthday time.Time `bson:"birthday,omitempty" validate:"omitempty,datetime"` - Gender string `bson:"gender,omitempty" validate:"omitempty,oneof=male female other"` - Nationality string `bson:"nationality,omitempty"` - Bio string `bson:"bio,omitempty"` - ProfilePicture string `bson:"profilePicture,omitempty" validate:"omitempty,url"` + ID primitive.ObjectID `bson:"_id,omitempty" json:"id"` + FirstName string `bson:"firstName" json:"firstName"` + LastName string `bson:"lastName" json:"lastName"` + Email string `bson:"email" json:"email"` + Password string `bson:"password" json:"password"` + RememberToken string `bson:"rememberToken,omitempty" json:"rememberToken,omitempty"` + Phone string `bson:"phone" json:"phone"` + Birthday time.Time `bson:"birthday,omitempty" json:"birthday,omitempty"` + Gender string `bson:"gender,omitempty" json:"gender,omitempty"` + Nationality string `bson:"nationality,omitempty" json:"nationality,omitempty"` + Bio string `bson:"bio,omitempty" json:"bio,omitempty"` + ProfilePicture string `bson:"profilePicture,omitempty" json:"profilePicture,omitempty"` + Address Address `bson:"address,omitempty" json:"address,omitempty"` - // Using 'dive' in validation to apply validation rules on each field of the struct - Address Address `bson:"address,omitempty" validate:"omitempty"` + MusicalInfoID primitive.ObjectID `bson:"musical_info_id,omitempty" json:"musical_info_id,omitempty"` // Reference to MusicalInformation + CoursePrefs CoursePreferences `bson:"course_preferences" json:"course_preferences"` // Using embedded approach for now, will be change when Instructor model is implemented - CreatedAt time.Time `bson:"createdAt"` - UpdatedAt time.Time `bson:"updatedAt"` - DeletedAt *time.Time `bson:"deletedAt,omitempty"` + CreatedAt time.Time `bson:"createdAt" json:"createdAt"` + UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` + DeletedAt *time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` // RoleID primitive.ObjectID `bson:"roleId" validate:"required"` // currently not used } type Address struct { - Street string `bson:"street,omitempty" validate:"omitempty"` - City string `bson:"city" validate:"required"` - State string `bson:"state" validate:"required"` - PostalCode string `bson:"postalCode" validate:"required"` - Country string `bson:"country" validate:"required"` + Street string `bson:"street,omitempty" json:"street,omitempty"` + City string `bson:"city" json:"city"` + State string `bson:"state" json:"state"` + PostalCode string `bson:"postalCode" json:"postalCode"` + Country string `bson:"country" json:"country"` } // SoftDelete sets the DeletedAt field to the current time to mark an entry as deleted.