databag/net/server/internal/api_removeCard.go

87 lines
2.3 KiB
Go
Raw Normal View History

2022-02-11 23:32:53 +00:00
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func RemoveCard(w http.ResponseWriter, r *http.Request) {
account, code, err := BearerAppToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return
}
// scan parameters
params := mux.Vars(r)
cardId := params["cardId"]
// load referenced card
var slot store.CardSlot
if err := store.DB.Preload("Card.Groups").Preload("Card.Channels.Cards").Preload("Card.Channels.ChannelSlot").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
2022-02-11 23:32:53 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if slot.Card == nil {
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
return
}
// cards to update
cards := make(map[string]*store.Card)
2022-02-11 23:32:53 +00:00
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-12 08:08:30 +00:00
for _, channel := range slot.Card.Channels {
if res := tx.Model(&channel).Association("Cards").Delete(&slot.Card); res != nil {
return res
}
if res := tx.Model(&channel.ChannelSlot).Update("revision", account.ChannelRevision + 1).Error; res != nil {
return res
}
for _, card := range channel.Cards {
cards[card.Guid] = &card
}
2022-02-12 08:08:30 +00:00
}
2022-02-11 23:32:53 +00:00
if res := tx.Model(&slot.Card).Association("Groups").Clear(); res != nil {
return res
}
if res := tx.Delete(&slot.Card).Error; res != nil {
return res
}
slot.Card = nil
if res := tx.Model(&slot).Update("card_id", 0).Error; res != nil {
return res
}
if res := tx.Model(&slot).Update("revision", account.CardRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
return res
}
2022-02-12 08:08:30 +00:00
if res := tx.Model(&account).Update("channel_revision", account.ChannelRevision + 1).Error; res != nil {
return res
}
2022-02-11 23:32:53 +00:00
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
for _, card := range cards {
SetContactChannelNotification(account, card)
}
2022-02-11 23:32:53 +00:00
SetStatus(account)
WriteResponse(w, nil);
}