using Guid as foreign key for App and Card

This commit is contained in:
Roland Osborne 2022-01-22 10:16:33 -08:00
parent bc0921c721
commit bbdc4ed722
10 changed files with 17 additions and 16 deletions

View File

@ -38,7 +38,7 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
// populate new record // populate new record
card.CardId = uuid.New().String() card.CardId = uuid.New().String()
card.AccountID = account.ID card.AccountID = account.Guid
card.Guid = guid card.Guid = guid
card.Username = identity.Handle card.Username = identity.Handle
card.Name = identity.Name card.Name = identity.Name

View File

@ -18,7 +18,7 @@ func GetCard(w http.ResponseWriter, r *http.Request) {
cardId := mux.Vars(r)["cardId"] cardId := mux.Vars(r)["cardId"]
var card store.Card var card store.Card
if err := store.DB.Where("account_id = ? AND card_id = ?", account.ID, cardId).First(&card).Error; err != nil { if err := store.DB.Where("account_id = ? AND card_id = ?", account.Guid, cardId).First(&card).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err) ErrResponse(w, http.StatusNotFound, err)
} else { } else {

View File

@ -22,7 +22,7 @@ func GetCardView(w http.ResponseWriter, r *http.Request) {
} }
var views []CardView var views []CardView
if err := store.DB.Model(&store.Card{}).Where("account_id = ?", account.ID).Find(&views).Error; err != nil { if err := store.DB.Model(&store.Card{}).Where("account_id = ?", account.Guid).Find(&views).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
} }

View File

@ -19,7 +19,7 @@ func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
cardId := mux.Vars(r)["cardId"] cardId := mux.Vars(r)["cardId"]
var card store.Card var card store.Card
if err := store.DB.Where("account_id = ? AND card_id = ?", account.ID, cardId).First(&card).Error; err != nil { if err := store.DB.Where("account_id = ? AND card_id = ?", account.Guid, cardId).First(&card).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) { if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
} else { } else {

View File

@ -36,7 +36,7 @@ func SetAccountApp(w http.ResponseWriter, r *http.Request) {
// create app entry // create app entry
app := store.App { app := store.App {
AccountID: token.AccountID, AccountID: token.Account.Guid,
Name: appData.Name, Name: appData.Name,
Description: appData.Description, Description: appData.Description,
Image: appData.Image, Image: appData.Image,

View File

@ -38,7 +38,7 @@ func SetCardStatus(w http.ResponseWriter, r *http.Request) {
// load referenced card // load referenced card
var card store.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 err := store.DB.Preload("Groups").Where("account_id = ? AND card_id = ?", account.Guid, cardId).First(&card).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) { if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
} else { } else {

View File

@ -41,7 +41,7 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
// see if card exists // see if card exists
var card store.Card var card store.Card
if err := store.DB.Where("account_id = ? AND guid = ?", account.ID, guid).First(&card).Error; err != nil { if err := store.DB.Where("account_id = ? AND guid = ?", account.Guid, guid).First(&card).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) { if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err) ErrResponse(w, http.StatusInternalServerError, err)
return return
@ -49,7 +49,7 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
// populate new record // populate new record
card.CardId = uuid.New().String() card.CardId = uuid.New().String()
card.AccountID = account.ID card.AccountID = account.Guid
card.Guid = guid card.Guid = guid
card.Username = connect.Handle card.Username = connect.Handle
card.Name = connect.Name card.Name = connect.Name

View File

@ -13,6 +13,7 @@ import (
type accountLogin struct { type accountLogin struct {
ID uint ID uint
Guid string
Password []byte Password []byte
} }
@ -73,7 +74,7 @@ func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
// find token record // find token record
var accountToken store.AccountToken var accountToken store.AccountToken
if err := store.DB.Where("token = ?", token).First(&accountToken).Error; err != nil { if err := store.DB.Preload("Account").Where("token = ?", token).First(&accountToken).Error; err != nil {
return accountToken, err return accountToken, err
} }
if accountToken.Expires < time.Now().Unix() { if accountToken.Expires < time.Now().Unix() {

View File

@ -78,7 +78,7 @@ func SetProfileNotification(account *store.Account) {
// select all connected cards // select all connected cards
var cards []store.Card var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ?", account.ID, APP_CARDCONNECTED).Find(&cards).Error; err != nil { if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
ErrMsg(err) ErrMsg(err)
return return
} }
@ -111,7 +111,7 @@ func SetContentNotification(account *store.Account) {
// select all connected cards // select all connected cards
var cards []store.Card var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ?", account.ID, APP_CARDCONNECTED).Find(&cards).Error; err != nil { if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
ErrMsg(err) ErrMsg(err)
return return
} }
@ -143,7 +143,7 @@ func SetContactContentNotification(account *store.Account, cardId string) {
// select card if connected // select card if connected
var cards []store.Card var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ? AND card_id = ?", account.ID, APP_CARDCONNECTED, cardId).Find(&cards).Error; err != nil { if err := store.DB.Where("account_id = ? AND status = ? AND card_id = ?", account.Guid, APP_CARDCONNECTED, cardId).Find(&cards).Error; err != nil {
ErrMsg(err) ErrMsg(err)
return return
} }

View File

@ -87,14 +87,14 @@ type AccountDetail struct {
type App struct { type App struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
AccountID uint `gorm:"index"` AccountID string `gorm:"index"`
Name string Name string
Description string Description string
Image string Image string
Url string Url string
Token string `gorm:"not null;index"` Token string `gorm:"not null;index"`
Created int64 `gorm:"autoCreateTime"` Created int64 `gorm:"autoCreateTime"`
Account Account Account Account `gorm:"references:Guid"`
} }
type Group struct { type Group struct {
@ -124,7 +124,7 @@ type Label struct {
type Card struct { type Card struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
CardId string `gorm:"not null;index:card,unique"` CardId string `gorm:"not null;index:card,unique"`
AccountID uint `gorm:"not null;index:card,unique;index:guid,unqiue"` AccountID string `gorm:"not null;index:card,unique;index:guid,unqiue"`
Guid string `gorm:"not null;index:guid,unique"` Guid string `gorm:"not null;index:guid,unique"`
Username string Username string
Name string Name string
@ -144,7 +144,7 @@ type Card struct {
ViewRevision int64 `gorm:"not null"` ViewRevision int64 `gorm:"not null"`
RemoteProfile int64 RemoteProfile int64
RemoteContent int64 RemoteContent int64
Account Account Account Account `gorm:"references:Guid"`
Groups []Group `gorm:"many2many:card_groups;"` Groups []Group `gorm:"many2many:card_groups;"`
} }