|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/labstack/echo/v4" |
| 9 | + "github.com/ockibagusp/golang-website-example/middleware" |
| 10 | + "github.com/ockibagusp/golang-website-example/models" |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +/* |
| 15 | + * Delete Permanently |
| 16 | + * |
| 17 | + * @target: [Admin] Delete Permanently |
| 18 | + * @method: GET |
| 19 | + * @route: /admin/delete-permanently |
| 20 | + */ |
| 21 | +func (controller *Controller) DeletePermanently(c echo.Context) error { |
| 22 | + session, _ := middleware.GetAuth(c) |
| 23 | + log := log.WithFields(log.Fields{ |
| 24 | + "username": session.Values["username"], |
| 25 | + "route": c.Path(), |
| 26 | + }) |
| 27 | + log.Info("START request method GET for admin delete permanently") |
| 28 | + |
| 29 | + is_auth_type := session.Values["is_auth_type"] |
| 30 | + if is_auth_type == -1 { |
| 31 | + log.Warn("for GET to admin delete permanently without no-session [@route: /login]") |
| 32 | + middleware.SetFlashError(c, "login process failed!") |
| 33 | + log.Warn("END request method GET for admin delete permanently: [-]failure") |
| 34 | + return c.Redirect(http.StatusFound, "/login") |
| 35 | + } |
| 36 | + |
| 37 | + if !middleware.IsAdmin(is_auth_type) { |
| 38 | + log.Warn("END request method GET for admin delete permanently: [-]failure") |
| 39 | + // HTTP response status: 404 Not Found |
| 40 | + return c.HTML(http.StatusNotFound, "404 Not Found") |
| 41 | + } |
| 42 | + |
| 43 | + var ( |
| 44 | + users []models.User |
| 45 | + err error |
| 46 | + |
| 47 | + // typing: all, admin and user |
| 48 | + typing string |
| 49 | + ) |
| 50 | + |
| 51 | + if c.QueryParam("admin") == "all" { |
| 52 | + log.Infof(`for GET to admin delete permanently: admin models.User{}.FindDeleteAll(db, "admin")`) |
| 53 | + typing = "Admin" |
| 54 | + users, err = models.User{}.FindDeleteAll(controller.DB, "admin") |
| 55 | + } else if c.QueryParam("user") == "all" { |
| 56 | + log.Infof(`for GET to admin delete permanently: user models.User{}.FindDeleteAll(db, "user")`) |
| 57 | + typing = "User" |
| 58 | + users, err = models.User{}.FindDeleteAll(controller.DB, "user") |
| 59 | + } else { |
| 60 | + log.Infof(`for GET to admin delete permanently: models.User{}.FindDeleteAll(db) or models.User{}.FindDeleteAll(db, "all")`) |
| 61 | + typing = "All" |
| 62 | + // models.User{} or (models.User{}) or var user models.User or user := models.User{} |
| 63 | + users, err = models.User{}.FindDeleteAll(controller.DB) |
| 64 | + } |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + log.Warnf("for GET to admin delete permanently without models.User{}.FindDeleteAll() errors: `%v`", err) |
| 68 | + log.Warn("END request method GET for admin delete permanently: [-]failure") |
| 69 | + // HTTP response status: 404 Not Found |
| 70 | + return c.HTML(http.StatusNotFound, err.Error()) |
| 71 | + } |
| 72 | + |
| 73 | + log.Info("END request method GET to admin delete permanently: [+]success") |
| 74 | + return c.Render(http.StatusOK, "admin/admin-delete-permanently.html", echo.Map{ |
| 75 | + "name": fmt.Sprintf("Users: %v", typing), |
| 76 | + "nav": "users", // (?) |
| 77 | + "session": session, |
| 78 | + /* |
| 79 | + "flash": echo.Map{"success": ..., "error": ...} |
| 80 | +
|
| 81 | + or, |
| 82 | +
|
| 83 | + "flash_success": .... |
| 84 | + "flash_error": .... |
| 85 | + */ |
| 86 | + |
| 87 | + "flash": echo.Map{ |
| 88 | + "success": middleware.GetFlashSuccess(c), |
| 89 | + "error": middleware.GetFlashError(c), |
| 90 | + }, |
| 91 | + "users": users, |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +/* |
| 96 | + * Delete Permanently By ID |
| 97 | + * |
| 98 | + * @target: [Admin] Delete Permanently By ID |
| 99 | + * @method: GET |
| 100 | + * @route: /admin/delete/permanently/:id |
| 101 | + */ |
| 102 | +func (controller *Controller) DeletePermanentlyByID(c echo.Context) error { |
| 103 | + session, _ := middleware.GetAuth(c) |
| 104 | + log := log.WithFields(log.Fields{ |
| 105 | + "username": session.Values["username"], |
| 106 | + "route": c.Path(), |
| 107 | + }) |
| 108 | + log.Info("START request method GET for admin delete permanently by id") |
| 109 | + |
| 110 | + is_auth_type := session.Values["is_auth_type"] |
| 111 | + if is_auth_type == -1 { |
| 112 | + log.Warn("for GET to admin delete permanently by id without no-session [@route: /login]") |
| 113 | + middleware.SetFlashError(c, "login process failed!") |
| 114 | + log.Warn("END request method GET for admin delete permanently by id: [-]failure") |
| 115 | + return c.Redirect(http.StatusFound, "/login") |
| 116 | + } |
| 117 | + |
| 118 | + id, _ := strconv.Atoi(c.Param("id")) |
| 119 | + |
| 120 | + // why? |
| 121 | + // delete permanently not for admin |
| 122 | + if id == 1 { |
| 123 | + log.Warn("END request method GET for admin delete permanently by id [admin]: [-]failure") |
| 124 | + // HTTP response status: 403 Forbidden |
| 125 | + return c.HTML(http.StatusForbidden, "403 Forbidden") |
| 126 | + } |
| 127 | + |
| 128 | + if !middleware.IsAdmin(is_auth_type) { |
| 129 | + log.Warn("END request method GET for admin delete permanently by id: [-]failure") |
| 130 | + // HTTP response status: 404 Not Found |
| 131 | + return c.HTML(http.StatusNotFound, "404 Not Found") |
| 132 | + } |
| 133 | + |
| 134 | + user, err := (models.User{}).UnscopedFirstUserByID(controller.DB, id) |
| 135 | + if err != nil { |
| 136 | + log.Warnf("for GET to admin delete permanently by id without models.User{}.FirstByID() errors: `%v`", err) |
| 137 | + log.Warn("END request method GET for admin delete permanently by id: [-]failure") |
| 138 | + // HTTP response status: 404 Not Found |
| 139 | + return c.HTML(http.StatusNotFound, err.Error()) |
| 140 | + } |
| 141 | + |
| 142 | + if err := user.DeletePermanently(controller.DB, id); err != nil { |
| 143 | + log.Warnf("for GET to admin delete permanently by id without models.User{}.Delete() errors: `%v`", err) |
| 144 | + log.Warn("END request method GET for admin delete permanently by id: [-]failure") |
| 145 | + // HTTP response status: 403 Forbidden |
| 146 | + return c.HTML(http.StatusForbidden, err.Error()) |
| 147 | + } |
| 148 | + |
| 149 | + // delete permanently admin |
| 150 | + log.Info("END request method GET for admin delete permanently by id: [+]success") |
| 151 | + return c.Redirect(http.StatusMovedPermanently, "/admin/delete-permanently") |
| 152 | +} |
| 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 | +} |
0 commit comments