2022-01-19 08:03:46 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-01-19 19:00:20 +00:00
|
|
|
"gorm.io/gorm"
|
2022-01-19 08:03:46 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetProfile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-01-20 23:19:26 +00:00
|
|
|
account, code, err := BearerAppToken(r, true);
|
2022-01-19 19:36:53 +00:00
|
|
|
if err != nil {
|
2022-01-20 23:19:26 +00:00
|
|
|
ErrResponse(w, code, err)
|
2022-01-19 08:03:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
detail := account.AccountDetail
|
|
|
|
|
|
|
|
// extract profile data from body
|
|
|
|
var profileData ProfileData;
|
2022-01-19 19:36:53 +00:00
|
|
|
err = ParseRequest(r, w, &profileData)
|
2022-01-19 08:03:46 +00:00
|
|
|
if err != nil {
|
2022-01-19 19:36:53 +00:00
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
2022-01-19 08:03:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// update record
|
|
|
|
detail.Name = profileData.Name
|
|
|
|
detail.Location = profileData.Location
|
|
|
|
detail.Description = profileData.Description
|
2022-01-19 19:00:20 +00:00
|
|
|
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
if res := store.DB.Save(&detail).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := store.DB.Model(&account).Update("profile_revision", account.ProfileRevision + 1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-01-19 19:36:53 +00:00
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
2022-01-19 08:03:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:00:20 +00:00
|
|
|
SetStatus(account)
|
2022-01-20 23:19:26 +00:00
|
|
|
WriteResponse(w, nil)
|
2022-01-19 08:03:46 +00:00
|
|
|
}
|
|
|
|
|