Skip to content

Commit 49fe240

Browse files
committed
[31/8/22] add user restore
1 parent 575ca80 commit 49fe240

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

controllers/admin_controller.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,64 @@ func (controller *Controller) DeletePermanentlyByID(c echo.Context) error {
150150
log.Info("END request method GET for admin delete permanently by id: [+]success")
151151
return c.Redirect(http.StatusMovedPermanently, "/admin/delete-permanently")
152152
}
153+
154+
/*
155+
* Restore User
156+
*
157+
* @target: [Admin] Restore User
158+
* @method: GET
159+
* @route: /admin/restore/:id
160+
*/
161+
func (controller *Controller) RestoreUser(c echo.Context) error {
162+
session, _ := middleware.GetAuth(c)
163+
log := log.WithFields(log.Fields{
164+
"username": session.Values["username"],
165+
"route": c.Path(),
166+
})
167+
log.Info("START request method GET for admin restore")
168+
169+
is_auth_type := session.Values["is_auth_type"]
170+
if is_auth_type == -1 {
171+
log.Warn("for GET to admin restore without no-session [@route: /login]")
172+
middleware.SetFlashError(c, "login process failed!")
173+
log.Warn("END request method GET for admin restore: [-]failure")
174+
return c.Redirect(http.StatusFound, "/login")
175+
}
176+
177+
id, _ := strconv.Atoi(c.Param("id"))
178+
179+
// why?
180+
// delete permanently not for admin
181+
if id == 1 {
182+
log.Warn("END request method GET for admin restore [admin]: [-]failure")
183+
// HTTP response status: 403 Forbidden
184+
return c.HTML(http.StatusForbidden, "403 Forbidden")
185+
}
186+
187+
if !middleware.IsAdmin(is_auth_type) {
188+
log.Warn("END request method GET for admin restore: [-]failure")
189+
// HTTP response status: 404 Not Found
190+
return c.HTML(http.StatusNotFound, "404 Not Found")
191+
}
192+
193+
user, err := (models.User{}).UnscopedFirstUserByID(controller.DB, id)
194+
if err != nil {
195+
log.Warnf("for GET to admin restore without models.User{}.FirstByID() errors: `%v`", err)
196+
log.Warn("END request method GET for admin restore: [-]failure")
197+
// HTTP response status: 404 Not Found
198+
return c.HTML(http.StatusNotFound, err.Error())
199+
}
200+
201+
if err := user.Restore(controller.DB, id); err != nil {
202+
log.Warnf("for GET to admin restore without models.User{}.Restore() errors: `%v`", err)
203+
log.Warn("END request method GET for admin restore: [-]failure")
204+
// HTTP response status: 403 Forbidden
205+
return c.HTML(http.StatusForbidden, err.Error())
206+
}
207+
208+
middleware.SetFlashSuccess(c, fmt.Sprintf("success restore user: %s!", user.Username))
209+
210+
// restore admin
211+
log.Info("END request method GET for admin restore: [+]success")
212+
return c.Redirect(http.StatusMovedPermanently, "/admin/delete-permanently")
213+
}

models/user.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ func (user User) FindDeleteAll(db *gorm.DB, admin_or_user ...string) ([]User, er
217217
return users, nil
218218
}
219219

220+
// User: Restore
221+
func (user User) Restore(db *gorm.DB, id int) error {
222+
tx := db.Begin()
223+
var count int64
224+
// if tx.Unscoped().Select("id").First(&user).Error != nil {}
225+
if tx.Unscoped().Select("id").First(&user).Count(&count); count != 1 {
226+
tx.Rollback()
227+
return errors.New("User Not Found")
228+
}
229+
230+
// if tx.Model(&user).Unscoped().Where("id = ?", id).Update(...).Error; err != nil {}
231+
if err := tx.Model(&user).Unscoped().Where("id = ?", id).Update("deleted_at", nil).First(&user).Error; err != nil {
232+
tx.Rollback()
233+
return err
234+
}
235+
tx.Commit()
236+
237+
return nil
238+
}
239+
220240
// User: Delete Permanently
221241
func (user User) DeletePermanently(db *gorm.DB, id int) error {
222242
tx := db.Begin()

router/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func New(controllers *controllers.Controller) (router *echo.Echo) {
6969
// admin
7070
router.GET("/admin/delete-permanently", controllers.DeletePermanently).
7171
Name = "/admin/delete-permanently get"
72+
router.GET("/admin/restore/:id", controllers.RestoreUser).
73+
Name = "/admin/restore/:id get"
7274
// "/admin/delete-permanently/:id" can not
7375
// "/admin/delete/permanently/:id" can
7476
router.GET("/admin/delete/permanently/:id", controllers.DeletePermanentlyByID).

0 commit comments

Comments
 (0)