Cleanup and document hacky sql interaction

This commit is contained in:
Tom Wiesing 2022-09-19 23:16:56 +02:00
parent 881b538dff
commit 07409a01be
No known key found for this signature in database
17 changed files with 284 additions and 204 deletions

View file

@ -3,10 +3,10 @@ package instances
import (
"errors"
"github.com/FAU-CDI/wisski-distillery/internal/bookkeeping"
"github.com/FAU-CDI/wisski-distillery/internal/component"
"github.com/FAU-CDI/wisski-distillery/internal/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/component/triplestore"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/tkw1536/goprogram/exit"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@ -36,17 +36,17 @@ var errSQL = exit.Error{
// It the WissKI does not exist, returns ErrWissKINotFound.
func (instances *Instances) WissKI(slug string) (i WissKI, err error) {
sql := instances.SQL
if err := sql.Wait(); err != nil {
if err := sql.WaitQueryTable(); err != nil {
return i, err
}
table, err := sql.OpenBookkeeping(false)
table, err := sql.QueryTable(false, models.InstanceTable)
if err != nil {
return i, err
}
// find the instance by slug
query := table.Where(&bookkeeping.Instance{Slug: slug}).Find(&i.Instance)
query := table.Where(&models.Instance{Slug: slug}).Find(&i.Instance)
switch {
case query.Error != nil:
return i, errSQL.WithMessageF(query.Error)
@ -62,11 +62,11 @@ func (instances *Instances) WissKI(slug string) (i WissKI, err error) {
// It does not perform any checks on the WissKI itself.
func (instances *Instances) Has(slug string) (ok bool, err error) {
sql := instances.SQL
if err := sql.Wait(); err != nil {
if err := sql.WaitQueryTable(); err != nil {
return false, err
}
table, err := sql.OpenBookkeeping(false)
table, err := sql.QueryTable(false, models.InstanceTable)
if err != nil {
return false, err
}
@ -106,12 +106,12 @@ func (instances *Instances) Load(slugs ...string) ([]WissKI, error) {
// find finds instances based on the provided query
func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB) (results []WissKI, err error) {
sql := instances.SQL
if err := sql.Wait(); err != nil {
if err := sql.WaitQueryTable(); err != nil {
return nil, err
}
// open the bookkeeping table
table, err := sql.OpenBookkeeping(false)
table, err := sql.QueryTable(false, models.InstanceTable)
if err != nil {
return nil, err
}
@ -126,7 +126,7 @@ func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB
}
// fetch bookkeeping instances
var bks []bookkeeping.Instance
var bks []models.Instance
find = find.Find(&bks)
if find.Error != nil {
return nil, errSQL.WithMessageF(find.Error)

View file

@ -1,13 +1,11 @@
package instances
import (
"github.com/FAU-CDI/wisski-distillery/internal/bookkeeping"
)
import "github.com/FAU-CDI/wisski-distillery/internal/models"
// WissKI represents a single WissKI Instance
type WissKI struct {
// Whatever is stored inside the bookkeeping database
bookkeeping.Instance
models.Instance
// Credentials to Drupal
DrupalUsername string
@ -19,7 +17,7 @@ type WissKI struct {
// Save saves this instance in the bookkeeping table
func (wisski *WissKI) Save() error {
db, err := wisski.instances.SQL.OpenBookkeeping(false)
db, err := wisski.instances.SQL.QueryTable(false, models.InstanceTable)
if err != nil {
return err
}
@ -35,7 +33,7 @@ func (wisski *WissKI) Save() error {
// Delete deletes this instance from the bookkeeping table
func (wisski *WissKI) Delete() error {
db, err := wisski.instances.SQL.OpenBookkeeping(false)
db, err := wisski.instances.SQL.QueryTable(false, models.InstanceTable)
if err != nil {
return err
}