up peer update
This commit is contained in:
@@ -689,9 +689,9 @@ func (a *Ab) PeerDel(c *gin.Context) {
|
|||||||
// @Router /ab/peer/update/{guid} [put]
|
// @Router /ab/peer/update/{guid} [put]
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
func (a *Ab) PeerUpdate(c *gin.Context) {
|
func (a *Ab) PeerUpdate(c *gin.Context) {
|
||||||
//f := &gin.H{}
|
f := gin.H{}
|
||||||
f := &requstform.PersonalAddressBookForm{}
|
//f := &requstform.PersonalAddressBookForm{}
|
||||||
err := c.ShouldBindJSON(f)
|
err := c.ShouldBindJSON(&f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
|
response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||||
return
|
return
|
||||||
@@ -709,17 +709,33 @@ func (a *Ab) PeerUpdate(c *gin.Context) {
|
|||||||
response.Error(c, response.TranslateMsg(c, "NoAccess"))
|
response.Error(c, response.TranslateMsg(c, "NoAccess"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//fmt.Println(f)
|
//fmt.Println(f)
|
||||||
//return
|
//判断f["Id"]是否存在
|
||||||
ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, f.Id, cid)
|
fid, ok := f["id"]
|
||||||
|
if !ok {
|
||||||
|
response.Error(c, response.TranslateMsg(c, "ParamsError"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fidstr := fid.(string)
|
||||||
|
|
||||||
|
ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, fidstr, cid)
|
||||||
if ab == nil || ab.RowId == 0 {
|
if ab == nil || ab.RowId == 0 {
|
||||||
response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
|
response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nab := f.ToAddressBook()
|
//允许的字段
|
||||||
nab.RowId = ab.RowId
|
allowUp := []string{"password", "hash", "tags", "alias"}
|
||||||
err = service.AllService.AddressBookService.Update(nab)
|
//f中的字段如果不在allowUp中,就删除
|
||||||
|
for k := range f {
|
||||||
|
if !utils.InArray(k, allowUp) {
|
||||||
|
delete(f, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//fmt.Println(f)
|
||||||
|
if tags, _ok := f["tags"]; _ok {
|
||||||
|
f["tags"], _ = json.Marshal(tags)
|
||||||
|
}
|
||||||
|
err = service.AllService.AddressBookService.UpdateByMap(ab, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -130,6 +130,11 @@ func (s *AddressBookService) Update(u *model.AddressBook) error {
|
|||||||
return global.DB.Model(u).Updates(u).Error
|
return global.DB.Model(u).Updates(u).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateByMap 更新
|
||||||
|
func (s *AddressBookService) UpdateByMap(u *model.AddressBook, data map[string]interface{}) error {
|
||||||
|
return global.DB.Model(u).Updates(data).Error
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateAll 更新
|
// UpdateAll 更新
|
||||||
func (s *AddressBookService) UpdateAll(u *model.AddressBook) error {
|
func (s *AddressBookService) UpdateAll(u *model.AddressBook) error {
|
||||||
return global.DB.Model(u).Select("*").Omit("created_at").Updates(u).Error
|
return global.DB.Model(u).Select("*").Omit("created_at").Updates(u).Error
|
||||||
|
|||||||
@@ -91,3 +91,12 @@ func Values[K comparable, V any](m map[K]V) []V {
|
|||||||
}
|
}
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InArray(k string, arr []string) bool {
|
||||||
|
for _, v := range arr {
|
||||||
|
if k == v {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user