Skip to content

Instantly share code, notes, and snippets.

@victorsteven
Created September 11, 2020 14:13
Show Gist options
  • Save victorsteven/857eeb4afe211c4693b9527936d2b8c8 to your computer and use it in GitHub Desktop.
Save victorsteven/857eeb4afe211c4693b9527936d2b8c8 to your computer and use it in GitHub Desktop.
package models
import (
"errors"
"html"
"log"
"os"
"strings"
"time"
"github.com/victorsteven/forum/api/security"
"github.com/badoux/checkmail"
"github.com/jinzhu/gorm"
)
type User struct {
ID uint32 `gorm:"primary_key;auto_increment" json:"id"`
Username string `gorm:"size:255;not null;unique" json:"username"`
Email string `gorm:"size:100;not null;unique" json:"email"`
Password string `gorm:"size:100;not null;" json:"password"`
AvatarPath string `gorm:"size:255;null;" json:"avatar_path"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
func (u *User) BeforeSave() error {
hashedPassword, err := security.Hash(u.Password)
if err != nil {
return err
}
u.Password = string(hashedPassword)
return nil
}
func (u *User) Prepare() {
u.Username = html.EscapeString(strings.TrimSpace(u.Username))
u.Email = html.EscapeString(strings.TrimSpace(u.Email))
u.CreatedAt = time.Now()
u.UpdatedAt = time.Now()
}
func (u *User) AfterFind() (err error) {
if err != nil {
return err
}
if u.AvatarPath != "" {
u.AvatarPath = os.Getenv("DO_SPACES_URL") + u.AvatarPath
}
//dont return the user password
// u.Password = ""
return nil
}
func (u *User) Validate(action string) map[string]string {
var errorMessages = make(map[string]string)
var err error
switch strings.ToLower(action) {
case "update":
if u.Email == "" {
err = errors.New("Required Email")
errorMessages["Required_email"] = err.Error()
}
if u.Email != "" {
if err = checkmail.ValidateFormat(u.Email); err != nil {
err = errors.New("Invalid Email")
errorMessages["Invalid_email"] = err.Error()
}
}
case "login":
if u.Password == "" {
err = errors.New("Required Password")
errorMessages["Required_password"] = err.Error()
}
if u.Email == "" {
err = errors.New("Required Email")
errorMessages["Required_email"] = err.Error()
}
if u.Email != "" {
if err = checkmail.ValidateFormat(u.Email); err != nil {
err = errors.New("Invalid Email")
errorMessages["Invalid_email"] = err.Error()
}
}
case "forgotpassword":
if u.Email == "" {
err = errors.New("Required Email")
errorMessages["Required_email"] = err.Error()
}
if u.Email != "" {
if err = checkmail.ValidateFormat(u.Email); err != nil {
err = errors.New("Invalid Email")
errorMessages["Invalid_email"] = err.Error()
}
}
default:
if u.Username == "" {
err = errors.New("Required Username")
errorMessages["Required_username"] = err.Error()
}
if u.Password == "" {
err = errors.New("Required Password")
errorMessages["Required_password"] = err.Error()
}
if u.Password != "" && len(u.Password) < 6 {
err = errors.New("Password should be atleast 6 characters")
errorMessages["Invalid_password"] = err.Error()
}
if u.Email == "" {
err = errors.New("Required Email")
errorMessages["Required_email"] = err.Error()
}
if u.Email != "" {
if err = checkmail.ValidateFormat(u.Email); err != nil {
err = errors.New("Invalid Email")
errorMessages["Invalid_email"] = err.Error()
}
}
}
return errorMessages
}
func (u *User) SaveUser(db *gorm.DB) (*User, error) {
var err error
err = db.Debug().Create(&u).Error
if err != nil {
return &User{}, err
}
return u, nil
}
// THE ONLY PERSON THAT NEED TO DO THIS IS THE ADMIN, SO I HAVE COMMENTED THE ROUTES, SO SOMEONE ELSE DONT VIEW THIS DETAILS.
func (u *User) FindAllUsers(db *gorm.DB) (*[]User, error) {
var err error
users := []User{}
err = db.Debug().Model(&User{}).Limit(100).Find(&users).Error
if err != nil {
return &[]User{}, err
}
return &users, err
}
func (u *User) FindUserByID(db *gorm.DB, uid uint32) (*User, error) {
var err error
err = db.Debug().Model(User{}).Where("id = ?", uid).Take(&u).Error
if err != nil {
return &User{}, err
}
if gorm.IsRecordNotFoundError(err) {
return &User{}, errors.New("User Not Found")
}
return u, err
}
func (u *User) UpdateAUser(db *gorm.DB, uid uint32) (*User, error) {
if u.Password != "" {
// To hash the password
err := u.BeforeSave()
if err != nil {
log.Fatal(err)
}
db = db.Debug().Model(&User{}).Where("id = ?", uid).Take(&User{}).UpdateColumns(
map[string]interface{}{
"password": u.Password,
"email": u.Email,
"update_at": time.Now(),
},
)
}
db = db.Debug().Model(&User{}).Where("id = ?", uid).Take(&User{}).UpdateColumns(
map[string]interface{}{
"email": u.Email,
"update_at": time.Now(),
},
)
if db.Error != nil {
return &User{}, db.Error
}
// This is the display the updated user
err := db.Debug().Model(&User{}).Where("id = ?", uid).Take(&u).Error
if err != nil {
return &User{}, err
}
return u, nil
}
func (u *User) UpdateAUserAvatar(db *gorm.DB, uid uint32) (*User, error) {
db = db.Debug().Model(&User{}).Where("id = ?", uid).Take(&User{}).UpdateColumns(
map[string]interface{}{
"avatar_path": u.AvatarPath,
"update_at": time.Now(),
},
)
if db.Error != nil {
return &User{}, db.Error
}
// This is the display the updated user
err := db.Debug().Model(&User{}).Where("id = ?", uid).Take(&u).Error
if err != nil {
return &User{}, err
}
return u, nil
}
func (u *User) DeleteAUser(db *gorm.DB, uid uint32) (int64, error) {
db = db.Debug().Model(&User{}).Where("id = ?", uid).Take(&User{}).Delete(&User{})
if db.Error != nil {
return 0, db.Error
}
return db.RowsAffected, nil
}
func (u *User) UpdatePassword(db *gorm.DB) error {
// To hash the password
err := u.BeforeSave()
if err != nil {
log.Fatal(err)
}
db = db.Debug().Model(&User{}).Where("email = ?", u.Email).Take(&User{}).UpdateColumns(
map[string]interface{}{
"password": u.Password,
"update_at": time.Now(),
},
)
if db.Error != nil {
return db.Error
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment