2015-09-25 22:33:55 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Author database model
|
|
|
|
type Author struct {
|
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time
|
|
|
|
|
|
|
|
Slug sql.NullString `sql:"not null;unique_index"`
|
|
|
|
Name sql.NullString `sql:"not null"`
|
|
|
|
Youtube sql.NullString `sql:"not null"`
|
|
|
|
AvatarS sql.NullString
|
|
|
|
AvatarB sql.NullString
|
|
|
|
FanArtS sql.NullString
|
|
|
|
FanArtB sql.NullString
|
|
|
|
|
|
|
|
LetsPlays []LetsPlay
|
|
|
|
LetsTests []LetsTest
|
|
|
|
Episodes []Episode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's Play database model
|
|
|
|
type LetsPlay struct {
|
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time
|
|
|
|
AuthorID uint `sql:"index"`
|
|
|
|
|
|
|
|
Slug sql.NullString `sql:"not null;unique_index"`
|
|
|
|
Name sql.NullString `sql:"not null"`
|
|
|
|
PosterS sql.NullString
|
|
|
|
PosterB sql.NullString
|
|
|
|
Aired time.Time `sql:"default:null"`
|
|
|
|
|
2016-02-27 22:10:00 +00:00
|
|
|
MergeID sql.NullInt64
|
|
|
|
MergeSeason sql.NullInt64
|
|
|
|
|
2015-09-25 22:33:55 +00:00
|
|
|
Episodes []Episode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's Test database model
|
|
|
|
type LetsTest struct {
|
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time
|
|
|
|
AuthorID uint `sql:"index"`
|
|
|
|
|
|
|
|
Slug sql.NullString `sql:"not null;unique_index"`
|
|
|
|
Name sql.NullString `sql:"not null"`
|
|
|
|
PosterS sql.NullString
|
|
|
|
PosterB sql.NullString
|
|
|
|
ThumbS sql.NullString
|
|
|
|
ThumbB sql.NullString
|
2016-01-01 13:58:20 +00:00
|
|
|
Youtube sql.NullString `sql:"not null;unique"`
|
2015-09-25 22:33:55 +00:00
|
|
|
Descr sql.NullString `sql:"type:TEXT"`
|
|
|
|
Aired time.Time `sql:"not null"`
|
|
|
|
Rating sql.NullFloat64
|
|
|
|
Votes sql.NullInt64
|
|
|
|
Duration sql.NullInt64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Episode database model
|
|
|
|
type Episode struct {
|
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time
|
|
|
|
AuthorID uint `sql:"index"`
|
|
|
|
LetsPlayID uint `sql:"index"`
|
|
|
|
|
|
|
|
Slug sql.NullString `sql:"not null;unique_index"`
|
|
|
|
Name sql.NullString `sql:"not null"`
|
|
|
|
Episode sql.NullInt64 `sql:"not null"`
|
|
|
|
ThumbS sql.NullString
|
|
|
|
ThumbB sql.NullString
|
2016-01-01 13:58:20 +00:00
|
|
|
Youtube sql.NullString `sql:"not null;unique"`
|
2015-09-25 22:33:55 +00:00
|
|
|
Descr sql.NullString `sql:"type:TEXT"`
|
|
|
|
Aired time.Time `sql:"not null"`
|
|
|
|
Rating sql.NullFloat64
|
|
|
|
Votes sql.NullInt64
|
|
|
|
Duration sql.NullInt64
|
2016-02-27 22:10:00 +00:00
|
|
|
Season sql.NullInt64 `sql:"not null;default:1"`
|
2015-09-25 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-27 16:06:46 +00:00
|
|
|
var Db gorm.DB
|
2015-09-25 22:33:55 +00:00
|
|
|
|
|
|
|
func InitDb(connection string) (error) {
|
|
|
|
var err error
|
|
|
|
Db, err = gorm.Open("postgres", connection)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FAT Database error: %+v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Db.LogMode(false)
|
|
|
|
|
2016-02-27 22:10:00 +00:00
|
|
|
Db.Model(&LetsPlay{}).AddForeignKey("author_id", "authors(id)", "RESTRICT", "RESTRICT")
|
|
|
|
Db.Model(&LetsTest{}).AddForeignKey("author_id", "authors(id)", "RESTRICT", "RESTRICT")
|
|
|
|
Db.Model(&Episode{}).AddForeignKey("author_id", "authors(id)", "RESTRICT", "RESTRICT")
|
|
|
|
Db.Model(&Episode{}).AddForeignKey("lets_play_id", "lets_plays(id)", "RESTRICT", "RESTRICT")
|
|
|
|
|
2015-09-25 22:33:55 +00:00
|
|
|
Db.AutoMigrate(&Author{}, &LetsPlay{}, &LetsTest{}, &Episode{})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty string cleanup
|
|
|
|
func (a *Author) BeforeSave() (err error) {
|
|
|
|
if a.Slug.String == "" {
|
|
|
|
a.Slug.Valid = false
|
|
|
|
} else {
|
|
|
|
a.Slug.Valid = true
|
|
|
|
}
|
|
|
|
if a.Name.String == "" {
|
|
|
|
a.Name.Valid = false
|
|
|
|
} else {
|
|
|
|
a.Name.Valid = true
|
|
|
|
}
|
|
|
|
if a.Youtube.String == "" {
|
|
|
|
a.Youtube.Valid = false
|
|
|
|
} else {
|
|
|
|
a.Youtube.Valid = true
|
|
|
|
}
|
|
|
|
if a.AvatarS.String == "" {
|
|
|
|
a.AvatarS.Valid = false
|
|
|
|
} else {
|
|
|
|
a.AvatarS.Valid = true
|
|
|
|
}
|
|
|
|
if a.AvatarB.String == "" {
|
|
|
|
a.AvatarB.Valid = false
|
|
|
|
} else {
|
|
|
|
a.AvatarB.Valid = true
|
|
|
|
}
|
|
|
|
if a.FanArtS.String == "" {
|
|
|
|
a.FanArtS.Valid = false
|
|
|
|
} else {
|
|
|
|
a.FanArtS.Valid = true
|
|
|
|
}
|
|
|
|
if a.FanArtB.String == "" {
|
|
|
|
a.FanArtB.Valid = false
|
|
|
|
} else {
|
|
|
|
a.FanArtB.Valid = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LetsPlay) BeforeSave() (err error) {
|
|
|
|
if l.Slug.String == "" {
|
|
|
|
l.Slug.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Slug.Valid = true
|
|
|
|
}
|
|
|
|
if l.Name.String == "" {
|
|
|
|
l.Name.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Name.Valid = true
|
|
|
|
}
|
|
|
|
if l.PosterS.String == "" {
|
|
|
|
l.PosterS.Valid = false
|
|
|
|
} else {
|
|
|
|
l.PosterS.Valid = true
|
|
|
|
}
|
|
|
|
if l.PosterB.String == "" {
|
|
|
|
l.PosterB.Valid = false
|
|
|
|
} else {
|
|
|
|
l.PosterB.Valid = true
|
|
|
|
}
|
2016-02-27 22:10:00 +00:00
|
|
|
if l.MergeID.Int64 == 0 {
|
|
|
|
l.MergeID.Valid = false
|
|
|
|
} else {
|
|
|
|
l.MergeID.Valid = true
|
|
|
|
}
|
|
|
|
if l.MergeSeason.Int64 == 0 {
|
|
|
|
l.MergeSeason.Valid = false
|
|
|
|
} else {
|
|
|
|
l.MergeSeason.Valid = true
|
|
|
|
}
|
2015-09-25 22:33:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LetsTest) BeforeSave() (err error) {
|
|
|
|
if l.Slug.String == "" {
|
|
|
|
l.Slug.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Slug.Valid = true
|
|
|
|
}
|
|
|
|
if l.Name.String == "" {
|
|
|
|
l.Name.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Name.Valid = true
|
|
|
|
}
|
|
|
|
if l.PosterS.String == "" {
|
|
|
|
l.PosterS.Valid = false
|
|
|
|
} else {
|
|
|
|
l.PosterS.Valid = true
|
|
|
|
}
|
|
|
|
if l.PosterB.String == "" {
|
|
|
|
l.PosterB.Valid = false
|
|
|
|
} else {
|
|
|
|
l.PosterB.Valid = true
|
|
|
|
}
|
|
|
|
if l.ThumbS.String == "" {
|
|
|
|
l.ThumbS.Valid = false
|
|
|
|
} else {
|
|
|
|
l.ThumbS.Valid = true
|
|
|
|
}
|
|
|
|
if l.ThumbB.String == "" {
|
|
|
|
l.ThumbB.Valid = false
|
|
|
|
} else {
|
|
|
|
l.ThumbB.Valid = true
|
|
|
|
}
|
|
|
|
if l.Youtube.String == "" {
|
|
|
|
l.Youtube.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Youtube.Valid = true
|
|
|
|
}
|
|
|
|
if l.Descr.String == "" {
|
|
|
|
l.Descr.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Descr.Valid = true
|
|
|
|
}
|
|
|
|
l.Votes.Valid = true
|
|
|
|
if l.Votes.Int64 == 0 {
|
|
|
|
l.Rating.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Rating.Valid = true
|
|
|
|
}
|
|
|
|
if l.Duration.Int64 == 0 {
|
|
|
|
l.Duration.Valid = false
|
|
|
|
} else {
|
|
|
|
l.Duration.Valid = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Episode) BeforeSave() (err error) {
|
|
|
|
if e.Slug.String == "" {
|
|
|
|
e.Slug.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Slug.Valid = true
|
|
|
|
}
|
|
|
|
if e.Name.String == "" {
|
|
|
|
e.Name.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Name.Valid = true
|
|
|
|
}
|
|
|
|
if e.ThumbS.String == "" {
|
|
|
|
e.ThumbS.Valid = false
|
|
|
|
} else {
|
|
|
|
e.ThumbS.Valid = true
|
|
|
|
}
|
|
|
|
if e.ThumbB.String == "" {
|
|
|
|
e.ThumbB.Valid = false
|
|
|
|
} else {
|
|
|
|
e.ThumbB.Valid = true
|
|
|
|
}
|
|
|
|
if e.Youtube.String == "" {
|
|
|
|
e.Youtube.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Youtube.Valid = true
|
|
|
|
}
|
|
|
|
if e.Descr.String == "" {
|
|
|
|
e.Descr.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Descr.Valid = true
|
|
|
|
}
|
|
|
|
e.Votes.Valid = true
|
|
|
|
e.Episode.Valid = true
|
2016-02-27 16:06:46 +00:00
|
|
|
e.Season.Valid = true
|
2015-09-25 22:33:55 +00:00
|
|
|
if e.Votes.Int64 == 0 {
|
|
|
|
e.Rating.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Rating.Valid = true
|
|
|
|
}
|
|
|
|
if e.Duration.Int64 == 0 {
|
|
|
|
e.Duration.Valid = false
|
|
|
|
} else {
|
|
|
|
e.Duration.Valid = true
|
|
|
|
}
|
|
|
|
return
|
2015-09-26 11:45:14 +00:00
|
|
|
}
|