add db models, refactor json models, add references in migrations
This commit is contained in:
53
internal/foundry/models/db/system.go
Normal file
53
internal/foundry/models/db/system.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type System struct {
|
||||
Id int
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
Url string
|
||||
Download string
|
||||
CreatedAt time.Time
|
||||
Compatibility Compatibility
|
||||
}
|
||||
|
||||
type Systems []System
|
||||
|
||||
func (systems Systems) GetById(id int) *System {
|
||||
return &systems[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int) error {
|
||||
query := `
|
||||
INSERT INTO systems (state_id, text_id, title, description, url, download)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`
|
||||
|
||||
args := []any{stateId, system.TextId, system.Title, system.Description, system.Url, system.Download}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&system.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.InsertModuleCompatibility(&system.Compatibility, system.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int) error {
|
||||
query := `
|
||||
INSERT INTO systems_compatibility (system_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
|
||||
args := []any{systemId, compatibility.Minimum, compatibility.Verified, compatibility.Maximum}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
}
|
||||
Reference in New Issue
Block a user