mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
added card not support
This commit is contained in:
parent
dc055fa9af
commit
8cdf70a4cd
63
net/server/internal/api_clearCardNotes.go
Normal file
63
net/server/internal/api_clearCardNotes.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package databag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"databag/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ClearCardNotes(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.GroupSlot").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
|
||||||
|
}
|
||||||
|
|
||||||
|
// update card
|
||||||
|
slot.Revision = account.CardRevision + 1
|
||||||
|
slot.Card.Notes = ""
|
||||||
|
|
||||||
|
// save and update contact revision
|
||||||
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if res := tx.Save(&slot.Card).Error; res != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
if res := tx.Preload("Card").Save(&slot).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, getCardModel(&slot));
|
||||||
|
}
|
||||||
|
|
@ -18,11 +18,6 @@ func ClearCardGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ClearCardNotes(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetCardData(w http.ResponseWriter, r *http.Request) {
|
func GetCardData(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
@ -48,11 +43,6 @@ func RemoveCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetCardNotes(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
func SetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
69
net/server/internal/api_setCardNotes.go
Normal file
69
net/server/internal/api_setCardNotes.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package databag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"databag/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetCardNotes(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"]
|
||||||
|
|
||||||
|
var notes string
|
||||||
|
if err := ParseRequest(r, w, ¬es); err != nil {
|
||||||
|
ErrResponse(w, http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// load referenced card
|
||||||
|
var slot store.CardSlot
|
||||||
|
if err := store.DB.Preload("Card.Groups.GroupSlot").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
|
||||||
|
}
|
||||||
|
|
||||||
|
// update card
|
||||||
|
slot.Revision = account.CardRevision + 1
|
||||||
|
slot.Card.Notes = notes
|
||||||
|
|
||||||
|
// save and update contact revision
|
||||||
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if res := tx.Save(&slot.Card).Error; res != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
if res := tx.Preload("Card").Save(&slot).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, getCardModel(&slot));
|
||||||
|
}
|
||||||
|
|
@ -20,7 +20,8 @@ func getCardModel(slot *store.CardSlot) *Card {
|
|||||||
|
|
||||||
return &Card{
|
return &Card{
|
||||||
CardId: slot.CardSlotId,
|
CardId: slot.CardSlotId,
|
||||||
CardData: &CardData {
|
Revision: slot.Revision,
|
||||||
|
CardData: &CardData {
|
||||||
NotifiedProfile: slot.Card.NotifiedProfile,
|
NotifiedProfile: slot.Card.NotifiedProfile,
|
||||||
NotifiedContent: slot.Card.NotifiedContent,
|
NotifiedContent: slot.Card.NotifiedContent,
|
||||||
NotifiedLabel: slot.Card.NotifiedLabel,
|
NotifiedLabel: slot.Card.NotifiedLabel,
|
||||||
@ -53,6 +54,7 @@ func getGroupModel(slot *store.GroupSlot) *Group {
|
|||||||
|
|
||||||
return &Group{
|
return &Group{
|
||||||
GroupId: slot.GroupSlotId,
|
GroupId: slot.GroupSlotId,
|
||||||
|
Revision: slot.Revision,
|
||||||
GroupData: &GroupData {
|
GroupData: &GroupData {
|
||||||
DataType: slot.Group.DataType,
|
DataType: slot.Group.DataType,
|
||||||
Data: slot.Group.GroupData.Data,
|
Data: slot.Group.GroupData.Data,
|
||||||
@ -67,6 +69,7 @@ func getArticleModel(slot *store.ArticleSlot, contact bool, shared bool) *Articl
|
|||||||
if !shared || slot.Article == nil {
|
if !shared || slot.Article == nil {
|
||||||
return &Article{
|
return &Article{
|
||||||
ArticleId: slot.ArticleSlotId,
|
ArticleId: slot.ArticleSlotId,
|
||||||
|
Revision: slot.Revision,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +87,7 @@ func getArticleModel(slot *store.ArticleSlot, contact bool, shared bool) *Articl
|
|||||||
|
|
||||||
return &Article{
|
return &Article{
|
||||||
ArticleId: slot.ArticleSlotId,
|
ArticleId: slot.ArticleSlotId,
|
||||||
|
Revision: slot.Revision,
|
||||||
ArticleData: &ArticleData{
|
ArticleData: &ArticleData{
|
||||||
DataType: slot.Article.DataType,
|
DataType: slot.Article.DataType,
|
||||||
Data: slot.Article.Data,
|
Data: slot.Article.Data,
|
||||||
|
@ -45,6 +45,7 @@ type Subject struct {
|
|||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
ArticleId string `json:"article_id"`
|
ArticleId string `json:"article_id"`
|
||||||
|
Revision int64 `json:"revision"`
|
||||||
ArticleData *ArticleData `json:"articleData"`
|
ArticleData *ArticleData `json:"articleData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,12 +84,12 @@ type Asset struct {
|
|||||||
|
|
||||||
type Card struct {
|
type Card struct {
|
||||||
CardId string `json:"cardId"`
|
CardId string `json:"cardId"`
|
||||||
|
Revision int64 `json:"revision"`
|
||||||
CardData *CardData `json:"cardData"`
|
CardData *CardData `json:"cardData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CardData struct {
|
type CardData struct {
|
||||||
CardProfile *CardProfile `json:"cardProfile"`
|
CardProfile *CardProfile `json:"cardProfile"`
|
||||||
CardData *CardData `json:"cardData"`
|
|
||||||
NotifiedProfile int64 `json:"notifiedProfile"`
|
NotifiedProfile int64 `json:"notifiedProfile"`
|
||||||
NotifiedContent int64 `json:"notifiedContent"`
|
NotifiedContent int64 `json:"notifiedContent"`
|
||||||
NotifiedLabel int64 `json:"notifiedLabel"`
|
NotifiedLabel int64 `json:"notifiedLabel"`
|
||||||
@ -144,6 +145,7 @@ type DialogueInsights struct {
|
|||||||
|
|
||||||
type Group struct {
|
type Group struct {
|
||||||
GroupId string `json:"groupId"`
|
GroupId string `json:"groupId"`
|
||||||
|
Revision int64 `json:"revision"`
|
||||||
GroupData *GroupData `json:"groupData"`
|
GroupData *GroupData `json:"groupData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +170,7 @@ type Insight struct {
|
|||||||
|
|
||||||
type Label struct {
|
type Label struct {
|
||||||
LabelId string `json:"labelId"`
|
LabelId string `json:"labelId"`
|
||||||
|
Revision int64 `json:"revision"`
|
||||||
LabelData *LabelData `json:"labelData"`
|
LabelData *LabelData `json:"labelData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ func TestAddContact(t *testing.T) {
|
|||||||
var r *Revision
|
var r *Revision
|
||||||
var msg DataMessage
|
var msg DataMessage
|
||||||
var cards []Card
|
var cards []Card
|
||||||
var card Card
|
var card *Card
|
||||||
|
|
||||||
// setup testing group
|
// setup testing group
|
||||||
set, err = AddTestGroup("addaccount")
|
set, err = AddTestGroup("addaccount")
|
||||||
@ -38,21 +38,37 @@ func TestAddContact(t *testing.T) {
|
|||||||
r = GetTestRevision(set.B.Revisions)
|
r = GetTestRevision(set.B.Revisions)
|
||||||
assert.NotEqual(t, rev.Card, r.Card)
|
assert.NotEqual(t, rev.Card, r.Card)
|
||||||
|
|
||||||
assert.NoError(t, SendEndpointTest(GetCards, "PUT", "/contact/cards?cardRevision=" + strconv.FormatInt(rev.Card, 10), nil, nil, APP_TOKENAPP, set.B.Token, &cards))
|
assert.NoError(t, SendEndpointTest(GetCards, "GET", "/contact/cards?cardRevision=" + strconv.FormatInt(rev.Card, 10), nil, nil, APP_TOKENAPP, set.B.Token, &cards))
|
||||||
assert.Equal(t, 1, len(cards))
|
assert.Equal(t, 1, len(cards))
|
||||||
assert.Equal(t, set.A.Guid, cards[0].CardData.CardProfile.Guid)
|
assert.Equal(t, set.A.Guid, cards[0].CardData.CardProfile.Guid)
|
||||||
assert.NotEqual(t, "Namer", cards[0].CardData.CardProfile.Name)
|
assert.NotEqual(t, "Namer", cards[0].CardData.CardProfile.Name)
|
||||||
rev = r
|
rev = r
|
||||||
|
|
||||||
|
card = &Card{}
|
||||||
assert.NoError(t, SendEndpointTest(GetProfileMessage, "GET", "/profile/message", nil, nil, APP_TOKENCONTACT, set.B.A.Token, &msg))
|
assert.NoError(t, SendEndpointTest(GetProfileMessage, "GET", "/profile/message", nil, nil, APP_TOKENCONTACT, set.B.A.Token, &msg))
|
||||||
assert.NoError(t, SendEndpointTest(SetCardProfile, "PUT", "/contact/cards/{cardId}/profile", &map[string]string{"cardId":cards[0].CardId}, msg, APP_TOKENAPP, set.B.Token, &card))
|
assert.NoError(t, SendEndpointTest(SetCardProfile, "PUT", "/contact/cards/{cardId}/profile", &map[string]string{"cardId":cards[0].CardId}, msg, APP_TOKENAPP, set.B.Token, &card))
|
||||||
|
|
||||||
r = GetTestRevision(set.B.Revisions)
|
r = GetTestRevision(set.B.Revisions)
|
||||||
assert.NotEqual(t, rev.Card, r.Card)
|
assert.NotEqual(t, rev.Card, r.Card)
|
||||||
|
|
||||||
assert.NoError(t, SendEndpointTest(GetCards, "PUT", "/contact/cards?cardRevision=" + strconv.FormatInt(rev.Card, 10), nil, nil, APP_TOKENAPP, set.B.Token, &cards))
|
assert.NoError(t, SendEndpointTest(GetCards, "GET", "/contact/cards?cardRevision=" + strconv.FormatInt(rev.Card, 10), nil, nil, APP_TOKENAPP, set.B.Token, &cards))
|
||||||
assert.Equal(t, 1, len(cards))
|
assert.Equal(t, 1, len(cards))
|
||||||
assert.Equal(t, set.A.Guid, cards[0].CardData.CardProfile.Guid)
|
assert.Equal(t, set.A.Guid, cards[0].CardData.CardProfile.Guid)
|
||||||
assert.Equal(t, "Namer", cards[0].CardData.CardProfile.Name)
|
assert.Equal(t, "Namer", cards[0].CardData.CardProfile.Name)
|
||||||
rev = r
|
rev = r
|
||||||
|
|
||||||
|
card = &Card{}
|
||||||
|
assert.NoError(t, SendEndpointTest(SetCardNotes, "PUT", "/contact/cards/{cardId}/notes", &map[string]string{"cardId":cards[0].CardId}, "some interesting notes", APP_TOKENAPP, set.B.Token, &card))
|
||||||
|
assert.Equal(t, "some interesting notes", card.CardData.Notes)
|
||||||
|
r = GetTestRevision(set.B.Revisions)
|
||||||
|
assert.NotEqual(t, rev.Card, r.Card)
|
||||||
|
rev = r
|
||||||
|
|
||||||
|
card = &Card{}
|
||||||
|
assert.NoError(t, SendEndpointTest(ClearCardNotes, "DELETE", "/contact/cards/{cardId}/notes", &map[string]string{"cardId":cards[0].CardId}, nil, APP_TOKENAPP, set.B.Token, &card))
|
||||||
|
assert.Equal(t, "", card.CardData.Notes)
|
||||||
|
r = GetTestRevision(set.B.Revisions)
|
||||||
|
assert.NotEqual(t, rev.Card, r.Card)
|
||||||
|
rev = r
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user