287 lines
5.6 KiB
Go
287 lines
5.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"example.com/gin/test/models"
|
|
"example.com/gin/test/repositories"
|
|
"example.com/gin/test/services"
|
|
)
|
|
|
|
type UserController struct{}
|
|
|
|
func NewUserController() UserController {
|
|
return UserController{}
|
|
}
|
|
|
|
func (uc *UserController) Register(c *gin.Context) {
|
|
//Get the email/passwd off req body
|
|
var body struct {
|
|
Name string
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
err := c.Bind(&body)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Failed to read body",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
_, err = services.Users.Register(body.Name, body.Email, body.Password)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error: ", err)
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Failed to create user",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
//respond
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
}
|
|
|
|
func (uc *UserController) Login(c *gin.Context) {
|
|
//Get the email/passwd off req body
|
|
var body struct {
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
err := c.Bind(&body)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Login Failed",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
tokenString, err := services.Users.Login(body.Email, body.Password)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Login Failed",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
// send it back
|
|
c.SetSameSite(http.SameSiteLaxMode)
|
|
c.SetCookie("Authorization", tokenString, 3600*24, "", "", false, true)
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
}
|
|
|
|
func (uc *UserController) Validate(c *gin.Context) {
|
|
user, exists := c.Get("user")
|
|
|
|
if exists {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": fmt.Sprintf("Logged in with userID: %d", user.(models.User).ID),
|
|
})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Currently not logged in.",
|
|
})
|
|
}
|
|
}
|
|
|
|
func (rc *UserController) LoginView(c *gin.Context) {
|
|
//if already logged in
|
|
_, exists := c.Get("user")
|
|
if exists {
|
|
c.HTML(http.StatusOK, "index.html", CreateSessionData(c, gin.H{}))
|
|
return
|
|
}
|
|
|
|
data := gin.H{
|
|
"error": "",
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "login.html", CreateSessionData(c, data))
|
|
}
|
|
|
|
func (rc *UserController) LoginHandler(c *gin.Context) {
|
|
email := c.PostForm("email")
|
|
password := c.PostForm("password")
|
|
|
|
tokenString, err := services.Users.Login(email, password)
|
|
|
|
if err != nil {
|
|
data := gin.H{
|
|
"error": "Login Failed. Wrong Email or Password!",
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "login.html", data)
|
|
return
|
|
}
|
|
|
|
//set this so that CreateSessionData works
|
|
//otherwise header would not be generated correctly
|
|
user, _ := repositories.Users.GetByEmail(email)
|
|
c.Set("user", user)
|
|
|
|
// send it back
|
|
//c.SetSameSite(http.SameSiteLaxMode)
|
|
c.SetCookie("Authorization", tokenString, 3600*24, "", "", false, true)
|
|
c.HTML(http.StatusOK, "login.html", CreateSessionData(c, gin.H{}))
|
|
}
|
|
|
|
func CreateSessionData(c *gin.Context, extra any) gin.H {
|
|
_, exists := c.Get("user")
|
|
|
|
return gin.H{
|
|
"loggedIn": exists,
|
|
"data": extra,
|
|
}
|
|
}
|
|
|
|
func (rc *UserController) RegisterHandler(c *gin.Context) {
|
|
name := c.PostForm("name")
|
|
email := c.PostForm("email")
|
|
password := c.PostForm("password")
|
|
|
|
_, err := services.Users.Register(name, email, password)
|
|
|
|
if err != nil {
|
|
data := gin.H{
|
|
"error": "Registering Failed.",
|
|
"success": "",
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "register.html", data)
|
|
return
|
|
}
|
|
|
|
data := gin.H{
|
|
"error": "",
|
|
"success": "You successfully registered. Try logging in.",
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "register.html", data)
|
|
}
|
|
|
|
func (rc *UserController) RegisterView(c *gin.Context) {
|
|
data := gin.H{
|
|
"error": "",
|
|
"success": "",
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "register.html", data)
|
|
}
|
|
|
|
func (rc *UserController) ResetView(c *gin.Context) {
|
|
shopItems, _ := repositories.ShopItems.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "shopItem Page",
|
|
"shopItems": shopItems,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "passwordreset.html", data)
|
|
}
|
|
|
|
func (rc *UserController) ResetHandler(c *gin.Context) {
|
|
shopItems, _ := repositories.ShopItems.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "shopItem Page",
|
|
"shopItems": shopItems,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "passwordreset.html", data)
|
|
}
|
|
|
|
func (rc *UserController) MainView(c *gin.Context) {
|
|
shopItems, _ := repositories.ShopItems.GetAll()
|
|
fmt.Println(len(shopItems))
|
|
|
|
data := CreateSessionData(c, gin.H{
|
|
"title": "shopItem Page",
|
|
"shopItems": shopItems,
|
|
})
|
|
|
|
fmt.Println(data)
|
|
|
|
c.HTML(http.StatusOK, "index.html", data)
|
|
}
|
|
|
|
type booking struct {
|
|
Booked bool
|
|
}
|
|
|
|
type calendarbooking struct {
|
|
Time string
|
|
Bookings []booking
|
|
}
|
|
|
|
func (rc *UserController) CalendarView(c *gin.Context) {
|
|
shopItems, _ := repositories.ShopItems.GetAll()
|
|
fmt.Println(len(shopItems))
|
|
|
|
generateBookings := func(amountShopItems int) []calendarbooking {
|
|
var result []calendarbooking
|
|
time := 6
|
|
for _ = range 18 {
|
|
book := calendarbooking{
|
|
Time: fmt.Sprintf("%d:00", time),
|
|
Bookings: []booking{},
|
|
}
|
|
for _ = range amountShopItems {
|
|
book.Bookings = append(book.Bookings, booking{Booked: rand.Float32() < 0.5})
|
|
}
|
|
|
|
time += 1
|
|
result = append(result, book)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
bookings := gin.H{
|
|
"head": []string{
|
|
"malobeo",
|
|
"hole of fame",
|
|
"BK",
|
|
"AZ Conni",
|
|
},
|
|
"bookings": generateBookings(4),
|
|
//"bookings": []calendarbooking{
|
|
// {
|
|
// Time: "10:00",
|
|
// Bookings: []booking{
|
|
// { Booked: true },
|
|
// { Booked: false },
|
|
// },
|
|
// },
|
|
//},
|
|
}
|
|
|
|
data := CreateSessionData(c, gin.H{
|
|
"title": "shopItem Page",
|
|
"bookings": bookings,
|
|
"shopItemcount": len(bookings["head"].([]string)) + 1,
|
|
})
|
|
|
|
fmt.Println(data)
|
|
|
|
c.HTML(http.StatusOK, "calendar.html", data)
|
|
}
|
|
|
|
func (rc *UserController) Logout(c *gin.Context) {
|
|
c.SetCookie("Authorization", "", -1, "", "", false, true)
|
|
c.HTML(http.StatusOK, "index.html", gin.H{})
|
|
}
|