Skip to content

Commit b7f5a69

Browse files
authored
Merge pull request #3 from ockibagusp/session-3-Is-types
User tables
2 parents c404c7b + 441547e commit b7f5a69

24 files changed

+1581
-450
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ file: golang-website-example.sql -> new database: golang-website-example_test
2626

2727
## User Tables
2828

29-
| Username | Password |
30-
| --- | --- |
31-
| ockibagusp | user123 |
32-
| sugriwa | user123 |
33-
| subali | user123 |
29+
| Username | Password | is Admin |
30+
| --- | --- | --- |
31+
| admin | admin123 | yes |
32+
| sugriwa | user123 | no |
33+
| subali | user123 | no |
3434

3535

3636
## Router
@@ -108,7 +108,6 @@ $ go test github.com/ockibagusp/golang-website-example/test -v
108108
109109
## TODO List
110110
- mock unit test
111-
- session: IsAdmin, IsUser and IsAuth
112111
- list pagination with next, previous, first and last
113112
- Mutex: BankAccount
114113
- too much

controllers/home_controller.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package controllers
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/labstack/echo/v4"
78
"github.com/ockibagusp/golang-website-example/middleware"
9+
"github.com/ockibagusp/golang-website-example/models"
810
log "github.com/sirupsen/logrus"
911
)
1012

@@ -15,7 +17,7 @@ import (
1517
* @method: GET
1618
* @route: /
1719
*/
18-
func (Controller) Home(c echo.Context) error {
20+
func (controller Controller) Home(c echo.Context) error {
1921
// Please note the the second parameter "home.html" is the template name and should
2022
// be equal to one of the keys in the TemplateRegistry array defined in main.go
2123
session, _ := middleware.GetAuth(c)
@@ -24,12 +26,27 @@ func (Controller) Home(c echo.Context) error {
2426
"route": c.Path(),
2527
})
2628
log.Info("START request method GET for home")
27-
// ---
29+
30+
var message string
31+
if session.Values["username"] != "" {
32+
var user models.User
33+
if err := controller.DB.Select("name").Where(
34+
"username = ?", session.Values["username"],
35+
).First(&user); err.Error != nil { // TODO: why?
36+
log.Warnf(`session values "username" error: %v`, err.Error)
37+
}
38+
39+
message = fmt.Sprintf("%v!", user.Name)
40+
// or,
41+
// session.Values["username"].(string) + "!"
42+
}
43+
2844
log.Info("END request method GET for home: [+]success")
2945
return c.Render(http.StatusOK, "home.html", echo.Map{
30-
"name": "Home",
31-
"nav": "home", // (?)
32-
"session": session,
33-
"msg": "Ocki Bagus Pratama!",
46+
"name": "Home",
47+
"nav": "home", // (?)
48+
"session": session,
49+
"flash_success": middleware.GetFlashSuccess(c),
50+
"msg": message,
3451
})
3552
}

controllers/session_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (controller *Controller) Login(c echo.Context) error {
5252

5353
var user models.User
5454
// err := controller.DB.Select(...).Where(...).First(...).Error
55-
if err := controller.DB.Select("username", "password").Where(
55+
if err := controller.DB.Select("username", "password", "is_admin").Where(
5656
"username = ?", passwordForm.Username,
5757
).First(&user).Error; err != nil {
5858
middleware.SetFlashError(c, err.Error())

0 commit comments

Comments
 (0)