From 1d390cf7ff747cd3e06fd921c36a5d8fa54dcca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Bouzour=C3=A8ne?= Date: Mon, 20 Jan 2025 15:40:03 +0100 Subject: [PATCH] Fix 404 & clean code --- controllers/account.go | 4 +- controllers/contacts.go | 16 ++-- controllers/fields.go | 36 ++++---- controllers/lists.go | 160 +++++++++++++++++------------------ controllers/roles.go | 36 ++++---- controllers/sections.go | 36 ++++---- controllers/users.go | 78 ++++++++--------- helpers/users.go | 8 ++ middlewares/saved_session.go | 2 +- 9 files changed, 192 insertions(+), 184 deletions(-) diff --git a/controllers/account.go b/controllers/account.go index 3f1c931..ba1b289 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -90,9 +90,9 @@ func AccountManage(c *fiber.Ctx) error { result = db.Save(&user) if result.Error != nil { return result.Error - } else { - c.Redirect("/account/manage") } + + c.Redirect("/account/manage") } } diff --git a/controllers/contacts.go b/controllers/contacts.go index 1cf623e..9ba18fe 100644 --- a/controllers/contacts.go +++ b/controllers/contacts.go @@ -307,14 +307,14 @@ func ContactEdit(c *fiber.Ctx) error { var person models.Person result := db.Find(&person, "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 %s | Modifier contact", person.LastName, @@ -549,14 +549,14 @@ func ContactConvert(c *fiber.Ctx) error { var person models.Person result := db.Find(&person, "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") + } + person.IsContact = false person.IsMember = true diff --git a/controllers/fields.go b/controllers/fields.go index 3a3bdfb..8012765 100644 --- a/controllers/fields.go +++ b/controllers/fields.go @@ -43,14 +43,14 @@ func FieldShow(c *fiber.Ctx) error { var field models.Field 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 { return result.Error } + if result.RowsAffected < 1 { + return fiber.NewError(fiber.StatusNotFound, "Not found") + } + title := fmt.Sprintf( "%s | Champs supplémentaires", field.Name, @@ -115,12 +115,12 @@ func FieldAdd(c *fiber.Ctx) error { result := db.Create(&field) if result.Error != nil { 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 result := db.Find(&field, "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 un champ", field.Name, @@ -169,12 +169,12 @@ func FieldEdit(c *fiber.Ctx) error { result := db.Save(&field) if result.Error != nil { return result.Error - } else { - c.Redirect(fmt.Sprintf( - "/admin/fields/%d", - field.ID, - )) } + + c.Redirect(fmt.Sprintf( + "/admin/fields/%d", + field.ID, + )) } } diff --git a/controllers/lists.go b/controllers/lists.go index 7329312..a947723 100644 --- a/controllers/lists.go +++ b/controllers/lists.go @@ -40,14 +40,14 @@ func ListShow(c *fiber.Ctx) error { var list models.List result := db.Find(&list, "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 | Listes", list.Name, @@ -90,12 +90,12 @@ func ListAdd(c *fiber.Ctx) error { result := db.Create(&list) if result.Error != nil { 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 result := db.Find(&list, "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 liste", list.Name, @@ -148,29 +148,29 @@ func ListEdit(c *fiber.Ctx) error { } if len(errors) == 0 { - result := db.Save(&list) + result = db.Save(&list) if result.Error != nil { 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 result := db.Find(&list, "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 | Ajouter un élément à la liste", list.Name, @@ -244,19 +244,19 @@ func ListItemAdd(c *fiber.Ctx) error { result := db.Create(&listItem) if result.Error != nil { 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 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") } + var listItem models.ListItem + result = db.Find(&listItem, "id = ?", itemid) + if result.Error != nil { return result.Error } - var listItem models.ListItem - result2 := db.Find(&listItem, "id = ?", itemid) - - if errors.Is(result2.Error, gorm.ErrRecordNotFound) { + if result.RowsAffected < 1 { return fiber.NewError(fiber.StatusNotFound, "Not found") } - if result2.Error != nil { - return result2.Error - } - title := fmt.Sprintf( "%s | Modifier un élément de la liste", list.Name, @@ -320,22 +320,22 @@ func ListItemEdit(c *fiber.Ctx) error { } if len(errors) == 0 { - result3 := db.Save(&listItem) - if result3.Error != nil { - return result3.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, - )) + result = db.Save(&listItem) + if result.Error != nil { + return result.Error } + + 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 result := db.Find(&list, "id = ?", id) - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return fiber.NewError(fiber.StatusNotFound, "Not found") - } - if result.Error != nil { return result.Error } - result2 := db.Delete(&models.ListItem{}, itemid) - if result2.Error != nil { - return result2.Error + if result.RowsAffected < 1 { + return fiber.NewError(fiber.StatusNotFound, "Not found") + } + + result = db.Delete(&models.ListItem{}, itemid) + if result.Error != nil { + return result.Error } return c.Redirect(fmt.Sprintf( diff --git a/controllers/roles.go b/controllers/roles.go index bf5b056..575675a 100644 --- a/controllers/roles.go +++ b/controllers/roles.go @@ -40,14 +40,14 @@ func RoleShow(c *fiber.Ctx) error { var role models.Role result := db.Find(&role, "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 | Rôles", role.Name, @@ -100,12 +100,12 @@ func RoleAdd(c *fiber.Ctx) error { result := db.Create(&role) if result.Error != nil { 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 result := db.Find(&role, "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 rôle", role.Name, @@ -173,12 +173,12 @@ func RoleEdit(c *fiber.Ctx) error { result := db.Save(&role) if result.Error != nil { return result.Error - } else { - c.Redirect(fmt.Sprintf( - "/admin/roles/%d", - role.ID, - )) } + + c.Redirect(fmt.Sprintf( + "/admin/roles/%d", + role.ID, + )) } } diff --git a/controllers/sections.go b/controllers/sections.go index 2c857c6..4c13af9 100644 --- a/controllers/sections.go +++ b/controllers/sections.go @@ -41,14 +41,14 @@ func SectionShow(c *fiber.Ctx) error { var section models.Section result := db.Preload("ParentSection").Find(§ion, "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 | Sections", section.Name, @@ -131,12 +131,12 @@ func SectionAdd(c *fiber.Ctx) error { result := db.Create(§ion) if result.Error != nil { 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 result := db.Find(§ion, "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") + } + var childSections []models.Section db.Find(&childSections, "parent_section_id = ?", section.ID) isParent := (len(childSections) > 0) @@ -225,12 +225,12 @@ func SectionEdit(c *fiber.Ctx) error { result := db.Save(§ion) if result.Error != nil { return result.Error - } else { - c.Redirect(fmt.Sprintf( - "/admin/sections/%d", - section.ID, - )) } + + c.Redirect(fmt.Sprintf( + "/admin/sections/%d", + section.ID, + )) } } diff --git a/controllers/users.go b/controllers/users.go index e1fcc26..685ed44 100644 --- a/controllers/users.go +++ b/controllers/users.go @@ -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") diff --git a/helpers/users.go b/helpers/users.go index 58c6f73..9ebcb6d 100644 --- a/helpers/users.go +++ b/helpers/users.go @@ -26,6 +26,10 @@ func FirstAccountCheck() (bool, error) { return false, result.Error } + if result.RowsAffected < 1 { + return false, nil + } + return true, nil } @@ -72,6 +76,10 @@ func UserExistsAndIsActive(id uint) (bool, error) { return false, result.Error } + if result.RowsAffected < 1 { + return false, nil + } + return true, nil } diff --git a/middlewares/saved_session.go b/middlewares/saved_session.go index 18b9cc9..aa89c7d 100644 --- a/middlewares/saved_session.go +++ b/middlewares/saved_session.go @@ -37,7 +37,7 @@ func SavedSessionMiddleware(c *fiber.Ctx) error { 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-secret")