save insights explorer to database

This commit is contained in:
MaysWind
2026-01-07 01:04:54 +08:00
parent d4d1342c70
commit d462d0164c
36 changed files with 2091 additions and 286 deletions
+9
View File
@@ -31,6 +31,7 @@ type DataManagementsApi struct {
pictures *services.TransactionPictureService
templates *services.TransactionTemplateService
userCustomExchangeRates *services.UserCustomExchangeRatesService
insightsExploreres *services.InsightsExplorerService
}
// Initialize a data management api singleton instance
@@ -48,6 +49,7 @@ var (
pictures: services.TransactionPictures,
templates: services.TransactionTemplates,
userCustomExchangeRates: services.UserCustomExchangeRates,
insightsExploreres: services.InsightsExplorers,
}
)
@@ -190,6 +192,13 @@ func (a *DataManagementsApi) ClearAllDataHandler(c *core.WebContext) (any, *errs
return nil, errs.Or(err, errs.ErrOperationFailed)
}
err = a.insightsExploreres.DeleteAllInsightsExplorers(c, uid)
if err != nil {
log.Errorf(c, "[data_managements.ClearAllDataHandler] failed to delete all insights explorers, because %s", err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[data_managements.ClearAllDataHandler] user \"uid:%d\" has cleared all data", uid)
return true, nil
}
+274
View File
@@ -0,0 +1,274 @@
package api
import (
"encoding/json"
"sort"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/services"
)
// InsightsExplorersApi represents insights explorers api
type InsightsExplorersApi struct {
insightsExploreres *services.InsightsExplorerService
}
// Initialize a insights explorers api singleton instance
var (
InsightsExplorers = &InsightsExplorersApi{
insightsExploreres: services.InsightsExplorers,
}
)
// InsightsExplorerListHandler returns insights explorer list of current user
func (a *InsightsExplorersApi) InsightsExplorerListHandler(c *core.WebContext) (any, *errs.Error) {
uid := c.GetCurrentUid()
explorers, err := a.insightsExploreres.GetAllInsightsExplorerNamesByUid(c, uid)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerListHandler] failed to get insights explorers for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
explorerResps := make(models.InsightsExplorerInfoResponseSlice, len(explorers))
for i := 0; i < len(explorers); i++ {
explorerResps[i], err = explorers[i].ToInsightsExplorerInfoResponse()
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerListHandler] failed to get insights explorer response for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
}
sort.Sort(explorerResps)
return explorerResps, nil
}
// InsightsExplorerGetHandler returns one specific insights explorer of current user
func (a *InsightsExplorersApi) InsightsExplorerGetHandler(c *core.WebContext) (any, *errs.Error) {
var explorerGetReq models.InsightsExplorerGetRequest
err := c.ShouldBindQuery(&explorerGetReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerGetHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
explorer, err := a.insightsExploreres.GetInsightsExplorerByExplorerId(c, uid, explorerGetReq.Id)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerGetHandler] failed to get insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorerGetReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
explorerResp, err := explorer.ToInsightsExplorerInfoResponse()
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerGetHandler] failed to get insights explorer response for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
return explorerResp, nil
}
// InsightsExplorerCreateHandler saves a new insights explorer by request parameters for current user
func (a *InsightsExplorersApi) InsightsExplorerCreateHandler(c *core.WebContext) (any, *errs.Error) {
var explorerCreateReq models.InsightsExplorerCreateRequest
err := c.ShouldBindJSON(&explorerCreateReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerCreateHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
maxOrderId, err := a.insightsExploreres.GetMaxDisplayOrder(c, uid)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerCreateHandler] failed to get max display order for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
explorer, err := a.createNewInsightsExplorerModel(uid, &explorerCreateReq, maxOrderId+1)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerCreateHandler] failed to parse insights explorer data for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
err = a.insightsExploreres.CreateInsightsExplorer(c, explorer)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerCreateHandler] failed to create insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorer.ExplorerId, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[explorers.InsightsExplorerCreateHandler] user \"uid:%d\" has created a new insights explorer \"id:%d\" successfully", uid, explorer.ExplorerId)
explorerResp, err := explorer.ToInsightsExplorerInfoResponse()
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerCreateHandler] failed to get insights explorer response for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
return explorerResp, nil
}
// InsightsExplorerModifyHandler saves an existed insights explorer by request parameters for current user
func (a *InsightsExplorersApi) InsightsExplorerModifyHandler(c *core.WebContext) (any, *errs.Error) {
var explorerModifyReq models.InsightsExplorerModifyRequest
err := c.ShouldBindJSON(&explorerModifyReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerModifyHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
explorer, err := a.insightsExploreres.GetInsightsExplorerByExplorerId(c, uid, explorerModifyReq.Id)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerModifyHandler] failed to get insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorerModifyReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
newData, err := json.Marshal(explorerModifyReq.Data)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerModifyHandler] failed to parse insights explorer data for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
newExplorer := &models.InsightsExplorer{
ExplorerId: explorer.ExplorerId,
Uid: uid,
Name: explorerModifyReq.Name,
Data: string(newData),
}
if newExplorer.Name == explorer.Name && newExplorer.Data == explorer.Data {
return nil, errs.ErrNothingWillBeUpdated
}
err = a.insightsExploreres.ModifyInsightsExplorer(c, newExplorer)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerModifyHandler] failed to update insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorerModifyReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[explorers.InsightsExplorerModifyHandler] user \"uid:%d\" has updated insights explorer \"id:%d\" successfully", uid, explorerModifyReq.Id)
explorer.Name = newExplorer.Name
explorer.Data = newExplorer.Data
explorerResp, err := explorer.ToInsightsExplorerInfoResponse()
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerModifyHandler] failed to get insights explorer response for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.ErrInsightsExplorerDataInvalid
}
return explorerResp, nil
}
// InsightsExplorerHideHandler hides a insights explorer by request parameters for current user
func (a *InsightsExplorersApi) InsightsExplorerHideHandler(c *core.WebContext) (any, *errs.Error) {
var explorerHideReq models.InsightsExplorerHideRequest
err := c.ShouldBindJSON(&explorerHideReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerHideHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
err = a.insightsExploreres.HideInsightsExplorer(c, uid, []int64{explorerHideReq.Id}, explorerHideReq.Hidden)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerHideHandler] failed to hide insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorerHideReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[explorers.InsightsExplorerHideHandler] user \"uid:%d\" has hidden insights explorer \"id:%d\"", uid, explorerHideReq.Id)
return true, nil
}
// InsightsExplorerMoveHandler moves display order of existed insights explorers by request parameters for current user
func (a *InsightsExplorersApi) InsightsExplorerMoveHandler(c *core.WebContext) (any, *errs.Error) {
var explorerMoveReq models.InsightsExplorerMoveRequest
err := c.ShouldBindJSON(&explorerMoveReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerMoveHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
explorers := make([]*models.InsightsExplorer, len(explorerMoveReq.NewDisplayOrders))
for i := 0; i < len(explorerMoveReq.NewDisplayOrders); i++ {
newDisplayOrder := explorerMoveReq.NewDisplayOrders[i]
explorer := &models.InsightsExplorer{
Uid: uid,
ExplorerId: newDisplayOrder.Id,
DisplayOrder: newDisplayOrder.DisplayOrder,
}
explorers[i] = explorer
}
err = a.insightsExploreres.ModifyInsightsExplorerDisplayOrders(c, uid, explorers)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerMoveHandler] failed to move insights explorers for user \"uid:%d\", because %s", uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[explorers.InsightsExplorerMoveHandler] user \"uid:%d\" has moved insights explorers", uid)
return true, nil
}
// InsightsExplorerDeleteHandler deletes an existed insights explorer by request parameters for current user
func (a *InsightsExplorersApi) InsightsExplorerDeleteHandler(c *core.WebContext) (any, *errs.Error) {
var explorerDeleteReq models.InsightsExplorerDeleteRequest
err := c.ShouldBindJSON(&explorerDeleteReq)
if err != nil {
log.Warnf(c, "[explorers.InsightsExplorerDeleteHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
err = a.insightsExploreres.DeleteInsightsExplorer(c, uid, explorerDeleteReq.Id)
if err != nil {
log.Errorf(c, "[explorers.InsightsExplorerDeleteHandler] failed to delete insights explorer \"id:%d\" for user \"uid:%d\", because %s", explorerDeleteReq.Id, uid, err.Error())
return nil, errs.Or(err, errs.ErrOperationFailed)
}
log.Infof(c, "[explorers.InsightsExplorerDeleteHandler] user \"uid:%d\" has deleted insights explorer \"id:%d\"", uid, explorerDeleteReq.Id)
return true, nil
}
func (a *InsightsExplorersApi) createNewInsightsExplorerModel(uid int64, explorerCreateReq *models.InsightsExplorerCreateRequest, order int32) (*models.InsightsExplorer, error) {
data, err := json.Marshal(explorerCreateReq.Data)
if err != nil {
return nil, err
}
return &models.InsightsExplorer{
Uid: uid,
Name: explorerCreateReq.Name,
Data: string(data),
DisplayOrder: order,
}, nil
}
+1
View File
@@ -43,6 +43,7 @@ const (
NormalSubcategoryLargeLanguageModel = 15
NormalSubcategoryUserExternalAuth = 16
NormalSubcategoryOAuth2 = 17
NormalSubcategoryInsightsExplorer = 18
)
// Error represents the specific error returned to user
+10
View File
@@ -0,0 +1,10 @@
package errs
import "net/http"
// Error codes related to insights explorers
var (
ErrInsightsExplorerIdInvalid = NewNormalError(NormalSubcategoryInsightsExplorer, 0, http.StatusBadRequest, "explorer id is invalid")
ErrInsightsExplorerNotFound = NewNormalError(NormalSubcategoryInsightsExplorer, 1, http.StatusBadRequest, "explorer not found")
ErrInsightsExplorerDataInvalid = NewNormalError(NormalSubcategoryInsightsExplorer, 2, http.StatusBadRequest, "explorer data is invalid")
)
+108
View File
@@ -0,0 +1,108 @@
package models
import "encoding/json"
// InsightsExplorer represents a saved insights explorer configuration
type InsightsExplorer struct {
ExplorerId int64 `xorm:"PK"`
Uid int64 `xorm:"INDEX(IDX_insights_explorer_uid_deleted_order) NOT NULL"`
Deleted bool `xorm:"INDEX(IDX_insights_explorer_uid_deleted_order) NOT NULL"`
Name string `xorm:"VARCHAR(64) NOT NULL"`
DisplayOrder int32 `xorm:"INDEX(IDX_insights_explorer_uid_deleted_order) NOT NULL"`
Data string `xorm:"MEDIUMBLOB"`
Hidden bool `xorm:"NOT NULL"`
CreatedUnixTime int64
UpdatedUnixTime int64
DeletedUnixTime int64
}
// InsightsExplorerCreateRequest represents all parameters of insights explorer creation request
type InsightsExplorerCreateRequest struct {
Name string `json:"name" binding:"required,notBlank,max=64"`
Data map[string]any `json:"data" binding:"required"`
ClientSessionId string `json:"clientSessionId"`
}
// InsightsExplorerModifyRequest represents all parameters of insights explorer modification request
type InsightsExplorerModifyRequest struct {
Id int64 `json:"id,string" binding:"required,min=0"`
Name string `json:"name" binding:"required,notBlank,max=64"`
Data map[string]any `json:"data" binding:"required"`
Hidden bool `json:"hidden"`
ClientSessionId string `json:"clientSessionId"`
}
// InsightsExplorerGetRequest represents all parameters of insights explorer getting request
type InsightsExplorerGetRequest struct {
Id int64 `form:"id,string" binding:"required,min=1"`
}
// InsightsExplorerHideRequest represents all parameters of insights explorer hiding request
type InsightsExplorerHideRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
Hidden bool `json:"hidden"`
}
// InsightsExplorerMoveRequest represents all parameters of insights explorer moving request
type InsightsExplorerMoveRequest struct {
NewDisplayOrders []*InsightsExplorerNewDisplayOrderRequest `json:"newDisplayOrders" binding:"required,min=1"`
}
// InsightsExplorerNewDisplayOrderRequest represents a data pair of id and display order
type InsightsExplorerNewDisplayOrderRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
DisplayOrder int32 `json:"displayOrder"`
}
// InsightsExplorerDeleteRequest represents all parameters of insights explorer deleting request
type InsightsExplorerDeleteRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
}
// InsightsExplorerInfoResponse represents a view-object of insights explorer info
type InsightsExplorerInfoResponse struct {
Id int64 `json:"id,string"`
Name string `json:"name"`
DisplayOrder int32 `json:"displayOrder"`
Hidden bool `json:"hidden"`
Data map[string]any `json:"data,omitempty"`
}
// ToInsightsExplorerInfoResponse returns a view-object according to database model
func (a *InsightsExplorer) ToInsightsExplorerInfoResponse() (*InsightsExplorerInfoResponse, error) {
var data map[string]any = nil
if a.Data != "" {
err := json.Unmarshal([]byte(a.Data), &data)
if err != nil {
return nil, err
}
}
return &InsightsExplorerInfoResponse{
Id: a.ExplorerId,
Name: a.Name,
DisplayOrder: a.DisplayOrder,
Hidden: a.Hidden,
Data: data,
}, nil
}
// InsightsExplorerInfoResponseSlice represents the slice data structure of InsightsExplorerInfoResponse
type InsightsExplorerInfoResponseSlice []*InsightsExplorerInfoResponse
// Len returns the count of items
func (s InsightsExplorerInfoResponseSlice) Len() int {
return len(s)
}
// Swap swaps two items
func (s InsightsExplorerInfoResponseSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less reports whether the first item is less than the second one
func (s InsightsExplorerInfoResponseSlice) Less(i, j int) bool {
return s[i].DisplayOrder < s[j].DisplayOrder
}
+30
View File
@@ -0,0 +1,30 @@
package models
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInsightsExplorerInfoResponseSliceLess(t *testing.T) {
var insightsExplorerRespSlice InsightsExplorerInfoResponseSlice
insightsExplorerRespSlice = append(insightsExplorerRespSlice, &InsightsExplorerInfoResponse{
Id: 1,
DisplayOrder: 3,
})
insightsExplorerRespSlice = append(insightsExplorerRespSlice, &InsightsExplorerInfoResponse{
Id: 2,
DisplayOrder: 1,
})
insightsExplorerRespSlice = append(insightsExplorerRespSlice, &InsightsExplorerInfoResponse{
Id: 3,
DisplayOrder: 2,
})
sort.Sort(insightsExplorerRespSlice)
assert.Equal(t, int64(2), insightsExplorerRespSlice[0].Id)
assert.Equal(t, int64(3), insightsExplorerRespSlice[1].Id)
assert.Equal(t, int64(1), insightsExplorerRespSlice[2].Id)
}
+230
View File
@@ -0,0 +1,230 @@
package services
import (
"time"
"xorm.io/xorm"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/datastore"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/uuid"
)
// InsightsExplorerService represents insights explorer service
type InsightsExplorerService struct {
ServiceUsingDB
ServiceUsingUuid
}
// Initialize a insights explorer service singleton instance
var (
InsightsExplorers = &InsightsExplorerService{
ServiceUsingDB: ServiceUsingDB{
container: datastore.Container,
},
ServiceUsingUuid: ServiceUsingUuid{
container: uuid.Container,
},
}
)
// GetAllInsightsExplorerNamesByUid returns all insights explorer models of user without data
func (s *InsightsExplorerService) GetAllInsightsExplorerNamesByUid(c core.Context, uid int64) ([]*models.InsightsExplorer, error) {
if uid <= 0 {
return nil, errs.ErrUserIdInvalid
}
var explorers []*models.InsightsExplorer
err := s.UserDataDB(uid).NewSession(c).Select("explorer_id, uid, name, display_order, hidden").Where("uid=? AND deleted=?", uid, false).Find(&explorers)
return explorers, err
}
// GetInsightsExplorerByExplorerId returns a insights explorer model according to insights explorer id
func (s *InsightsExplorerService) GetInsightsExplorerByExplorerId(c core.Context, uid int64, explorerId int64) (*models.InsightsExplorer, error) {
if uid <= 0 {
return nil, errs.ErrUserIdInvalid
}
if explorerId <= 0 {
return nil, errs.ErrInsightsExplorerIdInvalid
}
explorer := &models.InsightsExplorer{}
has, err := s.UserDataDB(uid).NewSession(c).ID(explorerId).Where("uid=? AND deleted=?", uid, false).Get(explorer)
if err != nil {
return nil, err
} else if !has {
return nil, errs.ErrInsightsExplorerNotFound
}
return explorer, nil
}
// GetMaxDisplayOrder returns the max display order
func (s *InsightsExplorerService) GetMaxDisplayOrder(c core.Context, uid int64) (int32, error) {
if uid <= 0 {
return 0, errs.ErrUserIdInvalid
}
explorer := &models.InsightsExplorer{}
has, err := s.UserDataDB(uid).NewSession(c).Cols("uid", "deleted", "display_order").Where("uid=? AND deleted=?", uid, false).OrderBy("display_order desc").Limit(1).Get(explorer)
if err != nil {
return 0, err
}
if has {
return explorer.DisplayOrder, nil
} else {
return 0, nil
}
}
// CreateInsightsExplorer saves a new insights explorer model to database
func (s *InsightsExplorerService) CreateInsightsExplorer(c core.Context, explorer *models.InsightsExplorer) error {
if explorer.Uid <= 0 {
return errs.ErrUserIdInvalid
}
explorer.ExplorerId = s.GenerateUuid(uuid.UUID_TYPE_EXPLORER)
if explorer.ExplorerId < 1 {
return errs.ErrSystemIsBusy
}
explorer.Deleted = false
explorer.CreatedUnixTime = time.Now().Unix()
explorer.UpdatedUnixTime = time.Now().Unix()
return s.UserDataDB(explorer.Uid).DoTransaction(c, func(sess *xorm.Session) error {
_, err := sess.Insert(explorer)
return err
})
}
// ModifyInsightsExplorer saves an existed insights explorer model to database
func (s *InsightsExplorerService) ModifyInsightsExplorer(c core.Context, explorer *models.InsightsExplorer) error {
if explorer.Uid <= 0 {
return errs.ErrUserIdInvalid
}
explorer.UpdatedUnixTime = time.Now().Unix()
return s.UserDataDB(explorer.Uid).DoTransaction(c, func(sess *xorm.Session) error {
updatedRows, err := sess.ID(explorer.ExplorerId).Cols("name", "data", "updated_unix_time").Where("uid=? AND deleted=?", explorer.Uid, false).Update(explorer)
if err != nil {
return err
} else if updatedRows < 1 {
return errs.ErrInsightsExplorerNotFound
}
return err
})
}
// HideInsightsExplorer updates hidden field of given insights explorer ids
func (s *InsightsExplorerService) HideInsightsExplorer(c core.Context, uid int64, ids []int64, hidden bool) error {
if uid <= 0 {
return errs.ErrUserIdInvalid
}
now := time.Now().Unix()
updateModel := &models.InsightsExplorer{
Hidden: hidden,
UpdatedUnixTime: now,
}
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
updatedRows, err := sess.Cols("hidden", "updated_unix_time").Where("uid=? AND deleted=?", uid, false).In("explorer_id", ids).Update(updateModel)
if err != nil {
return err
} else if updatedRows < 1 {
return errs.ErrInsightsExplorerNotFound
}
return err
})
}
// ModifyInsightsExplorerDisplayOrders updates display order of given insights explorers
func (s *InsightsExplorerService) ModifyInsightsExplorerDisplayOrders(c core.Context, uid int64, explorers []*models.InsightsExplorer) error {
if uid <= 0 {
return errs.ErrUserIdInvalid
}
for i := 0; i < len(explorers); i++ {
explorers[i].UpdatedUnixTime = time.Now().Unix()
}
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
for i := 0; i < len(explorers); i++ {
explorer := explorers[i]
updatedRows, err := sess.ID(explorer.ExplorerId).Cols("display_order", "updated_unix_time").Where("uid=? AND deleted=?", uid, false).Update(explorer)
if err != nil {
return err
} else if updatedRows < 1 {
return errs.ErrInsightsExplorerNotFound
}
}
return nil
})
}
// DeleteInsightsExplorer deletes an existed insights explorer from database
func (s *InsightsExplorerService) DeleteInsightsExplorer(c core.Context, uid int64, explorerId int64) error {
if uid <= 0 {
return errs.ErrUserIdInvalid
}
now := time.Now().Unix()
updateModel := &models.InsightsExplorer{
Deleted: true,
DeletedUnixTime: now,
}
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
deletedRows, err := sess.ID(explorerId).Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).Update(updateModel)
if err != nil {
return err
} else if deletedRows < 1 {
return errs.ErrInsightsExplorerNotFound
}
return err
})
}
// DeleteAllInsightsExplorers deletes all existed insights explorers from database
func (s *InsightsExplorerService) DeleteAllInsightsExplorers(c core.Context, uid int64) error {
if uid <= 0 {
return errs.ErrUserIdInvalid
}
now := time.Now().Unix()
updateModel := &models.InsightsExplorer{
Deleted: true,
DeletedUnixTime: now,
}
return s.UserDataDB(uid).DoTransaction(c, func(sess *xorm.Session) error {
_, err := sess.Cols("deleted", "deleted_unix_time").Where("uid=? AND deleted=?", uid, false).Update(updateModel)
if err != nil {
return err
}
return nil
})
}
+1
View File
@@ -14,4 +14,5 @@ const (
UUID_TYPE_TAG_INDEX UuidType = 6
UUID_TYPE_TEMPLATE UuidType = 7
UUID_TYPE_PICTURE UuidType = 8
UUID_TYPE_EXPLORER UuidType = 9
)