123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package cosy
- import (
- "github.com/0xJacky/Nginx-UI/model"
- "gorm.io/gorm"
- "net/http"
- )
- func (c *Ctx[T]) PermanentlyDelete() {
- c.permanentlyDelete = true
- c.Destroy()
- }
- func (c *Ctx[T]) Destroy() {
- if c.abort {
- return
- }
- id := c.ctx.Param("id")
- c.beforeExecuteHook()
- db := model.UseDB()
- var dbModel T
- result := db
- if len(c.gormScopes) > 0 {
- result = result.Scopes(c.gormScopes...)
- }
- err := result.Session(&gorm.Session{}).First(&dbModel, id).Error
- if err != nil {
- errHandler(c.ctx, err)
- return
- }
- if c.permanentlyDelete {
- result = result.Unscoped()
- }
- err = result.Delete(&dbModel).Error
- if err != nil {
- errHandler(c.ctx, err)
- return
- }
- if len(c.executedHookFunc) > 0 {
- for _, v := range c.executedHookFunc {
- v(c)
- if c.abort {
- return
- }
- }
- }
- c.ctx.JSON(http.StatusNoContent, nil)
- }
- func (c *Ctx[T]) Recover() {
- if c.abort {
- return
- }
- id := c.ctx.Param("id")
- c.beforeExecuteHook()
- db := model.UseDB()
- var dbModel T
- result := db.Unscoped()
- if len(c.gormScopes) > 0 {
- result = result.Scopes(c.gormScopes...)
- }
- err := result.Session(&gorm.Session{}).First(&dbModel, id).Error
- if err != nil {
- errHandler(c.ctx, err)
- return
- }
- err = result.Model(&dbModel).Update("deleted_at", nil).Error
- if err != nil {
- errHandler(c.ctx, err)
- return
- }
- if len(c.executedHookFunc) > 0 {
- for _, v := range c.executedHookFunc {
- v(c)
- if c.abort {
- return
- }
- }
- }
- c.ctx.JSON(http.StatusNoContent, nil)
- }
|