@@ -15,6 +15,10 @@ type CreateAnonRuleRequest struct {
1515 Template string `json:"template" binding:"required"`
1616}
1717
18+ type UpdateAnonRulesRequest struct {
19+ Rules []CreateAnonRuleRequest `json:"rules" binding:"required"`
20+ }
21+
1822// @Router /api/anon-rules [get]
1923// @Success 200 {object} []models.AnonRule
2024func (s * Server ) listAnonRules (c * gin.Context ) {
@@ -93,3 +97,61 @@ func (s *Server) deleteAnonRule(c *gin.Context) {
9397
9498 c .Status (http .StatusNoContent )
9599}
100+
101+ // @Router /api/anon-rules [put]
102+ // @Param request body UpdateAnonRulesRequest true "Update anon rules request"
103+ // @Success 200 {object} []models.AnonRule
104+ func (s * Server ) updateAnonRules (c * gin.Context ) {
105+ var req UpdateAnonRulesRequest
106+ if err := c .ShouldBindJSON (& req ); err != nil {
107+ s .logger .Warn ().Err (err ).Msg ("Invalid request body" )
108+ c .JSON (http .StatusBadRequest , gin.H {"error" : "Invalid request body" , "details" : err .Error ()})
109+ return
110+ }
111+
112+ // Use transaction to ensure atomicity (delete all + insert all)
113+ err := s .db .Transaction (func (tx * gorm.DB ) error {
114+ // Delete all existing rules
115+ if err := tx .Where ("1=1" ).Delete (& models.AnonRule {}).Error ; err != nil {
116+ return err
117+ }
118+
119+ // Insert new rules
120+ var newRules []models.AnonRule
121+ for _ , rule := range req .Rules {
122+ newRules = append (newRules , models.AnonRule {
123+ Table : rule .Table ,
124+ Column : rule .Column ,
125+ Template : rule .Template ,
126+ })
127+ }
128+
129+ if len (newRules ) > 0 {
130+ if err := tx .Create (& newRules ).Error ; err != nil {
131+ return err
132+ }
133+ }
134+
135+ return nil
136+ })
137+
138+ if err != nil {
139+ s .logger .Error ().Err (err ).Msg ("Failed to update anon rules" )
140+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to update anonymization rules" })
141+ return
142+ }
143+
144+ // Load and return the new rules
145+ var rules []models.AnonRule
146+ if err := s .db .Order ("created_at DESC" ).Find (& rules ).Error ; err != nil {
147+ s .logger .Error ().Err (err ).Msg ("Failed to load anon rules" )
148+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Internal server error" })
149+ return
150+ }
151+
152+ s .logger .Info ().
153+ Int ("count" , len (rules )).
154+ Msg ("Updated anonymization rules" )
155+
156+ c .JSON (http .StatusOK , rules )
157+ }
0 commit comments