mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
testing card deletion
This commit is contained in:
parent
71dc276c93
commit
3b85a6e023
@ -1,20 +0,0 @@
|
|||||||
/*
|
|
||||||
* DataBag
|
|
||||||
*
|
|
||||||
* DataBag provides storage for decentralized identity based self-hosting apps. It is intended to support sharing of personal data and hosting group conversations.
|
|
||||||
*
|
|
||||||
* API version: 0.0.1
|
|
||||||
* Contact: roland.osborne@gmail.com
|
|
||||||
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
|
|
||||||
*/
|
|
||||||
package databag
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RemoveCard(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
67
net/server/internal/api_removeCard.go
Normal file
67
net/server/internal/api_removeCard.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
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").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// save and update contact revision
|
||||||
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
// TODO clear from channels
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
SetStatus(account)
|
||||||
|
WriteResponse(w, nil);
|
||||||
|
}
|
||||||
|
|
@ -58,7 +58,7 @@ func TestContactSync(t *testing.T) {
|
|||||||
assert.NoError(t, ApiTestMsg(GetCards, "GET", "/contact/cards?revision=" + cardRevision,
|
assert.NoError(t, ApiTestMsg(GetCards, "GET", "/contact/cards?revision=" + cardRevision,
|
||||||
nil, nil, APP_TOKENAPP, set.B.Token, cards, &hdr))
|
nil, nil, APP_TOKENAPP, set.B.Token, cards, &hdr))
|
||||||
cardRevision = hdr["Card-Revision"][0]
|
cardRevision = hdr["Card-Revision"][0]
|
||||||
assert.Equal(t, 0, len(*cards))
|
assert.Equal(t, 0, len(*cards)) // ?? actual 1
|
||||||
|
|
||||||
// set card notes
|
// set card notes
|
||||||
GetTestRevision(set.B.Revisions)
|
GetTestRevision(set.B.Revisions)
|
||||||
@ -135,4 +135,12 @@ func TestContactSync(t *testing.T) {
|
|||||||
¶m, nil, APP_TOKENAPP, set.D.Token, card, nil))
|
¶m, nil, APP_TOKENAPP, set.D.Token, card, nil))
|
||||||
assert.Nil(t, card.Data)
|
assert.Nil(t, card.Data)
|
||||||
|
|
||||||
|
// delete card
|
||||||
|
param["cardId"] = set.A.C.CardId
|
||||||
|
assert.NoError(t, ApiTestMsg(RemoveCard, "DELETE", "/contact/cards/{cardId}",
|
||||||
|
¶m, nil, APP_TOKENAPP, set.A.Token, nil, nil))
|
||||||
|
card = &Card{}
|
||||||
|
assert.NoError(t, ApiTestMsg(GetCard, "GET", "/contact/cards/{cardId}",
|
||||||
|
¶m, nil, APP_TOKENAPP, set.A.Token, card, nil))
|
||||||
|
assert.Nil(t, card.Data)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user