Merge pull request #96 from akuma06/master
Models for User, Role and Comment …
Cette révision appartient à :
révision
5ab269b0ad
7 fichiers modifiés avec 169 ajouts et 3 suppressions
|
@ -102,4 +102,4 @@ func (config *Config) Pretty(output io.Writer) error {
|
|||
data = append(data, []byte("\n")...)
|
||||
_, err = output.Write(data)
|
||||
return err
|
||||
}
|
||||
}
|
|
@ -54,4 +54,4 @@ func GormInit(conf *config.Config) (*gorm.DB, error) {
|
|||
// handler.Setup(&relation, "users_followers", m1Type, m2Type)
|
||||
|
||||
return db, err
|
||||
}
|
||||
}
|
2
main.go
Fichier exécutable → Fichier normal
2
main.go
Fichier exécutable → Fichier normal
|
@ -47,4 +47,4 @@ func main() {
|
|||
db.ORM, _ = db.GormInit(conf)
|
||||
RunServer(conf)
|
||||
}
|
||||
}
|
||||
}
|
18
model/comment.go
Fichier normal
18
model/comment.go
Fichier normal
|
@ -0,0 +1,18 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Comment is a comment model.
|
||||
type Comment struct {
|
||||
Id int `json:"id"`
|
||||
Content string `json:"content"`
|
||||
UserId int `json:"userId"`
|
||||
TorrentId int
|
||||
// LikingCount int `json:"likingCount"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt time.Time `json:"deletedAt"`
|
||||
User User `json:"user"`
|
||||
}
|
8
model/role.go
Fichier normal
8
model/role.go
Fichier normal
|
@ -0,0 +1,8 @@
|
|||
package model
|
||||
|
||||
// Role is a role model for user permission.
|
||||
type Role struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name",sql:"size:255"`
|
||||
Description string `json:"description",sql:"size:255"`
|
||||
}
|
92
model/user.go
Fichier normal
92
model/user.go
Fichier normal
|
@ -0,0 +1,92 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// omit is the bool type for omitting a field of struct.
|
||||
type omit bool
|
||||
|
||||
// User is a user model
|
||||
type User struct {
|
||||
Id uint `json:"id"`
|
||||
|
||||
Email string `json:"email",sql:"size:255;unique"`
|
||||
Password string `json:"password",sql:"size:255"`
|
||||
Username string `json:"username",sql:"size:255;unique"`
|
||||
Description string `json:"description",sql:"size:100"`
|
||||
Token string `json:"token"`
|
||||
TokenExpiration time.Time `json:"tokenExperiation"`
|
||||
|
||||
// email md5 for gravatar
|
||||
Md5 string `json:"md5"`
|
||||
|
||||
// admin
|
||||
Activation bool `json:"activation"`
|
||||
PasswordResetToken string `json:"passwordResetToken"`
|
||||
ActivationToken string `json:"activationToken"`
|
||||
PasswordResetUntil time.Time `json:"passwordResetUntil"`
|
||||
ActivateUntil time.Time `json:"activateUntil"`
|
||||
ActivatedAt time.Time `json:"activatedAt"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt *time.Time `json:"deletedAt"`
|
||||
LastLoginAt time.Time `json:"lastLoginAt"`
|
||||
CurrentLoginAt time.Time `json:"currentLoginAt"`
|
||||
LastLoginIp string `json:"lastLoginIp",sql:"size:100"`
|
||||
CurrentLoginIp string `json:"currentLoginIp",sql:"size:100"`
|
||||
|
||||
// Liking
|
||||
LikingCount int `json:"likingCount"`
|
||||
LikedCount int `json:"likedCount"`
|
||||
Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:users_followers;"`
|
||||
Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:users_followers;"`
|
||||
|
||||
Connections []Connection
|
||||
Roles []Role `gorm:"many2many:users_roles;"` // Many To Many, users_roles
|
||||
Torrents []Torrents
|
||||
}
|
||||
|
||||
// UsersFollowers is a relation table to relate users each other.
|
||||
type UsersFollowers struct {
|
||||
UserID uint `json:"user_id"`
|
||||
FollowerID uint `json:"follower_id"`
|
||||
}
|
||||
|
||||
// PublicUser is a public user model that contains only a few information for everyone.
|
||||
type PublicUser struct {
|
||||
*User
|
||||
Email omit `json:"email,omitempty",sql:"size:255;unique"`
|
||||
Password omit `json:"password,omitempty",sql:"size:255"`
|
||||
Token omit `json:"token,omitempty"`
|
||||
TokenExpiration omit `json:"tokenExperiation,omitempty"`
|
||||
|
||||
// admin
|
||||
Activation omit `json:"activation,omitempty"`
|
||||
PasswordResetToken omit `json:"passwordResetToken,omitempty"`
|
||||
ActivationToken omit `json:"activationToken,omitempty"`
|
||||
PasswordResetUntil omit `json:"passwordResetUntil,omitempty"`
|
||||
ActivateUntil omit `json:"activateUntil,omitempty"`
|
||||
ActivatedAt omit `json:"activatedAt,omitempty"`
|
||||
UpdatedAt omit `json:"updatedAt,omitempty"`
|
||||
DeletedAt omit `json:"deletedAt,omitempty"`
|
||||
LastLoginAt omit `json:"lastLoginAt,omitempty"`
|
||||
CurrentLoginAt omit `json:"currentLoginAt,omitempty"`
|
||||
LastLoginIp omit `json:"lastLoginIp,omitempty",sql:"size:100"`
|
||||
CurrentLoginIp omit `json:"currentLoginIp,omitempty",sql:"size:100"`
|
||||
|
||||
Connections omit `json:"connections,omitempty"`
|
||||
Roles omit `json:"roles,omitempty"`
|
||||
Torrents omit `json:"articles,omitempty"`
|
||||
}
|
||||
|
||||
// Connection is a connection model for oauth.
|
||||
type Connection struct {
|
||||
Id uint `json:"id"`
|
||||
UserId uint `json:"userId"`
|
||||
ProviderId uint `gorm:"column:provider_id", json:"providerId"`
|
||||
ProviderUserId string `gorm:"column:provider_user_id", json:"providerUserId"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
ProfileUrl string `gorm:"column:profile_url", json:"profileUrl"`
|
||||
ImageUrl string `gorm:"column:image_url", json:"imageUrl"`
|
||||
}
|
48
util/modelHelper/modelHelper.go
Fichier normal
48
util/modelHelper/modelHelper.go
Fichier normal
|
@ -0,0 +1,48 @@
|
|||
package modelHelper
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"net/http"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func IsZeroOfUnderlyingType(x interface{}) bool {
|
||||
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
|
||||
}
|
||||
|
||||
// AssignValue assign form values to model.
|
||||
func AssignValue(model interface{}, form interface{}) {
|
||||
modelIndirect := reflect.Indirect(reflect.ValueOf(model))
|
||||
formElem := reflect.ValueOf(form).Elem()
|
||||
typeOfTForm := formElem.Type()
|
||||
for i := 0; i < formElem.NumField(); i++ {
|
||||
modelField := modelIndirect.FieldByName(typeOfTForm.Field(i).Name)
|
||||
if modelField.IsValid() {
|
||||
formField := formElem.Field(i)
|
||||
modelField.Set(formField)
|
||||
} else {
|
||||
log.Warnf("modelField : %s - %s", typeOfTForm.Field(i).Name, modelField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AssignValue assign form values to model.
|
||||
func BindValueForm(form interface{}, r *http.Request) {
|
||||
r.ParseForm()
|
||||
formElem := reflect.ValueOf(form).Elem()
|
||||
for i := 0; i < formElem.NumField(); i++ {
|
||||
typeField := formElem.Type().Field(i)
|
||||
tag := typeField.Tag
|
||||
switch typeField.Type.Name() {
|
||||
case "string" :
|
||||
formElem.Field(i).SetString(r.PostFormValue(tag.Get("form")))
|
||||
case "int" :
|
||||
nbr, _ := strconv.Atoi(r.PostFormValue(tag.Get("form")))
|
||||
formElem.Field(i).SetInt(int64(nbr))
|
||||
case "float" :
|
||||
nbr, _ := strconv.Atoi(r.PostFormValue(tag.Get("form")))
|
||||
formElem.Field(i).SetFloat(float64(nbr))
|
||||
}
|
||||
}
|
||||
}
|
Référencer dans un nouveau ticket