add db get queries
This commit is contained in:
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type System struct {
|
||||
Id int
|
||||
Id int64
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
@@ -22,7 +24,7 @@ func (systems Systems) GetById(id int) *System {
|
||||
return &systems[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int) error {
|
||||
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)`
|
||||
@@ -39,7 +41,7 @@ func (m FoundryStateModel) InsertSystem(system *System, stateId int) error {
|
||||
return m.InsertModuleCompatibility(&system.Compatibility, system.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int) error {
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems_compatibility (system_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
@@ -51,3 +53,91 @@ func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibilit
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user