Skip to content

Commit 11b1277

Browse files
authored
Merge pull request #16 from ockibagusp/admin-delete-permanently
Admin delete permanently
2 parents 194fe26 + ec30e3c commit 11b1277

File tree

14 files changed

+697
-71
lines changed

14 files changed

+697
-71
lines changed

README.md

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ First, clone the repo:
1919
$ git clone https://github.com/ockibagusp/golang-website-example.git
2020
```
2121

22-
### Setting MySQL database
22+
## Setting MySQL database
2323

24-
#### Database
24+
### Database
2525
file: golang-website-example.sql -> new database: golang-website-example
2626

27-
#### Testing
27+
### Testing
2828
file: golang-website-example.sql -> new database: golang-website-example_test
2929

3030
## Users Table
@@ -36,9 +36,38 @@ file: golang-website-example.sql -> new database: golang-website-example_test
3636
| subali | user123 | no |
3737

3838

39+
## Running app
40+
41+
### Compile and run Go program
42+
```
43+
$ go run main.go
44+
```
45+
46+
or,
47+
48+
### Build compiles the packages
49+
50+
```
51+
$ go build
52+
```
53+
54+
- On Linux or Mac:
55+
56+
```
57+
$ ./golang-website-example
58+
```
59+
60+
- On Windows:
61+
62+
```
63+
$ golang-website-example.exe
64+
```
65+
66+
3967
## Router
4068
This using [router](https://github.com/ockibagusp/golang-website-example/blob/master/router/router.go).
4169
70+
4271
## httpexpect: Test for Debug
4372
This using [debug](https://github.com/ockibagusp/golang-website-example/blob/master/tests/main_test.go).
4473
@@ -69,78 +98,54 @@ Optional. Default value @debug: {true} or {1}.
6998
os.Setenv("debug", "0")
7099
```
71100
72-
### Running app
101+
### Test the packages
73102
74-
#### Compile and run Go program
75-
```
76-
$ go run main.go
77-
```
103+
Example,
78104
79-
or,
80-
81-
#### Build compiles the packages
82-
83-
```
84-
$ go build
85-
```
86-
87-
- On Linux or Mac:
88-
89-
```
90-
$ ./golang-website-example
91-
```
92-
93-
- On Windows:
94-
95-
```
96-
$ golang-website-example.exe
97-
```
98-
99-
#### Test the packages
100-
101-
102-
```
105+
```bash
103106
$ go test github.com/ockibagusp/golang-website-example/tests
104107
```
105108

106109
or, verbose output (-v) flag:
107110

111+
```bash
112+
$ go test ./tests -v
108113
```
109-
$ go test github.com/ockibagusp/golang-website-example/tests -v
114+
115+
or, verbose output (-v) select function (-run) flag:
116+
117+
```bash
118+
$ go test ./tests -run=TestHomeController
110119
```
111120

121+
## Air: cloud Live reload for Go apps
122+
[Air](https://github.com/cosmtrek/air) is yet another live-reloading command line utility for Go applications in development.
123+
112124

113125
## TODO List
114-
- Admin Users: delete table User `deleted_at` @route: /users/admin/delete
115-
116-
button:
117-
Restore | Delete Permanently
118-
119-
- Admin user button: delete not for admin
120-
- Admin user search
121-
- mock unit test
122-
- list pagination with next, previous, first and last
123-
- moves files function Server and NewServer, etc.
124-
- Mutex: BankAccount
125-
- docker
126+
- mock
126127
- too much
127128

128129
## Operating System (with me)
129130
### Linux:
130-
- Fedora 35 Workstation
131+
- Fedora 36 Workstation
131132

132133
### Go:
133-
- go1.16.11 linux/amd64
134+
- go1.18.5 linux/amd64
134135

135136
### MySQL:
136137
- mysql Ver 8.0.27 for Linux on x86_64 (Source distribution)
137138

138139

139140
### Bahasa Indonesia
140-
Der Schlaganfall 03.10.2018-heute. Dirilis 7 Januari 2020. Coding ini sedikit lupa. Pun, ini Bahasa Inggris lupa lagi. Perlahan-lahan dari stroke. Aamiin.
141+
Saya sendang berjuang sembuh dari Stroke pada 03 Oktober 2018-hari ini. Coding ini dirilis 7 Januari 2020, ternyata coding sedikit lupa. Kata-katanya dari Bahasa Indonesia sedikit lupa dan Bahasa Inggris kayaknya sulit. Insya Allah, perlahan-lahan sembuh. Aamiin.
142+
143+
Allah itu baik. 🙂
141144

142145
### English (translate[.]google[.]co[.]id)
143-
Stroke: 03 10 2018-today. Released January 7, 2020. This coding is a little forgotten. This is English forgot again. Little by little from stroke. Aamiin.
146+
I'm struggling to recover from a stroke on October 03, 2018-today. This coding was released January 7, 2020, apparently the coding was a little forgotten. The words from Indonesian are a little forgotten and English seems difficult. Insya Allah, slowly recover. Aamiin.
147+
148+
Allah is good. 🙂
144149

145150
---
146151

controllers/admin_controller.go

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
}

controllers/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ type Controller struct {
1313
// New Controller
1414
func New() *Controller {
1515
// PROD or DEV
16-
dbManager := db.Init("PROD")
16+
db_manager := db.Init("PROD")
1717

1818
return &Controller{
19-
DB: dbManager,
19+
DB: db_manager,
2020
}
2121
}

0 commit comments

Comments
 (0)