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

@ -90,9 +90,9 @@ func AccountManage(c *fiber.Ctx) error {
result = db.Save(&user) result = db.Save(&user)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect("/account/manage")
} }
c.Redirect("/account/manage")
} }
} }

View file

@ -307,14 +307,14 @@ func ContactEdit(c *fiber.Ctx) error {
var person models.Person var person models.Person
result := db.Find(&person, "id = ?", id) result := db.Find(&person, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s %s | Modifier contact", "%s %s | Modifier contact",
person.LastName, person.LastName,
@ -549,14 +549,14 @@ func ContactConvert(c *fiber.Ctx) error {
var person models.Person var person models.Person
result := db.Find(&person, "id = ?", id) result := db.Find(&person, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
person.IsContact = false person.IsContact = false
person.IsMember = true person.IsMember = true

View file

@ -43,14 +43,14 @@ func FieldShow(c *fiber.Ctx) error {
var field models.Field var field models.Field
result := db.Preload("List").Find(&field, "id = ?", id) result := db.Preload("List").Find(&field, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Champs supplémentaires", "%s | Champs supplémentaires",
field.Name, field.Name,
@ -115,12 +115,12 @@ func FieldAdd(c *fiber.Ctx) error {
result := db.Create(&field) result := db.Create(&field)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/fields/%d",
field.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/fields/%d",
field.ID,
))
} }
} }
@ -145,14 +145,14 @@ func FieldEdit(c *fiber.Ctx) error {
var field models.Field var field models.Field
result := db.Find(&field, "id = ?", id) result := db.Find(&field, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Modifier un champ", "%s | Modifier un champ",
field.Name, field.Name,
@ -169,12 +169,12 @@ func FieldEdit(c *fiber.Ctx) error {
result := db.Save(&field) result := db.Save(&field)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/fields/%d",
field.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/fields/%d",
field.ID,
))
} }
} }

View file

@ -40,14 +40,14 @@ func ListShow(c *fiber.Ctx) error {
var list models.List var list models.List
result := db.Find(&list, "id = ?", id) result := db.Find(&list, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Listes", "%s | Listes",
list.Name, list.Name,
@ -90,12 +90,12 @@ func ListAdd(c *fiber.Ctx) error {
result := db.Create(&list) result := db.Create(&list)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
} }
@ -117,14 +117,14 @@ func ListEdit(c *fiber.Ctx) error {
var list models.List var list models.List
result := db.Find(&list, "id = ?", id) result := db.Find(&list, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Modifier liste", "%s | Modifier liste",
list.Name, list.Name,
@ -148,29 +148,29 @@ func ListEdit(c *fiber.Ctx) error {
} }
if len(errors) == 0 { if len(errors) == 0 {
result := db.Save(&list) result = db.Save(&list)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
if !list.Multi && multiOriginal {
var listItems []models.ListItem
result2 := db.Find(&listItems,
"list_id = ? AND `default` = ?",
list.ID, true,
)
if result2.RowsAffected > 1 {
db.Model(&models.ListItem{}).Where(
"list_id = ?", list.ID,
).Update("default", false)
}
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
if !list.Multi && multiOriginal {
var listItems []models.ListItem
result = db.Find(&listItems,
"list_id = ? AND `default` = ?",
list.ID, true,
)
if result.RowsAffected > 1 {
db.Model(&models.ListItem{}).Where(
"list_id = ?", list.ID,
).Update("default", false)
}
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
} }
@ -208,14 +208,14 @@ func ListItemAdd(c *fiber.Ctx) error {
var list models.List var list models.List
result := db.Find(&list, "id = ?", id) result := db.Find(&list, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Ajouter un élément à la liste", "%s | Ajouter un élément à la liste",
list.Name, list.Name,
@ -244,19 +244,19 @@ func ListItemAdd(c *fiber.Ctx) error {
result := db.Create(&listItem) result := db.Create(&listItem)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?",
list.ID, listItem.ID,
).Update("default", false)
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?",
list.ID, listItem.ID,
).Update("default", false)
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
} }
@ -280,25 +280,25 @@ func ListItemEdit(c *fiber.Ctx) error {
var list models.List var list models.List
result := db.Find(&list, "id = ?", id) result := db.Find(&list, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if result.Error != nil {
return result.Error
}
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found") return fiber.NewError(fiber.StatusNotFound, "Not found")
} }
var listItem models.ListItem
result = db.Find(&listItem, "id = ?", itemid)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
var listItem models.ListItem if result.RowsAffected < 1 {
result2 := db.Find(&listItem, "id = ?", itemid)
if errors.Is(result2.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found") return fiber.NewError(fiber.StatusNotFound, "Not found")
} }
if result2.Error != nil {
return result2.Error
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Modifier un élément de la liste", "%s | Modifier un élément de la liste",
list.Name, list.Name,
@ -320,22 +320,22 @@ func ListItemEdit(c *fiber.Ctx) error {
} }
if len(errors) == 0 { if len(errors) == 0 {
result3 := db.Save(&listItem) result = db.Save(&listItem)
if result3.Error != nil { if result.Error != nil {
return result3.Error return result.Error
} else {
if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?",
list.ID, listItem.ID,
).Update("default", false)
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?",
list.ID, listItem.ID,
).Update("default", false)
}
c.Redirect(fmt.Sprintf(
"/admin/lists/%d",
list.ID,
))
} }
} }
@ -359,17 +359,17 @@ func ListItemDelete(c *fiber.Ctx) error {
var list models.List var list models.List
result := db.Find(&list, "id = ?", id) result := db.Find(&list, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
result2 := db.Delete(&models.ListItem{}, itemid) if result.RowsAffected < 1 {
if result2.Error != nil { return fiber.NewError(fiber.StatusNotFound, "Not found")
return result2.Error }
result = db.Delete(&models.ListItem{}, itemid)
if result.Error != nil {
return result.Error
} }
return c.Redirect(fmt.Sprintf( return c.Redirect(fmt.Sprintf(

View file

@ -40,14 +40,14 @@ func RoleShow(c *fiber.Ctx) error {
var role models.Role var role models.Role
result := db.Find(&role, "id = ?", id) result := db.Find(&role, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Rôles", "%s | Rôles",
role.Name, role.Name,
@ -100,12 +100,12 @@ func RoleAdd(c *fiber.Ctx) error {
result := db.Create(&role) result := db.Create(&role)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/roles/%d",
role.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/roles/%d",
role.ID,
))
} }
} }
@ -127,14 +127,14 @@ func RoleEdit(c *fiber.Ctx) error {
var role models.Role var role models.Role
result := db.Find(&role, "id = ?", id) result := db.Find(&role, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Modifier rôle", "%s | Modifier rôle",
role.Name, role.Name,
@ -173,12 +173,12 @@ func RoleEdit(c *fiber.Ctx) error {
result := db.Save(&role) result := db.Save(&role)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/roles/%d",
role.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/roles/%d",
role.ID,
))
} }
} }

View file

@ -41,14 +41,14 @@ func SectionShow(c *fiber.Ctx) error {
var section models.Section var section models.Section
result := db.Preload("ParentSection").Find(&section, "id = ?", id) result := db.Preload("ParentSection").Find(&section, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
title := fmt.Sprintf( title := fmt.Sprintf(
"%s | Sections", "%s | Sections",
section.Name, section.Name,
@ -131,12 +131,12 @@ func SectionAdd(c *fiber.Ctx) error {
result := db.Create(&section) result := db.Create(&section)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
} }
} }
@ -159,14 +159,14 @@ func SectionEdit(c *fiber.Ctx) error {
var section models.Section var section models.Section
result := db.Find(&section, "id = ?", id) result := db.Find(&section, "id = ?", id)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }
if result.RowsAffected < 1 {
return fiber.NewError(fiber.StatusNotFound, "Not found")
}
var childSections []models.Section var childSections []models.Section
db.Find(&childSections, "parent_section_id = ?", section.ID) db.Find(&childSections, "parent_section_id = ?", section.ID)
isParent := (len(childSections) > 0) isParent := (len(childSections) > 0)
@ -225,12 +225,12 @@ func SectionEdit(c *fiber.Ctx) error {
result := db.Save(&section) result := db.Save(&section)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} else {
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
} }
c.Redirect(fmt.Sprintf(
"/admin/sections/%d",
section.ID,
))
} }
} }

View file

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

View file

@ -26,6 +26,10 @@ func FirstAccountCheck() (bool, error) {
return false, result.Error return false, result.Error
} }
if result.RowsAffected < 1 {
return false, nil
}
return true, nil return true, nil
} }
@ -72,6 +76,10 @@ func UserExistsAndIsActive(id uint) (bool, error) {
return false, result.Error return false, result.Error
} }
if result.RowsAffected < 1 {
return false, nil
}
return true, nil return true, nil
} }

View file

@ -37,7 +37,7 @@ func SavedSessionMiddleware(c *fiber.Ctx) error {
time.Now(), time.Now(),
) )
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) || result.RowsAffected < 1 {
c.ClearCookie("saved-session-uuid") c.ClearCookie("saved-session-uuid")
c.ClearCookie("saved-session-secret") c.ClearCookie("saved-session-secret")