mirror of
https://github.com/balzack/databag.git
synced 2025-03-13 00:50:03 +00:00
updating card status in test
This commit is contained in:
parent
7eb7cb8f61
commit
787e46ecc9
@ -77,36 +77,3 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
|
||||
WriteResponse(w, getCardModel(&card))
|
||||
}
|
||||
|
||||
func getCardModel(card *store.Card) *Card {
|
||||
|
||||
// populate group id list
|
||||
var groups []string;
|
||||
for _, group := range card.Groups {
|
||||
groups = append(groups, group.GroupId)
|
||||
}
|
||||
|
||||
return &Card{
|
||||
CardId: card.CardId,
|
||||
ProfileRevision: card.RemoteProfile,
|
||||
ContentRevision: card.RemoteContent,
|
||||
CardProfile: &CardProfile{
|
||||
Guid: card.Guid,
|
||||
Handle: card.Username,
|
||||
Name: card.Name,
|
||||
Description: card.Description,
|
||||
Location: card.Location,
|
||||
Revision: card.ProfileRevision,
|
||||
ImageSet: card.Image != "",
|
||||
Version: card.Version,
|
||||
Node: card.Node,
|
||||
},
|
||||
CardData: &CardData {
|
||||
Revision: card.DataRevision,
|
||||
Status: card.Status,
|
||||
Notes: card.Notes,
|
||||
Groups: groups,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -78,11 +78,6 @@ func SetCardProfile(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func SetCardStatus(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) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
55
net/server/internal/api_setCardStatus.go
Normal file
55
net/server/internal/api_setCardStatus.go
Normal file
@ -0,0 +1,55 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"gorm.io/gorm"
|
||||
"github.com/gorilla/mux"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func SetCardStatus(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"]
|
||||
token := r.FormValue("token")
|
||||
var status string
|
||||
if err := ParseRequest(r, w, &status); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
if !AppCardStatus(status) {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
// load referenced card
|
||||
var card store.Card
|
||||
if err := store.DB.Preload("Groups").Where("account_id = ? AND card_id = ?", account.ID, cardId).First(&card).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
// update card
|
||||
card.Status = status
|
||||
if token != "" {
|
||||
card.Token = token
|
||||
}
|
||||
if err := store.DB.Save(&card).Error; err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(w, getCardModel(&card));
|
||||
}
|
||||
|
@ -19,3 +19,21 @@ const APP_CARDREQUESTED = "requested"
|
||||
const APP_CARDCONNECTING = "connecting"
|
||||
const APP_CARDCONNECTED = "connected"
|
||||
|
||||
func AppCardStatus(status string) bool {
|
||||
if status == APP_CARDPENDING {
|
||||
return true
|
||||
}
|
||||
if status == APP_CARDCONFIRMED {
|
||||
return true
|
||||
}
|
||||
if status == APP_CARDREQUESTED {
|
||||
return true
|
||||
}
|
||||
if status == APP_CARDCONNECTING {
|
||||
return true
|
||||
}
|
||||
if status == APP_CARDCONNECTED {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
38
net/server/internal/cardUtil.go
Normal file
38
net/server/internal/cardUtil.go
Normal file
@ -0,0 +1,38 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func getCardModel(card *store.Card) *Card {
|
||||
|
||||
// populate group id list
|
||||
var groups []string;
|
||||
for _, group := range card.Groups {
|
||||
groups = append(groups, group.GroupId)
|
||||
}
|
||||
|
||||
return &Card{
|
||||
CardId: card.CardId,
|
||||
ProfileRevision: card.RemoteProfile,
|
||||
ContentRevision: card.RemoteContent,
|
||||
CardProfile: &CardProfile{
|
||||
Guid: card.Guid,
|
||||
Handle: card.Username,
|
||||
Name: card.Name,
|
||||
Description: card.Description,
|
||||
Location: card.Location,
|
||||
Revision: card.ProfileRevision,
|
||||
ImageSet: card.Image != "",
|
||||
Version: card.Version,
|
||||
Node: card.Node,
|
||||
},
|
||||
CardData: &CardData {
|
||||
Revision: card.DataRevision,
|
||||
Status: card.Status,
|
||||
Notes: card.Notes,
|
||||
Groups: groups,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ func WriteResponse(w http.ResponseWriter, v interface{}) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
w.Write(body);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ func NewRouter() *mux.Router {
|
||||
}
|
||||
|
||||
fs := http.FileServer(http.Dir("./"))
|
||||
router.PathPrefix("/").Handler(http.StripPrefix("/", fs))
|
||||
router.PathPrefix("/static").Handler(http.StripPrefix("/", fs))
|
||||
|
||||
return router
|
||||
}
|
||||
|
@ -2,10 +2,12 @@ package databag
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestConnectContact(t *testing.T) {
|
||||
var card Card
|
||||
|
||||
// create some contacts for this test
|
||||
access := AddTestContacts(t, "connect", 2)
|
||||
@ -21,7 +23,14 @@ func TestConnectContact(t *testing.T) {
|
||||
r, w, _ = NewRequest("POST", "/contact/cards", &msg)
|
||||
SetBearerAuth(r, access[1])
|
||||
AddCard(w, r)
|
||||
var card Card
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
// update status to connecting
|
||||
r, w, _ = NewRequest("PUT", "/contact/cards/{cardId}/status", APP_CARDCONNECTING)
|
||||
vars := map[string]string{ "cardId": card.CardId }
|
||||
r = mux.SetURLVars(r, vars)
|
||||
SetBearerAuth(r, access[1])
|
||||
SetCardStatus(w, r)
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
PrintMsg(card)
|
||||
|
Loading…
Reference in New Issue
Block a user