-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.go
More file actions
29 lines (27 loc) · 843 Bytes
/
delete.go
File metadata and controls
29 lines (27 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package gg
import (
"github.com/gin-gonic/gin"
)
// DeleteByID returns a handler that will delete a given record with the matching primary key given in the url
func DeleteByID(urlParam string) gin.HandlerFunc {
return func(ctx *gin.Context) {
db := GetDatabase(ctx)
model := GetModel(ctx)
if cast, ok := model.(BeforeDeleteWithContexter) ; ok {
if err := cast.BeforeDeleteWithContext(ctx, db) ; err != nil {
return
}
}
result := db.Model(model).Delete(model, ctx.Param(urlParam))
if result.RowsAffected == 0 {
DefaultOutput(ctx, 404, gin.H{"error": "not found"})
return
}
if cast, ok := model.(AfterDeleteWithContexter) ; ok {
if err := cast.AfterDeleteWithContext(ctx, db) ; err != nil {
return
}
}
DefaultOutput(ctx, 200, gin.H{"message": "Record deleted"})
}
}