diff --git a/greataped/app/models/repos/auth.go b/greataped/app/models/repos/auth.go index 22cd80a..528f2c6 100644 --- a/greataped/app/models/repos/auth.go +++ b/greataped/app/models/repos/auth.go @@ -4,7 +4,6 @@ import ( "contracts" "db" "errors" - "fmt" "github.com/gofiber/fiber/v2" "gorm.io/gorm" @@ -35,76 +34,54 @@ type User struct { } // CreateUser create a user entry in the user's table -func CreateUser(user *User) *gorm.DB { - return db.Executor.Create(user) +func CreateUser(user *User) error { + if err := db.Executor.Create(user).Error; err != nil { + return err + } + + return nil } // FindUser searches the user's table with the condition given -func FindUser(dest interface{}, conds ...interface{}) *gorm.DB { - return db.Executor.Model(&User{}).Take(dest, conds...) +func FindUser(conds ...any) (*User, error) { + dest := &User{} + if err := db.Executor.Model(dest).Take(dest, conds...).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, &fiber.Error{ + Code: contracts.StatusNotFound, + Message: "user not found", + } + } else { + return nil, &fiber.Error{ + Code: contracts.StatusInternalServerError, + Message: err.Error(), + } + } + } + + return dest, nil } // FindUserById searches the user's table with the id given func FindUserById(id uint) (*User, error) { - user := &User{} - if err := FindUser(user, "id = ?", id).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &fiber.Error{ - Code: contracts.StatusNotFound, - Message: "user not found", - } - } else { - return nil, &fiber.Error{ - Code: contracts.StatusInternalServerError, - Message: err.Error(), - } - } - } - - return user, nil + return FindUser("id = ?", id) } // FindUserByEmail searches the user's table with the email given func FindUserByEmail(email string) (*User, error) { - user := &User{} - if err := FindUser(user, "email = ?", email).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &fiber.Error{ - Code: contracts.StatusNotFound, - Message: "user not found", - } - } else { - return nil, &fiber.Error{ - Code: contracts.StatusInternalServerError, - Message: err.Error(), - } - } - } - - return user, nil + return FindUser("email = ?", email) } // FindUserByUsername searches the user's table with the name given func FindUserByUsername(username string) (*User, error) { - user := &User{} - if err := FindUser(user, "username = ?", username).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &fiber.Error{ - Code: contracts.StatusNotFound, - Message: fmt.Sprintf("user '%s' not found", username), - } - } else { - return nil, &fiber.Error{ - Code: contracts.StatusInternalServerError, - Message: err.Error(), - } - } - } - - return user, nil + return FindUser("username = ?", username) } // UpdateProfile updates the user's profile with the info given -func UpdateProfile(userId interface{}, data interface{}) *gorm.DB { - return db.Executor.Model(&User{}).Where("id = ?", userId).Updates(data) +func UpdateProfile(userId interface{}, data interface{}) error { + if err := db.Executor.Model(&User{}).Where("id = ?", userId).Updates(data).Error; err != nil { + return err + } + + return nil } diff --git a/greataped/app/routes/profile.go b/greataped/app/routes/profile.go index 90b927d..af8bcac 100644 --- a/greataped/app/routes/profile.go +++ b/greataped/app/routes/profile.go @@ -56,15 +56,16 @@ var UpdateProfile = route.New(HttpPost, "/api/v1/profile", func(x IContext) erro access = repos.ACCESS_PRIVATE } - if err := repos.UpdateProfile(user.ID, map[string]interface{}{ - "display_name": body.DisplayName, - "bio": body.Bio, - "github": body.Github, - "avatar": body.Avatar, - "banner": body.Banner, - "access": access, - }).Error; err != nil { - return x.InternalServerError("update_failed") + if err := repos.UpdateProfile(user.ID, + Values{ + "display_name": body.DisplayName, + "bio": body.Bio, + "github": body.Github, + "avatar": body.Avatar, + "banner": body.Banner, + "access": access, + }); err != nil { + return err } return x.Nothing() diff --git a/greataped/app/routes/signup.go b/greataped/app/routes/signup.go index 66db1b8..9dd23a8 100644 --- a/greataped/app/routes/signup.go +++ b/greataped/app/routes/signup.go @@ -39,8 +39,8 @@ var Signup = route.New(HttpPost, "/api/v1/signup", func(x IContext) error { PublicKey: publicKey, } - if err := repos.CreateUser(user); err.Error != nil { - return x.Conflict(err.Error) + if err := repos.CreateUser(user); err != nil { + return x.Conflict(err) } token := jwt.Generate(&jwt.TokenPayload{ diff --git a/greataped/contracts/types.go b/greataped/contracts/types.go index 98bbb4c..426f17e 100644 --- a/greataped/contracts/types.go +++ b/greataped/contracts/types.go @@ -1,3 +1,6 @@ package contracts -type ViewData map[string]interface{} +type ( + ViewData map[string]interface{} + Values map[string]interface{} +)