2646bc2db8
Showing how we can remove services, preventing cyclic imports and lessing the number of imports. Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package. In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user. However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool). By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
33 lignes
Pas d'EOL
792 o
Go
33 lignes
Pas d'EOL
792 o
Go
package users
|
|
|
|
// UpdateUserCore updates a user. (Applying the modifed data of user).
|
|
func Update(user *model.User) (int, error) {
|
|
if user.Email == "" {
|
|
user.MD5 = ""
|
|
} else {
|
|
var err error
|
|
user.MD5, err = crypto.GenerateMD5Hash(user.Email)
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
}
|
|
|
|
user.UpdatedAt = time.Now()
|
|
err := db.ORM.Save(user).Error
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
}
|
|
|
|
// UpdateRawUser : Function to update a user without updating his associations model
|
|
func UpdateRawUser(user *model.User) (int, error) {
|
|
user.UpdatedAt = time.Now()
|
|
err := db.ORM.Model(&user).UpdateColumn(&user).Error
|
|
if err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
} |