Fix 404 & clean code

This commit is contained in:
William Bouzourène 2025-01-20 15:40:03 +01:00
parent 1dc135f8f9
commit 1d390cf7ff
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
9 changed files with 192 additions and 184 deletions

View file

@ -48,14 +48,14 @@ func UserShow(c *fiber.Ctx) error {
var user models.User
result := db.Find(&user, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf(
"%s | Utilisateurs",
user.Name,
@ -132,12 +132,12 @@ func UserAdd(c *fiber.Ctx) error {
result = db.Create(&user)
if result.Error != nil {
return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
}
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
}
}
@ -159,14 +159,14 @@ func UserEdit(c *fiber.Ctx) error {
var user models.User
result := db.Find(&user, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf(
"%s | Modifier utilisateur",
user.Name,
@ -240,15 +240,15 @@ func UserEdit(c *fiber.Ctx) error {
}
if len(errors) == 0 {
result2 := db.Save(&user)
if result2.Error != nil {
return result2.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
result = db.Save(&user)
if result.Error != nil {
return result.Error
}
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
}
}
@ -270,14 +270,14 @@ func UserPermissions(c *fiber.Ctx) error {
var user models.User
result := db.Find(&user, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf(
"%s | Permissions utilisateur",
user.Name,
@ -325,15 +325,15 @@ func UserPermissions(c *fiber.Ctx) error {
}
if len(errors) == 0 {
result2 := db.Save(&user)
if result2.Error != nil {
return result2.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
result = db.Save(&user)
if result.Error != nil {
return result.Error
}
c.Redirect(fmt.Sprintf(
"/admin/users/%d",
user.ID,
))
}
}
@ -386,14 +386,14 @@ func UserDelete(c *fiber.Ctx) error {
user.IsAdmin = false
user.SkipWelcome = false
result2 := db.Save(&user)
if result2.Error != nil {
return result2.Error
result = db.Save(&user)
if result.Error != nil {
return result.Error
}
result3 := db.Delete(&models.User{}, id)
if result3.Error != nil {
return result3.Error
result = db.Delete(&models.User{}, id)
if result.Error != nil {
return result.Error
}
return c.Redirect("/admin/users")