144 lines
3.1 KiB
Go
144 lines
3.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type System struct {
|
|
Id int64
|
|
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 int64) 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 int64) 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)
|
|
}
|
|
|
|
func (m FoundryStateModel) GetSystems(idState int64) (Systems, error) {
|
|
if idState < 1 {
|
|
return nil, ErrorRecordNotFound
|
|
}
|
|
|
|
query := `
|
|
SELECT id, created_at, text_id, title, description, url, download
|
|
FROM systems
|
|
WHERE state_id = $1`
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
rows, err := m.DB.QueryContext(ctx, query, idState)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
return nil, ErrorRecordNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
systems := make(Systems, 0, 1)
|
|
|
|
for rows.Next() {
|
|
var system System
|
|
err := rows.Scan(
|
|
&system.Id,
|
|
&system.CreatedAt,
|
|
&system.TextId,
|
|
&system.Title,
|
|
&system.Description,
|
|
&system.Url,
|
|
&system.Download,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
compatibility, err := m.GetModuleCompatibility(system.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
system.Compatibility = *compatibility
|
|
systems = append(systems, system)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return systems, nil
|
|
}
|
|
|
|
func (m FoundryStateModel) GetSystemCompatibility(idSystem int64) (*Compatibility, error) {
|
|
if idSystem < 1 {
|
|
return nil, ErrorRecordNotFound
|
|
}
|
|
|
|
query := `
|
|
SELECT id, minimum, verified, maximum
|
|
FROM systems_compatibility
|
|
WHERE system_id = $1`
|
|
|
|
var compatibility Compatibility
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
err := m.DB.QueryRowContext(ctx, query, idSystem).Scan(
|
|
&compatibility.Id,
|
|
&compatibility.Minimum,
|
|
&compatibility.Verified,
|
|
&compatibility.Maximum,
|
|
)
|
|
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
return nil, ErrorRecordNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &compatibility, nil
|
|
}
|