add batch add ab from peer
add batch update ab tags
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"Gwen/http/response"
|
"Gwen/http/response"
|
||||||
"Gwen/model"
|
"Gwen/model"
|
||||||
"Gwen/service"
|
"Gwen/service"
|
||||||
|
"encoding/json"
|
||||||
_ "encoding/json"
|
_ "encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -327,3 +328,70 @@ func (ct *AddressBook) ShareByWebClient(c *gin.Context) {
|
|||||||
"share_token": m.ShareToken,
|
"share_token": m.ShareToken,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ct *AddressBook) BatchCreateFromPeers(c *gin.Context) {
|
||||||
|
f := &admin.BatchCreateFromPeersForm{}
|
||||||
|
if err := c.ShouldBindJSON(f); err != nil {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u := service.AllService.UserService.CurUser(c)
|
||||||
|
|
||||||
|
if f.CollectionId != 0 {
|
||||||
|
collection := service.AllService.AddressBookService.CollectionInfoById(f.CollectionId)
|
||||||
|
if collection.Id == 0 {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if collection.UserId != u.Id {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
peers := service.AllService.PeerService.List(1, 999, func(tx *gorm.DB) {
|
||||||
|
tx.Where("row_id in ?", f.PeerIds)
|
||||||
|
tx.Where("user_id = ?", u.Id)
|
||||||
|
})
|
||||||
|
if peers.Total == 0 {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tags, _ := json.Marshal(f.Tags)
|
||||||
|
for _, peer := range peers.Peers {
|
||||||
|
ab := service.AllService.AddressBookService.FromPeer(peer)
|
||||||
|
ab.Tags = tags
|
||||||
|
ab.CollectionId = f.CollectionId
|
||||||
|
ex := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(u.Id, ab.Id, ab.CollectionId)
|
||||||
|
if ex.RowId != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
service.AllService.AddressBookService.Create(ab)
|
||||||
|
}
|
||||||
|
response.Success(c, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ct *AddressBook) BatchUpdateTags(c *gin.Context) {
|
||||||
|
f := &admin.BatchUpdateTagsForm{}
|
||||||
|
if err := c.ShouldBindJSON(f); err != nil {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u := service.AllService.UserService.CurUser(c)
|
||||||
|
|
||||||
|
abs := service.AllService.AddressBookService.List(1, 999, func(tx *gorm.DB) {
|
||||||
|
tx.Where("row_id in ?", f.RowIds)
|
||||||
|
tx.Where("user_id = ?", u.Id)
|
||||||
|
})
|
||||||
|
if abs.Total == 0 {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := service.AllService.AddressBookService.BatchUpdateTags(abs.AddressBooks, f.Tags)
|
||||||
|
if err != nil {
|
||||||
|
response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.Success(c, nil)
|
||||||
|
}
|
||||||
|
|||||||
@@ -122,3 +122,13 @@ type AddressBookCollectionRuleQuery struct {
|
|||||||
IsMy int `form:"is_my"`
|
IsMy int `form:"is_my"`
|
||||||
PageQuery
|
PageQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BatchCreateFromPeersForm struct {
|
||||||
|
CollectionId uint `json:"collection_id"`
|
||||||
|
PeerIds []uint `json:"peer_ids"`
|
||||||
|
Tags []string `json:"tags"`
|
||||||
|
}
|
||||||
|
type BatchUpdateTagsForm struct {
|
||||||
|
RowIds []uint `json:"row_ids"`
|
||||||
|
Tags []string `json:"tags"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -107,9 +107,12 @@ func AddressBookBind(rg *gin.RouterGroup) {
|
|||||||
aR.POST("/update", cont.Update)
|
aR.POST("/update", cont.Update)
|
||||||
aR.POST("/delete", cont.Delete)
|
aR.POST("/delete", cont.Delete)
|
||||||
aR.POST("/shareByWebClient", cont.ShareByWebClient)
|
aR.POST("/shareByWebClient", cont.ShareByWebClient)
|
||||||
|
aR.POST("/batchCreateFromPeers", cont.BatchCreateFromPeers)
|
||||||
|
aR.POST("/batchUpdateTags", cont.BatchUpdateTags)
|
||||||
|
|
||||||
arp := aR.Use(middleware.AdminPrivilege())
|
arp := aR.Use(middleware.AdminPrivilege())
|
||||||
arp.POST("/batchCreate", cont.BatchCreate)
|
arp.POST("/batchCreate", cont.BatchCreate)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func PeerBind(rg *gin.RouterGroup) {
|
func PeerBind(rg *gin.RouterGroup) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package service
|
|||||||
import (
|
import (
|
||||||
"Gwen/global"
|
"Gwen/global"
|
||||||
"Gwen/model"
|
"Gwen/model"
|
||||||
|
"encoding/json"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -116,6 +117,16 @@ func (s *AddressBookService) List(page, pageSize uint, where func(tx *gorm.DB))
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *AddressBookService) FromPeer(peer *model.Peer) (a *model.AddressBook) {
|
||||||
|
a = &model.AddressBook{}
|
||||||
|
a.Id = peer.Id
|
||||||
|
a.Username = peer.Username
|
||||||
|
a.Hostname = peer.Hostname
|
||||||
|
a.UserId = peer.UserId
|
||||||
|
a.Platform = s.PlatformFromOs(peer.Os)
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
// Create 创建
|
// Create 创建
|
||||||
func (s *AddressBookService) Create(u *model.AddressBook) error {
|
func (s *AddressBookService) Create(u *model.AddressBook) error {
|
||||||
res := global.DB.Create(u).Error
|
res := global.DB.Create(u).Error
|
||||||
@@ -318,3 +329,12 @@ func (s *AddressBookService) CheckCollectionOwner(uid uint, cid uint) bool {
|
|||||||
p := s.CollectionInfoById(cid)
|
p := s.CollectionInfoById(cid)
|
||||||
return p.UserId == uid
|
return p.UserId == uid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *AddressBookService) BatchUpdateTags(abs []*model.AddressBook, tags []string) error {
|
||||||
|
ids := make([]uint, 0)
|
||||||
|
for _, ab := range abs {
|
||||||
|
ids = append(ids, ab.RowId)
|
||||||
|
}
|
||||||
|
tagsv, _ := json.Marshal(tags)
|
||||||
|
return global.DB.Model(&model.AddressBook{}).Where("row_id in ?", ids).Update("tags", tagsv).Error
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user