add init db data, fix bugs, refactor
This commit is contained in:
@@ -27,23 +27,24 @@ func (systems Systems) GetById(id int) *System {
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems (state_id, text_id, title, description, url, download)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at`
|
||||
|
||||
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 {
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&system.Id, &system.CreatedAt)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return m.InsertModuleCompatibility(&system.Compatibility, system.Id)
|
||||
return m.InsertSystemCompatibility(&system.Compatibility, system.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
func (m FoundryStateModel) InsertSystemCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems_compatibility (system_id, minumum, verified, maximum)
|
||||
INSERT INTO systems_compatibility (system_id, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
|
||||
args := []any{systemId, compatibility.Minimum, compatibility.Verified, compatibility.Maximum}
|
||||
@@ -51,7 +52,11 @@ func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibilit
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetSystems(idState int64) (Systems, error) {
|
||||
|
||||
Reference in New Issue
Block a user