shopitems instead rooms

This commit is contained in:
2025-03-02 18:49:36 +01:00
parent 48491d0786
commit 6398429c92
11 changed files with 409 additions and 438 deletions

View File

@@ -1,187 +0,0 @@
package controllers
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"example.com/gin/test/models"
"example.com/gin/test/repositories"
)
type CRUDController interface {
Create(*gin.Context)
GetAll(*gin.Context)
GetById(*gin.Context)
Update(*gin.Context)
Delete(*gin.Context)
}
type RoomController interface {
CRUDController
GetUsers(*gin.Context)
AddUser(*gin.Context)
}
type roomController struct {}
func NewRoomController() RoomController {
return &roomController{}
}
func (rc *roomController) GetAll(c *gin.Context) {
rooms, err := repositories.Rooms.GetAll()
if err != nil {
ReplyError(c, fmt.Errorf("Could not query rooms"))
return
}
c.JSON(http.StatusOK, rooms)
}
func (rc *roomController) GetById(c *gin.Context) {
room, err := repositories.Rooms.GetById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Could not query room: %v", err))
return
}
ReplyOK(c, room)
}
func (rc *roomController) Create(c *gin.Context) {
user, exists := c.Get("user")
if !exists {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
room, err := models.NewRoom(c)
room.Admins = append(room.Admins, user.(models.User))
if err != nil {
ReplyError(c, err)
}
_, err = repositories.Rooms.Create(room)
if err != nil {
ReplyError(c, fmt.Errorf("Room creation failed: %s", err))
return
}
//userID := user.(models.User).ID
//rc.DB.Model(&models.Room{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
//if result.Error != nil {
// ReplyError(c, fmt.Errorf("Room creation failed: %s", result.Error))
// return
//}
ReplyOK(c, "Room was created")
}
func (rc *roomController) Update(c *gin.Context) {
roomId, err := strconv.Atoi(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Room with Id '%s' does not exist", c.Param("id")))
return
}
room, err := models.NewRoom(c)
if err != nil {
ReplyError(c, err)
return
}
room.ID = uint(roomId)
_, err = repositories.Rooms.Update(room)
if err != nil {
ReplyError(c, fmt.Errorf("Room creation failed: %s", err))
return
}
ReplyOK(c, "Room was updated")
}
func (rc *roomController) Delete(c *gin.Context) {
err := repositories.Rooms.DeleteById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Room deletion failed: %s", err))
return
}
ReplyOK(c, "Room was deleted")
}
func (rc *roomController) GetUsers(c *gin.Context) {
//only allow room admin
users, err := repositories.Rooms.GetRoomUsersById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Could not get users for room '%s'", c.Param("id")))
return
}
var emails []string
for _, user := range users {
emails = append(emails, user.Email)
}
ReplyOK(c, emails)
}
func (rc *roomController) AddUser(c *gin.Context) {
//only allow room admin
var body struct {
Email string
}
err := c.Bind(&body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Failed to read body",
})
return
}
//lookup requested user
user, err := repositories.Users.GetByEmail(body.Email)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid user",
})
return
}
err = repositories.Rooms.AddRoomUserById(c.Param("id"), user)
if err != nil {
ReplyError(c, fmt.Errorf("Could not add user to room."))
return
}
ReplyOK(c, "Added User to Room")
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
}
func ReplyOK(ctx *gin.Context, message any) {
ctx.JSON(http.StatusOK, message)
}

View File

@@ -0,0 +1,122 @@
package controllers
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"example.com/gin/test/models"
"example.com/gin/test/repositories"
)
type CRUDController interface {
Create(*gin.Context)
GetAll(*gin.Context)
GetById(*gin.Context)
Update(*gin.Context)
Delete(*gin.Context)
}
type ShopItemController interface {
CRUDController
}
type shopItemController struct {}
func NewShopItemController() ShopItemController {
return &shopItemController{}
}
func (rc *shopItemController) GetAll(c *gin.Context) {
shopItems, err := repositories.ShopItems.GetAll()
if err != nil {
ReplyError(c, fmt.Errorf("Could not query shopItems"))
return
}
c.JSON(http.StatusOK, shopItems)
}
func (rc *shopItemController) GetById(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Could not query shopItem: %v", err))
return
}
ReplyOK(c, shopItem)
}
func (rc *shopItemController) Create(c *gin.Context) {
shopItem, err := models.NewShopItem(c)
if err != nil {
ReplyError(c, err)
}
_, err = repositories.ShopItems.Create(shopItem)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
//userID := user.(models.User).ID
//rc.DB.Model(&models.shopItem{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
//if result.Error != nil {
// ReplyError(c, fmt.Errorf("shopItem creation failed: %s", result.Error))
// return
//}
ReplyOK(c, "shopItem was created")
}
func (rc *shopItemController) Update(c *gin.Context) {
shopItemId, err := strconv.Atoi(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("shopItem with Id '%s' does not exist", c.Param("id")))
return
}
shopItem, err := models.NewShopItem(c)
if err != nil {
ReplyError(c, err)
return
}
shopItem.ID = uint(shopItemId)
_, err = repositories.ShopItems.Update(shopItem)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
ReplyOK(c, "shopItem was updated")
}
func (rc *shopItemController) Delete(c *gin.Context) {
err := repositories.ShopItems.DeleteById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("shopItem deletion failed: %s", err))
return
}
ReplyOK(c, "shopItem was deleted")
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
}
func ReplyOK(ctx *gin.Context, message any) {
ctx.JSON(http.StatusOK, message)
}

View File

@@ -3,6 +3,7 @@ package controllers
import(
"fmt"
"net/http"
"math/rand"
"github.com/gin-gonic/gin"
@@ -139,7 +140,7 @@ func (rc *UserController) LoginHandler(c *gin.Context) {
// send it back
//c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("Authorization", tokenString, 3600 * 24, "", "", false, true)
c.HTML(http.StatusPermanentRedirect, "index.html", CreateSessionData(c, gin.H{}))
c.HTML(http.StatusOK, "login.html", CreateSessionData(c, gin.H{}))
}
func CreateSessionData(c *gin.Context, extra any) gin.H {
@@ -187,30 +188,34 @@ func (rc *UserController) RegisterView(c *gin.Context) {
}
func (rc *UserController) ResetView(c *gin.Context) {
rooms, _ := repositories.Rooms.GetAll()
shopItems, _ := repositories.ShopItems.GetAll()
data := gin.H{
"title": "Room Page",
"rooms": rooms,
"title": "shopItem Page",
"shopItems": shopItems,
}
c.HTML(http.StatusOK, "passwordreset.html", data)
}
func (rc *UserController) ResetHandler(c *gin.Context) {
rooms, _ := repositories.Rooms.GetAll()
shopItems, _ := repositories.ShopItems.GetAll()
data := gin.H{
"title": "Room Page",
"rooms": rooms,
"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": "Room Page",
"title": "shopItem Page",
"shopItems": shopItems,
})
fmt.Println(data)
@@ -218,6 +223,69 @@ func (rc *UserController) MainView(c *gin.Context) {
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{})