104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.dynamicdiscord.de/malobeo/portal/services"
|
|
)
|
|
|
|
type AccessAuthController struct{}
|
|
|
|
func (gc *AccessAuthController) AccessAuthView(c *gin.Context) {
|
|
accessAuths, err := services.AccessAuths.GetAll(c)
|
|
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"data": gin.H{"error": err}})
|
|
}
|
|
|
|
data := CreateSessionData(c, gin.H{
|
|
"accessAuths": accessAuths,
|
|
})
|
|
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", data)
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "accessauths.html", data)
|
|
|
|
}
|
|
|
|
func (gc *AccessAuthController) AddAccessAuthHandler(c *gin.Context) {
|
|
name := c.PostForm("name")
|
|
|
|
if len(name) == 0 {
|
|
fmt.Println("Adding accessAuth with empty name is forbidden")
|
|
c.HTML(http.StatusBadRequest, "accessAuths.html", gin.H{"error": "Cant create accessAuth without name"})
|
|
return
|
|
}
|
|
|
|
_, err := services.AccessAuths.Create(c, name)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
gc.AccessAuthView(c)
|
|
}
|
|
|
|
func (gc *AccessAuthController) AccessAuthHandler(c *gin.Context) {
|
|
action := c.PostForm("action")
|
|
idStr := c.Param("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
if action == "createTimetable" {
|
|
weekdayStr := c.PostForm("weekday")
|
|
starttime := c.PostForm("starttime")
|
|
durationStr := c.PostForm("duration")
|
|
duration, err := strconv.Atoi(durationStr)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
weekday, err := strconv.Atoi(weekdayStr)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
_, err = services.AccessAuths.AddTimetable(c, int32(id), int32(weekday), starttime, int32(duration))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
if action == "delete" {
|
|
err := services.AccessAuths.Delete(c, int32(id))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
gc.AccessAuthView(c)
|
|
}
|