databag/net/server/internal/api_setProfile.go

56 lines
1.2 KiB
Go
Raw Normal View History

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) {
account, res := BearerAppToken(r, true);
if res != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
if account.Disabled {
w.WriteHeader(http.StatusGone);
return
}
detail := account.AccountDetail
// extract profile data from body
var profileData ProfileData;
err := ParseRequest(r, w, &profileData)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
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 {
PrintMsg(err)
LogMsg("failed to store profile")
2022-01-19 08:03:46 +00:00
w.WriteHeader(http.StatusInternalServerError)
return
}
2022-01-19 19:00:20 +00:00
SetStatus(account)
2022-01-19 08:03:46 +00:00
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}