modify geolocation storage type in database
This commit is contained in:
+16
-20
@@ -1,7 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -573,7 +572,7 @@ func (a *TransactionsApi) TransactionGetHandler(c *core.Context) (interface{}, *
|
||||
|
||||
transactionEditable := transaction.IsEditable(user, utcOffset, accountMap[transaction.AccountId], accountMap[transaction.RelatedAccountId])
|
||||
transactionTagIds := allTransactionTagIds[transaction.TransactionId]
|
||||
transactionResp := transaction.ToTransactionInfoResponse(c, transactionTagIds, transactionEditable)
|
||||
transactionResp := transaction.ToTransactionInfoResponse(transactionTagIds, transactionEditable)
|
||||
|
||||
if !transactionGetReq.TrimAccount {
|
||||
if sourceAccount := accountMap[transaction.AccountId]; sourceAccount != nil {
|
||||
@@ -665,7 +664,7 @@ func (a *TransactionsApi) TransactionCreateHandler(c *core.Context) (interface{}
|
||||
|
||||
log.InfofWithRequestId(c, "[transactions.TransactionCreateHandler] user \"uid:%d\" has created a new transaction \"id:%d\" successfully", uid, transaction.TransactionId)
|
||||
|
||||
transactionResp := transaction.ToTransactionInfoResponse(c, tagIds, transactionEditable)
|
||||
transactionResp := transaction.ToTransactionInfoResponse(tagIds, transactionEditable)
|
||||
|
||||
return transactionResp, nil
|
||||
}
|
||||
@@ -723,12 +722,6 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
|
||||
transactionTagIds = make([]int64, 0, 0)
|
||||
}
|
||||
|
||||
var geoLocation []byte
|
||||
|
||||
if transactionModifyReq.GeoLocation != nil {
|
||||
geoLocation, _ = json.Marshal(transactionModifyReq.GeoLocation)
|
||||
}
|
||||
|
||||
newTransaction := &models.Transaction{
|
||||
TransactionId: transaction.TransactionId,
|
||||
Uid: uid,
|
||||
@@ -739,7 +732,6 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
|
||||
Amount: transactionModifyReq.SourceAmount,
|
||||
HideAmount: transactionModifyReq.HideAmount,
|
||||
Comment: transactionModifyReq.Comment,
|
||||
GeoLocation: string(geoLocation),
|
||||
}
|
||||
|
||||
if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_OUT {
|
||||
@@ -747,6 +739,11 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
|
||||
newTransaction.RelatedAccountAmount = transactionModifyReq.DestinationAmount
|
||||
}
|
||||
|
||||
if transactionModifyReq.GeoLocation != nil {
|
||||
newTransaction.GeoLongitude = transactionModifyReq.GeoLocation.Longitude
|
||||
newTransaction.GeoLatitude = transactionModifyReq.GeoLocation.Latitude
|
||||
}
|
||||
|
||||
if newTransaction.CategoryId == transaction.CategoryId &&
|
||||
utils.GetUnixTimeFromTransactionTime(newTransaction.TransactionTime) == utils.GetUnixTimeFromTransactionTime(transaction.TransactionTime) &&
|
||||
newTransaction.TimezoneUtcOffset == transaction.TimezoneUtcOffset &&
|
||||
@@ -756,7 +753,8 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
|
||||
(transaction.Type != models.TRANSACTION_DB_TYPE_TRANSFER_OUT || newTransaction.RelatedAccountAmount == transaction.RelatedAccountAmount) &&
|
||||
newTransaction.HideAmount == transaction.HideAmount &&
|
||||
newTransaction.Comment == transaction.Comment &&
|
||||
newTransaction.GeoLocation == transaction.GeoLocation &&
|
||||
newTransaction.GeoLongitude == transaction.GeoLongitude &&
|
||||
newTransaction.GeoLatitude == transaction.GeoLatitude &&
|
||||
utils.Int64SliceEquals(tagIds, transactionTagIds) {
|
||||
return nil, errs.ErrNothingWillBeUpdated
|
||||
}
|
||||
@@ -786,7 +784,7 @@ func (a *TransactionsApi) TransactionModifyHandler(c *core.Context) (interface{}
|
||||
log.InfofWithRequestId(c, "[transactions.TransactionModifyHandler] user \"uid:%d\" has updated transaction \"id:%d\" successfully", uid, transactionModifyReq.Id)
|
||||
|
||||
newTransaction.Type = transaction.Type
|
||||
newTransactionResp := newTransaction.ToTransactionInfoResponse(c, tagIds, transactionEditable)
|
||||
newTransactionResp := newTransaction.ToTransactionInfoResponse(tagIds, transactionEditable)
|
||||
|
||||
return newTransactionResp, nil
|
||||
}
|
||||
@@ -1013,7 +1011,7 @@ func (a *TransactionsApi) getTransactionListResult(c *core.Context, user *models
|
||||
|
||||
transactionEditable := transaction.IsEditable(user, utcOffset, allAccounts[transaction.AccountId], allAccounts[transaction.RelatedAccountId])
|
||||
transactionTagIds := allTransactionTagIds[transaction.TransactionId]
|
||||
result[i] = transaction.ToTransactionInfoResponse(c, transactionTagIds, transactionEditable)
|
||||
result[i] = transaction.ToTransactionInfoResponse(transactionTagIds, transactionEditable)
|
||||
|
||||
if !trimAccount {
|
||||
if sourceAccount := allAccounts[transaction.AccountId]; sourceAccount != nil {
|
||||
@@ -1054,12 +1052,6 @@ func (a *TransactionsApi) createNewTransactionModel(uid int64, transactionCreate
|
||||
transactionDbType = models.TRANSACTION_DB_TYPE_TRANSFER_OUT
|
||||
}
|
||||
|
||||
var geoLocation []byte
|
||||
|
||||
if transactionCreateReq.GeoLocation != nil {
|
||||
geoLocation, _ = json.Marshal(transactionCreateReq.GeoLocation)
|
||||
}
|
||||
|
||||
transaction := &models.Transaction{
|
||||
Uid: uid,
|
||||
Type: transactionDbType,
|
||||
@@ -1070,7 +1062,6 @@ func (a *TransactionsApi) createNewTransactionModel(uid int64, transactionCreate
|
||||
Amount: transactionCreateReq.SourceAmount,
|
||||
HideAmount: transactionCreateReq.HideAmount,
|
||||
Comment: transactionCreateReq.Comment,
|
||||
GeoLocation: string(geoLocation),
|
||||
CreatedIp: clientIp,
|
||||
}
|
||||
|
||||
@@ -1079,5 +1070,10 @@ func (a *TransactionsApi) createNewTransactionModel(uid int64, transactionCreate
|
||||
transaction.RelatedAccountAmount = transactionCreateReq.DestinationAmount
|
||||
}
|
||||
|
||||
if transactionCreateReq.GeoLocation != nil {
|
||||
transaction.GeoLongitude = transactionCreateReq.GeoLocation.Longitude
|
||||
transaction.GeoLatitude = transactionCreateReq.GeoLocation.Latitude
|
||||
}
|
||||
|
||||
return transaction
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -38,8 +35,8 @@ const (
|
||||
// Transaction represents transaction data stored in database
|
||||
type Transaction struct {
|
||||
TransactionId int64 `xorm:"PK"`
|
||||
Uid int64 `xorm:"UNIQUE(UQE_transaction_uid_time) INDEX(IDX_transaction_uid_deleted_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) INDEX(IDX_transaction_uid_deleted_account_id_time) NOT NULL"`
|
||||
Deleted bool `xorm:"INDEX(IDX_transaction_uid_deleted_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) INDEX(IDX_transaction_uid_deleted_account_id_time) NOT NULL"`
|
||||
Uid int64 `xorm:"UNIQUE(UQE_transaction_uid_time) INDEX(IDX_transaction_uid_deleted_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) INDEX(IDX_transaction_uid_deleted_account_id_time) INDEX(IDX_transaction_uid_deleted_time_longitude_latitude) NOT NULL"`
|
||||
Deleted bool `xorm:"INDEX(IDX_transaction_uid_deleted_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) INDEX(IDX_transaction_uid_deleted_account_id_time) INDEX(IDX_transaction_uid_deleted_time_longitude_latitude) NOT NULL"`
|
||||
Type TransactionDbType `xorm:"INDEX(IDX_transaction_uid_deleted_type_time) NOT NULL"`
|
||||
CategoryId int64 `xorm:"INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"`
|
||||
AccountId int64 `xorm:"INDEX(IDX_transaction_uid_deleted_account_id_time) NOT NULL"`
|
||||
@@ -51,7 +48,8 @@ type Transaction struct {
|
||||
RelatedAccountAmount int64 `xorm:"NOT NULL"`
|
||||
HideAmount bool `xorm:"NOT NULL"`
|
||||
Comment string `xorm:"VARCHAR(255) NOT NULL"`
|
||||
GeoLocation string `xorm:"VARCHAR(255)"`
|
||||
GeoLongitude float64 `xorm:"INDEX(IDX_transaction_uid_deleted_time_longitude_latitude)"`
|
||||
GeoLatitude float64 `xorm:"INDEX(IDX_transaction_uid_deleted_time_longitude_latitude)"`
|
||||
CreatedIp string `xorm:"VARCHAR(39)"`
|
||||
CreatedUnixTime int64
|
||||
UpdatedUnixTime int64
|
||||
@@ -283,7 +281,7 @@ func (t *Transaction) IsEditable(currentUser *User, utcOffset int16, account *Ac
|
||||
}
|
||||
|
||||
// ToTransactionInfoResponse returns a view-object according to database model
|
||||
func (t *Transaction) ToTransactionInfoResponse(c *core.Context, tagIds []int64, editable bool) *TransactionInfoResponse {
|
||||
func (t *Transaction) ToTransactionInfoResponse(tagIds []int64, editable bool) *TransactionInfoResponse {
|
||||
var transactionType TransactionType
|
||||
|
||||
if t.Type == TRANSACTION_DB_TYPE_MODIFY_BALANCE {
|
||||
@@ -319,11 +317,9 @@ func (t *Transaction) ToTransactionInfoResponse(c *core.Context, tagIds []int64,
|
||||
|
||||
geoLocation := &TransactionGeoLocationResponse{}
|
||||
|
||||
if t.GeoLocation != "" {
|
||||
err := json.Unmarshal([]byte(t.GeoLocation), geoLocation)
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[transaction.ToTransactionInfoResponse] cannot unmarshal geo location \"%s\", because %s", t.GeoLocation, err.Error())
|
||||
}
|
||||
if t.GeoLongitude != 0 || t.GeoLatitude != 0 {
|
||||
geoLocation.Longitude = t.GeoLongitude
|
||||
geoLocation.Latitude = t.GeoLatitude
|
||||
} else {
|
||||
geoLocation = nil
|
||||
}
|
||||
|
||||
@@ -542,8 +542,12 @@ func (s *TransactionService) ModifyTransaction(transaction *models.Transaction,
|
||||
updateCols = append(updateCols, "comment")
|
||||
}
|
||||
|
||||
if transaction.GeoLocation != oldTransaction.GeoLocation {
|
||||
updateCols = append(updateCols, "geo_location")
|
||||
if transaction.GeoLongitude != oldTransaction.GeoLongitude {
|
||||
updateCols = append(updateCols, "geo_longitude")
|
||||
}
|
||||
|
||||
if transaction.GeoLatitude != oldTransaction.GeoLatitude {
|
||||
updateCols = append(updateCols, "geo_latitude")
|
||||
}
|
||||
|
||||
// Get and verify tags
|
||||
@@ -959,7 +963,8 @@ func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *
|
||||
RelatedAccountId: originalTransaction.AccountId,
|
||||
RelatedAccountAmount: originalTransaction.Amount,
|
||||
Comment: originalTransaction.Comment,
|
||||
GeoLocation: originalTransaction.GeoLocation,
|
||||
GeoLongitude: originalTransaction.GeoLongitude,
|
||||
GeoLatitude: originalTransaction.GeoLatitude,
|
||||
CreatedIp: originalTransaction.CreatedIp,
|
||||
CreatedUnixTime: originalTransaction.CreatedUnixTime,
|
||||
UpdatedUnixTime: originalTransaction.UpdatedUnixTime,
|
||||
|
||||
Reference in New Issue
Block a user