diff --git a/controllers/groupController.go b/controllers/groupController.go new file mode 100644 index 0000000..9948848 --- /dev/null +++ b/controllers/groupController.go @@ -0,0 +1,51 @@ +package controllers + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + + "git.dynamicdiscord.de/malobeo/portal/services" +) + +type GroupController struct{} + +func (gc *GroupController) GroupView(c *gin.Context) { + groups, err := services.Groups.GetAll(c) + + if err != nil { + c.HTML(http.StatusBadRequest, "groups.html", gin.H{"data": gin.H{"error": err}}) + } + + data := CreateSessionData(c, gin.H{ + "groups": groups, + }) + + if err != nil { + c.HTML(http.StatusBadRequest, "groups.html", data) + } + + c.HTML(http.StatusOK, "groups.html", data) + +} + +func (gc *GroupController) AddGroupHandler(c *gin.Context) { + name := c.PostForm("name") + + if len(name) == 0 { + fmt.Println("Adding group with empty name is forbidden") + c.HTML(http.StatusBadRequest, "groups.html", gin.H{"error": "Cant create group without name"}) + return + } + + err := services.Groups.Create(c, name) + + if err != nil { + fmt.Println(err) + c.HTML(http.StatusBadRequest, "groups.html", gin.H{"error": err}) + return + } + + gc.GroupView(c) +} diff --git a/main.go b/main.go index 04e9f40..4b10a11 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,9 @@ import ( ) var ( - userController controllers.UserController = controllers.UserController{} - authValidator middlewares.AuthValidator = middlewares.AuthValidator{} + userController controllers.UserController = controllers.UserController{} + groupController controllers.GroupController = controllers.GroupController{} + authValidator middlewares.AuthValidator = middlewares.AuthValidator{} ) func LoadEnvVariables() { @@ -49,11 +50,15 @@ func main() { server.Static("/static", os.Getenv("STATIC")) server.LoadHTMLGlob(fmt.Sprintf("%s/*.html", os.Getenv("VIEWS"))) - viewRoutes := server.Group("/", authValidator.OptionalAuth) + viewRoutes := server.Group("/") { viewRoutes.GET("/login", userController.LoginView) viewRoutes.POST("/login", userController.LoginHandler) + viewRoutes.GET("/groups", authValidator.RequireAdmin, groupController.GroupView) + viewRoutes.POST("/groups", authValidator.RequireAdmin, groupController.AddGroupHandler) + //viewRoutes.POST("/groups/:id", authValidator.RequireAdmin, groupController.GroupHandler) + //write middleware that redirects to homescreen on register/login/reset for logged in users //viewRoutes.GET("/login", userController.LoginView) //viewRoutes.GET("/logout", userController.Logout) diff --git a/views/groups.html b/views/groups.html new file mode 100644 index 0000000..e0b00ef --- /dev/null +++ b/views/groups.html @@ -0,0 +1,50 @@ +{{ template "header.html" . }} + + +
+
+ Your Company +

Edit Groups

+
+ +
+
+ {{ range .data.groups }} +
+
+
+ {{ .Name }} +
+
+
+ + +
+
+
+ + +
+
+
+ {{ end }} +
+
+
+
+ Create New Group +
+
+
+ + +
+
+
+ +
+
+
+
+{{ template "footer.html" . }} +