userhandling #16

Merged
kalipso merged 10 commits from userhandling into master 2025-04-15 01:06:12 +02:00
3 changed files with 53 additions and 5 deletions
Showing only changes of commit cca0b2775c - Show all commits

View File

@@ -2,7 +2,6 @@ package controllers
import (
"fmt"
"math/rand"
"net/http"
"github.com/gin-gonic/gin"
@@ -182,6 +181,38 @@ func (rc *UserController) RegisterView(c *gin.Context) {
c.HTML(http.StatusOK, "register.html", data)
}
func (rc *UserController) InitAdmin(c *gin.Context) {
isEmpty, err := repositories.Users.IsEmpty()
if err != nil {
data := gin.H{
"error": err,
"success": "",
}
c.HTML(http.StatusInternalServerError, "error.html", data)
return
}
if !isEmpty {
data := gin.H{
"error": "Registration is closed",
"success": "",
}
c.HTML(http.StatusInternalServerError, "error.html", data)
return
}
data := gin.H{
"error": "",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
}
func (rc *UserController) ResetView(c *gin.Context) {
shopItems, _ := repositories.ShopItems.GetAll()

View File

@@ -87,6 +87,7 @@ func main() {
//write middleware that redirects to homescreen on register/login/reset for logged in users
viewRoutes.GET("/login", userController.LoginView)
viewRoutes.GET("/logout", userController.Logout)
viewRoutes.GET("/register", userController.InitAdmin)
viewRoutes.GET("/register/:token", userController.RegisterView)
viewRoutes.GET("/passwordreset", userController.ResetView)
viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView)

View File

@@ -1,15 +1,16 @@
package repositories
import(
import (
"gorm.io/gorm"
"git.dynamicdiscord.de/kalipso/zineshop/models"
)
)
type UserRepository interface {
Create(models.User) (models.User, error)
Create(models.User) (models.User, error)
GetByEmail(string) (models.User, error)
GetById(interface{}) (models.User, error)
IsEmpty() (bool, error)
}
type GORMUserRepository struct {
@@ -22,7 +23,7 @@ func NewGORMUserRepository(db *gorm.DB) UserRepository {
}
}
func (u *GORMUserRepository) Create(user models.User) (models.User, error) {
func (u *GORMUserRepository) Create(user models.User) (models.User, error) {
result := u.DB.Create(&user)
if result.Error != nil {
@@ -53,3 +54,18 @@ func (u *GORMUserRepository) GetById(id interface{}) (models.User, error) {
return user, nil
}
func (u *GORMUserRepository) IsEmpty() (bool, error) {
var user models.User
result := u.DB.First(&user)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return true, nil
} else {
return false, result.Error
}
}
return false, nil
}