Skip to content

Commit

Permalink
feat: admin log for hole and division now show diff; ModifyDivision p…
Browse files Browse the repository at this point in the history
…artially updates
  • Loading branch information
JingYiJun committed Jul 31, 2024
1 parent 5bc4ef2 commit 1a1d2a6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
54 changes: 41 additions & 13 deletions apis/division/apis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package division

import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strconv"

"github.com/opentreehole/go-common"
Expand All @@ -9,7 +11,6 @@ import (
. "treehole_next/utils"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)

// AddDivision
Expand Down Expand Up @@ -104,16 +105,17 @@ func GetDivision(c *fiber.Ctx) error {
// @Router /divisions/{id} [put]
// @Router /divisions/{id}/_modify [patch]
// @Param id path int true "id"
// @Param json body ModifyModel true "json"
// @Param json body ModifyDivisionModel true "json"
// @Success 200 {object} models.Division
// @Failure 404 {object} MessageModel
func ModifyDivision(c *fiber.Ctx) error {
// validate body
var body ModifyModel
var body ModifyDivisionModel
err := common.ValidateBody(c, &body)
if err != nil {
return err
}

id, err := c.ParamsInt("id")
if err != nil {
return err
Expand All @@ -130,21 +132,47 @@ func ModifyDivision(c *fiber.Ctx) error {
return common.Forbidden()
}

division := Division{
Name: body.Name,
Description: body.Description,
Pinned: body.Pinned,
var division Division
err = DB.Transaction(func(tx *gorm.DB) error {
err = tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&division, id).Error
if err != nil {
return err
}

modifyData := make(map[string]any)
if body.Name != nil {
modifyData["name"] = *body.Name
}
if body.Description != nil {
modifyData["description"] = *body.Description
}
if body.Pinned != nil {
modifyData["pinned"] = body.Pinned
}

if len(modifyData) == 0 {
return common.BadRequest("No data to modify.")
}

return tx.Model(&division).Updates(modifyData).Error
})
if err != nil {
return err
}
division.ID = id
result := DB.Model(&division).Updates(division)
// nothing updated, means that the record does not exist
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound

var newDivision Division
err = DB.First(&newDivision, id).Error
if err != nil {
return err
}

MyLog("Division", "Modify", division.ID, user.ID, RoleAdmin)

CreateAdminLog(DB, AdminLogTypeDivision, user.ID, body)
CreateAdminLog(DB, AdminLogTypeDivision, user.ID, map[string]any{
"division_id": division.ID,
"before": division,
"after": newDivision,
})

// refresh cache. here should not use `go refreshCache`
err = refreshCache(c)
Expand Down
7 changes: 4 additions & 3 deletions apis/division/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type CreateModel struct {
Description string `json:"description"`
}

type ModifyModel struct {
CreateModel
Pinned []int `json:"pinned"`
type ModifyDivisionModel struct {
Name *string `json:"name"`
Description *string `json:"description"`
Pinned []int `json:"pinned"`
}
21 changes: 17 additions & 4 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,18 @@ func ModifyHole(c *fiber.Ctx) error {

if user.IsAdmin {
CreateAdminLog(tx, AdminLogTypeHole, user.ID, struct {
HoleID int `json:"hole_id"`
Body ModifyModel `json:"body"`
HoleID int `json:"hole_id"`
Before map[string]any `json:"before"`
Modify ModifyModel `json:"modify"`
}{
HoleID: holeID,
Body: body,
Before: map[string]any{
"division_id": hole.DivisionID,
"hidden": hole.Hidden,
"locked": hole.Locked,
"tags": hole.Tags,
},
Modify: body,
})
}
}
Expand Down Expand Up @@ -628,7 +635,13 @@ func HideHole(c *fiber.Ctx) error {

// log
MyLog("Hole", "Hide", holeID, user.ID, RoleAdmin)
CreateAdminLog(DB, AdminLogTypeHideHole, user.ID, hole)
CreateAdminLog(DB, AdminLogTypeHideHole, user.ID, struct {
HoleID int `json:"hole_id"`
Hidden bool `json:"hidden"`
}{
HoleID: holeID,
Hidden: true,
})

// find hole and update cache

Expand Down
6 changes: 3 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/division.ModifyModel"
"$ref": "#/definitions/division.ModifyDivisionModel"
}
}
],
Expand Down Expand Up @@ -360,7 +360,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/division.ModifyModel"
"$ref": "#/definitions/division.ModifyDivisionModel"
}
}
],
Expand Down Expand Up @@ -3065,7 +3065,7 @@ const docTemplate = `{
}
}
},
"division.ModifyModel": {
"division.ModifyDivisionModel": {
"type": "object",
"properties": {
"description": {
Expand Down
6 changes: 3 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/division.ModifyModel"
"$ref": "#/definitions/division.ModifyDivisionModel"
}
}
],
Expand Down Expand Up @@ -353,7 +353,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/division.ModifyModel"
"$ref": "#/definitions/division.ModifyDivisionModel"
}
}
],
Expand Down Expand Up @@ -3058,7 +3058,7 @@
}
}
},
"division.ModifyModel": {
"division.ModifyDivisionModel": {
"type": "object",
"properties": {
"description": {
Expand Down
6 changes: 3 additions & 3 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ definitions:
ID of the target division that all the deleted division's holes will be moved to
type: integer
type: object
division.ModifyModel:
division.ModifyDivisionModel:
properties:
description:
type: string
Expand Down Expand Up @@ -1031,7 +1031,7 @@ paths:
name: json
required: true
schema:
$ref: '#/definitions/division.ModifyModel'
$ref: '#/definitions/division.ModifyDivisionModel'
produces:
- application/json
responses:
Expand Down Expand Up @@ -1059,7 +1059,7 @@ paths:
name: json
required: true
schema:
$ref: '#/definitions/division.ModifyModel'
$ref: '#/definitions/division.ModifyDivisionModel'
produces:
- application/json
responses:
Expand Down

0 comments on commit 1a1d2a6

Please sign in to comment.