Fix 404 & clean code

This commit is contained in:
William Bouzourène 2025-01-20 15:40:03 +01:00
parent 6b27dd545a
commit 68edf0a76f
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,14 +115,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/fields/%d", "/admin/fields/%d",
field.ID, field.ID,
)) ))
} }
} }
}
return c.Render("field_form", fiber.Map{ return c.Render("field_form", fiber.Map{
"PageTitle": "Ajouter un champ", "PageTitle": "Ajouter un champ",
@ -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,14 +169,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/fields/%d", "/admin/fields/%d",
field.ID, field.ID,
)) ))
} }
} }
}
return c.Render("field_form", fiber.Map{ return c.Render("field_form", fiber.Map{
"PageTitle": title, "PageTitle": title,

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,14 +90,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/lists/%d", "/admin/lists/%d",
list.ID, list.ID,
)) ))
} }
} }
}
return c.Render("list_form", fiber.Map{ return c.Render("list_form", fiber.Map{
"PageTitle": "Ajouter une liste", "PageTitle": "Ajouter une liste",
@ -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,18 +148,19 @@ 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 { if !list.Multi && multiOriginal {
var listItems []models.ListItem var listItems []models.ListItem
result2 := db.Find(&listItems, result = db.Find(&listItems,
"list_id = ? AND `default` = ?", "list_id = ? AND `default` = ?",
list.ID, true, list.ID, true,
) )
if result2.RowsAffected > 1 { if result.RowsAffected > 1 {
db.Model(&models.ListItem{}).Where( db.Model(&models.ListItem{}).Where(
"list_id = ?", list.ID, "list_id = ?", list.ID,
).Update("default", false) ).Update("default", false)
@ -172,7 +173,6 @@ func ListEdit(c *fiber.Ctx) error {
)) ))
} }
} }
}
return c.Render("list_form", fiber.Map{ return c.Render("list_form", fiber.Map{
"PageTitle": title, "PageTitle": title,
@ -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,7 +244,8 @@ 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 { if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where( db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?", "list_id = ? AND id <> ?",
@ -258,7 +259,6 @@ func ListItemAdd(c *fiber.Ctx) error {
)) ))
} }
} }
}
return c.Render("listitem_form", fiber.Map{ return c.Render("listitem_form", fiber.Map{
"PageTitle": title, "PageTitle": title,
@ -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,10 +320,11 @@ 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 { if listItem.Default && !list.Multi {
db.Model(&models.ListItem{}).Where( db.Model(&models.ListItem{}).Where(
"list_id = ? AND id <> ?", "list_id = ? AND id <> ?",
@ -337,7 +338,6 @@ func ListItemEdit(c *fiber.Ctx) error {
)) ))
} }
} }
}
return c.Render("listitem_form", fiber.Map{ return c.Render("listitem_form", fiber.Map{
"PageTitle": title, "PageTitle": title,
@ -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,14 +100,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/roles/%d", "/admin/roles/%d",
role.ID, role.ID,
)) ))
} }
} }
}
return c.Render("role_form", fiber.Map{ return c.Render("role_form", fiber.Map{
"PageTitle": "Ajouter un rôle", "PageTitle": "Ajouter un rôle",
@ -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,14 +173,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/roles/%d", "/admin/roles/%d",
role.ID, role.ID,
)) ))
} }
} }
}
return c.Render("role_form", fiber.Map{ return c.Render("role_form", fiber.Map{
"PageTitle": title, "PageTitle": title,

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,14 +131,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/sections/%d", "/admin/sections/%d",
section.ID, section.ID,
)) ))
} }
} }
}
return c.Render("section_form", fiber.Map{ return c.Render("section_form", fiber.Map{
"PageTitle": "Ajouter une section", "PageTitle": "Ajouter une section",
@ -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,14 +225,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/sections/%d", "/admin/sections/%d",
section.ID, section.ID,
)) ))
} }
} }
}
return c.Render("section_form", fiber.Map{ return c.Render("section_form", fiber.Map{
"PageTitle": title, "PageTitle": title,

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,14 +132,14 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/users/%d", "/admin/users/%d",
user.ID, user.ID,
)) ))
} }
} }
}
return c.Render("user_form", fiber.Map{ return c.Render("user_form", fiber.Map{
"PageTitle": "Ajouter un utilisateur", "PageTitle": "Ajouter un utilisateur",
@ -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,17 +240,17 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/users/%d", "/admin/users/%d",
user.ID, user.ID,
)) ))
} }
} }
}
return c.Render("user_form", fiber.Map{ return c.Render("user_form", fiber.Map{
"PageTitle": title, "PageTitle": title,
@ -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,17 +325,17 @@ 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( c.Redirect(fmt.Sprintf(
"/admin/users/%d", "/admin/users/%d",
user.ID, user.ID,
)) ))
} }
} }
}
var userRoles []models.UserRole var userRoles []models.UserRole
db.Find(&userRoles, "user_id = ?", id) db.Find(&userRoles, "user_id = ?", 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")