refactor(app): 🗃️ enhance repositories

master
Xeronith 2022-09-19 16:40:41 +04:30
rodzic e03cd93353
commit e0ccadf480
4 zmienionych plików z 48 dodań i 67 usunięć

Wyświetl plik

@ -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
}

Wyświetl plik

@ -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()

Wyświetl plik

@ -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{

Wyświetl plik

@ -1,3 +1,6 @@
package contracts
type ViewData map[string]interface{}
type (
ViewData map[string]interface{}
Values map[string]interface{}
)