2022-01-25 05:22:33 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AddGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
account, code, err := BearerAppToken(r, false)
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var subject Subject
|
|
|
|
if err := ParseRequest(r, w, &subject); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-03 07:48:17 +00:00
|
|
|
slot := &store.GroupSlot{}
|
2022-01-25 05:22:33 +00:00
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-02-01 18:03:04 +00:00
|
|
|
|
2022-02-02 18:55:45 +00:00
|
|
|
data := &store.GroupData{
|
|
|
|
Data: subject.Data,
|
2022-03-10 06:18:37 +00:00
|
|
|
AccountID: account.ID,
|
2022-02-02 18:55:45 +00:00
|
|
|
}
|
|
|
|
if res := tx.Save(data).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:19:41 +00:00
|
|
|
group := &store.Group{}
|
2022-03-10 06:18:37 +00:00
|
|
|
group.AccountID = account.ID
|
2022-02-03 07:48:17 +00:00
|
|
|
group.GroupDataID = data.ID
|
|
|
|
group.DataType = subject.DataType
|
2022-01-28 21:39:31 +00:00
|
|
|
if res := tx.Save(group).Error; res != nil {
|
2022-01-25 05:22:33 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-02-01 18:03:04 +00:00
|
|
|
|
2022-02-13 23:19:41 +00:00
|
|
|
slot.GroupSlotId = uuid.New().String()
|
|
|
|
slot.AccountID = account.ID
|
2022-02-03 07:48:17 +00:00
|
|
|
slot.GroupID = group.ID
|
|
|
|
slot.Revision = account.GroupRevision + 1
|
|
|
|
slot.Group = group
|
|
|
|
if res := tx.Save(slot).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-01-28 21:39:31 +00:00
|
|
|
if res := tx.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
|
2022-01-25 05:22:33 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatus(account)
|
2022-02-03 07:48:17 +00:00
|
|
|
WriteResponse(w, getGroupModel(slot))
|
2022-01-25 05:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|