bigest part of the api done
This commit is contained in:
38
models/card.go
Normal file
38
models/card.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Package models provides ...
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Card struct {
|
||||
gorm.Model
|
||||
UserID uint
|
||||
User User
|
||||
Front string `json:"front" binding:"required"`
|
||||
Back string `json:"back" binding:"required"`
|
||||
Hint string `json:"hint"`
|
||||
CardDeckID uint `json:"deck_id" binding:"required"`
|
||||
CardDeck CardDeck
|
||||
PhaseID uint
|
||||
Phase Phase
|
||||
NextTest datatypes.Date
|
||||
}
|
||||
|
||||
type CardDto struct {
|
||||
ID uint `json:"id"`
|
||||
Front string `json:"front" binding:"required"`
|
||||
Back string `json:"back" binding:"required"`
|
||||
Hint string `json:"hint"`
|
||||
CardDeckID uint `json:"deck_id" binding:"required"`
|
||||
}
|
||||
|
||||
func (c Card) ToDto() CardDto {
|
||||
return CardDto{c.ID, c.Front, c.Back, c.Hint, c.CardDeckID}
|
||||
}
|
||||
|
||||
func (c *Card) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
c.PhaseID = phaseOneID
|
||||
return
|
||||
}
|
||||
28
models/card_deck.go
Normal file
28
models/card_deck.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Package models provides the data models and database connection
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type CardDeck struct {
|
||||
gorm.Model
|
||||
UserID uint
|
||||
User User
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Cards []Card `json:"cards"`
|
||||
}
|
||||
|
||||
type CardDeckDto struct {
|
||||
ID uint `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Cards []CardDto `json:"cards"`
|
||||
}
|
||||
|
||||
func (c CardDeck) ToDto() CardDeckDto {
|
||||
dto := CardDeckDto{ID: c.ID, Title: c.Title, Description: c.Description}
|
||||
for _, card := range c.Cards {
|
||||
dto.Cards = append(dto.Cards, card.ToDto())
|
||||
}
|
||||
return dto
|
||||
}
|
||||
13
models/phase.go
Normal file
13
models/phase.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Phase struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
PauseLength uint
|
||||
FirstID *uint
|
||||
First *Phase
|
||||
NextID *uint
|
||||
Next *Phase
|
||||
}
|
||||
114
models/setup.go
Normal file
114
models/setup.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
var phaseOneID uint
|
||||
|
||||
func ConnectDatabaseDev() {
|
||||
database, err := gorm.Open(sqlite.Open("dev.db"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Warn),
|
||||
})
|
||||
if err != nil {
|
||||
panic("Couldn't connect to database")
|
||||
}
|
||||
|
||||
database.AutoMigrate(&Card{}, &CardDeck{}, &Phase{})
|
||||
|
||||
DB = database
|
||||
}
|
||||
func ConnectDatabase() {
|
||||
database, err := gorm.Open(postgres.Open(""), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Warn),
|
||||
})
|
||||
if err != nil {
|
||||
panic("Couldn't connect to database")
|
||||
}
|
||||
|
||||
database.AutoMigrate(&Card{}, &CardDeck{}, &Phase{}, &User{})
|
||||
|
||||
DB = database
|
||||
}
|
||||
|
||||
func SeedDatabase() {
|
||||
|
||||
var phaseCount int64
|
||||
var phaseOne Phase
|
||||
if DB.Table("phases").Count(&phaseCount); phaseCount == 0 {
|
||||
phaseFive := Phase{
|
||||
Name: "Phase 5",
|
||||
PauseLength: 90,
|
||||
}
|
||||
phaseFour := Phase{
|
||||
Name: "Phase 4",
|
||||
PauseLength: 30,
|
||||
}
|
||||
phaseThree := Phase{
|
||||
Name: "Phase 3",
|
||||
PauseLength: 14,
|
||||
}
|
||||
phaseTwo := Phase{
|
||||
Name: "Phase 2",
|
||||
PauseLength: 7,
|
||||
}
|
||||
phaseOne = Phase{
|
||||
Name: "Phase 1",
|
||||
PauseLength: 3,
|
||||
}
|
||||
DB.Create(&phaseOne)
|
||||
DB.Create(&phaseTwo)
|
||||
DB.Create(&phaseThree)
|
||||
DB.Create(&phaseFour)
|
||||
DB.Create(&phaseFive)
|
||||
DB.Save(&phaseOne)
|
||||
DB.Save(&phaseTwo)
|
||||
DB.Save(&phaseThree)
|
||||
DB.Save(&phaseFour)
|
||||
DB.Save(&phaseFive)
|
||||
phaseTwo.FirstID = &phaseOne.ID
|
||||
phaseThree.FirstID = &phaseOne.ID
|
||||
phaseFour.FirstID = &phaseOne.ID
|
||||
phaseFive.FirstID = &phaseOne.ID
|
||||
phaseOne.NextID = &phaseTwo.ID
|
||||
phaseTwo.NextID = &phaseThree.ID
|
||||
phaseThree.NextID = &phaseFour.ID
|
||||
phaseFour.NextID = &phaseFive.ID
|
||||
DB.Save(&phaseOne)
|
||||
DB.Save(&phaseTwo)
|
||||
DB.Save(&phaseThree)
|
||||
DB.Save(&phaseFour)
|
||||
DB.Save(&phaseFive)
|
||||
|
||||
} else {
|
||||
DB.First(&phaseOne, &Phase{First: nil})
|
||||
}
|
||||
phaseOneID = phaseOne.ID
|
||||
|
||||
var cardDeckCount int64
|
||||
if DB.Table("card_decks").Count(&cardDeckCount); cardDeckCount == 0 {
|
||||
cardDeck := CardDeck{
|
||||
Title: "Test Stapel",
|
||||
Description: "Dieser Stapel wurde als Beispiel erstllt",
|
||||
Cards: []Card{
|
||||
{
|
||||
Front: "Hallo",
|
||||
Back: "Hello",
|
||||
Hint: "Hallo in Englisch",
|
||||
},
|
||||
{
|
||||
Front: "Auf Wiedersehen",
|
||||
Back: "Bye",
|
||||
Hint: "Auf Wiedersehen auf Englisch",
|
||||
},
|
||||
},
|
||||
}
|
||||
DB.Create(&cardDeck)
|
||||
|
||||
DB.Save(&cardDeck)
|
||||
}
|
||||
}
|
||||
35
models/user.go
Normal file
35
models/user.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"spahl.ddns.net/jasper/wok-able-backend/auth"
|
||||
)
|
||||
|
||||
const saltSize = 64
|
||||
|
||||
type User struct {
|
||||
ID uint `json:"-" gorm:"primary key"`
|
||||
Username string `json:"username" gorm:"unique;not null"`
|
||||
Email string `json:"email" gorm:"unique;not null"`
|
||||
Password string `json:"password" gorm:"-" binding:"required"`
|
||||
PasswordHash []byte `json:"-" gorm:"not null"`
|
||||
Salt []byte `json:"-" gorm:"not null"`
|
||||
CardDecks []CardDeck `json:"-"`
|
||||
Cards []Card `json:"-"`
|
||||
}
|
||||
type Auth struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
salt := make([]byte, saltSize)
|
||||
if _, err = rand.Read(salt[:]); err != nil {
|
||||
return
|
||||
}
|
||||
u.Salt = salt
|
||||
u.PasswordHash, err = auth.HashPassword(u.Password, u.Salt)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user