add db models, refactor json models, add references in migrations
This commit is contained in:
84
internal/foundry/models/db/module.go
Normal file
84
internal/foundry/models/db/module.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Module struct {
|
||||
Id int
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
Url string
|
||||
Version string
|
||||
Availability int
|
||||
CreatedAt time.Time
|
||||
Languages []Language
|
||||
Compatibility Compatibility
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Id string
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
type Modules []Module
|
||||
|
||||
func (modules Modules) GetById(id int) *Module {
|
||||
return &modules[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModule(module *Module, stateId int) error {
|
||||
query := `
|
||||
INSERT INTO modules (state_id, text_id, title, description, url, version, availability)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, created_at`
|
||||
|
||||
args := []any{stateId, module.TextId, module.Title, module.Description, module.Url, module.Version, module.Availability}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&module.Id, &module.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range module.Languages {
|
||||
err = m.InsertModuleLanguages(&module.Languages[i], module.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return m.InsertModuleCompatibility(&module.Compatibility, module.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibility, moduleId int) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (model_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{moduleId, 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)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (model_id, language, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{moduleId, lang.Lang, lang.Name, lang.Path}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&lang.Id)
|
||||
}
|
||||
Reference in New Issue
Block a user