diff --git a/backend/go.mod b/backend/go.mod index baad80f..6964dc1 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -5,6 +5,7 @@ go 1.16 require ( github.com/julienschmidt/httprouter v1.3.0 github.com/pelletier/go-toml/v2 v2.0.0-beta.3 + github.com/satori/go.uuid v1.2.0 gorm.io/driver/mysql v1.1.2 gorm.io/gorm v1.21.13 ) diff --git a/backend/go.sum b/backend/go.sum index 5e71c6a..8bd59e9 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -12,9 +12,12 @@ github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGl github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942 h1:t0lM6y/M5IiUZyvbBTcngso8SZEZICH7is9B6g/obVU= github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/backend/internal/data/gorm.go b/backend/internal/data/gorm.go index 5196900..7ac8ddd 100644 --- a/backend/internal/data/gorm.go +++ b/backend/internal/data/gorm.go @@ -5,6 +5,7 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" "strings" + "threadule/backend/internal/data/models" ) func connect(dsn string) (*gorm.DB, error) { @@ -14,7 +15,11 @@ func connect(dsn string) (*gorm.DB, error) { func migrate(db *gorm.DB) error { var errs []error - //errs = append(errs, db.AutoMigrate()) + errs = append(errs, db.AutoMigrate(&models.Group{})) + errs = append(errs, db.AutoMigrate(&models.User{})) + errs = append(errs, db.AutoMigrate(&models.Account{})) + errs = append(errs, db.AutoMigrate(&models.Tweet{})) + errs = append(errs, db.AutoMigrate(&models.Thread{})) errorBuilder := strings.Builder{} for _, err := range errs { diff --git a/backend/internal/data/models/account.go b/backend/internal/data/models/account.go new file mode 100644 index 0000000..84e56e7 --- /dev/null +++ b/backend/internal/data/models/account.go @@ -0,0 +1,13 @@ +package models + +import uuid "github.com/satori/go.uuid" + +type Account struct { + BaseModel + UserID uuid.UUID + User *User + + Name string + AccessToken *string + AccessTokenSecret *string +} diff --git a/backend/internal/data/models/base.go b/backend/internal/data/models/base.go new file mode 100644 index 0000000..a165c43 --- /dev/null +++ b/backend/internal/data/models/base.go @@ -0,0 +1,19 @@ +package models + +import ( + "github.com/satori/go.uuid" + "gorm.io/gorm" + "time" +) + +type BaseModel struct { + ID uuid.UUID `gorm:"type:uuid;primary_key;"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` +} + +func (b *BaseModel) BeforeCreate(_ *gorm.DB) error { + b.ID = uuid.NewV4() + return nil +} diff --git a/backend/internal/data/models/group.go b/backend/internal/data/models/group.go new file mode 100644 index 0000000..da05986 --- /dev/null +++ b/backend/internal/data/models/group.go @@ -0,0 +1,13 @@ +package models + +type Group struct { + BaseModel + Users []*User `gorm:"many2many:user_groups;"` + + Name string + DisplayName string + + LimitAccounts uint + LimitThreads uint + LimitTweets uint +} diff --git a/backend/internal/data/models/thread.go b/backend/internal/data/models/thread.go new file mode 100644 index 0000000..a1ca0c0 --- /dev/null +++ b/backend/internal/data/models/thread.go @@ -0,0 +1,16 @@ +package models + +import ( + uuid "github.com/satori/go.uuid" + "time" +) + +type Thread struct { + BaseModel + AccountID uuid.UUID + Account *Account + Tweets []Tweet + + Sent bool + ScheduledFor time.Time +} diff --git a/backend/internal/data/models/tweet.go b/backend/internal/data/models/tweet.go new file mode 100644 index 0000000..af0808f --- /dev/null +++ b/backend/internal/data/models/tweet.go @@ -0,0 +1,12 @@ +package models + +import uuid "github.com/satori/go.uuid" + +type Tweet struct { + BaseModel + ThreadID uuid.UUID + + TweetID *string + Text string + Sent bool +} diff --git a/backend/internal/data/models/user.go b/backend/internal/data/models/user.go new file mode 100644 index 0000000..865d85e --- /dev/null +++ b/backend/internal/data/models/user.go @@ -0,0 +1,9 @@ +package models + +type User struct { + BaseModel + Groups []*Group `gorm:"many2many:user_groups;"` + + Username string + Password string +}