85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package model_classes
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
// . "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
)
|
|
|
|
type ModelClass struct {
|
|
Id string
|
|
ModelId string
|
|
Name string
|
|
}
|
|
|
|
func ListClasses(db *sql.DB, model_id string) (cls []ModelClass, err error) {
|
|
|
|
rows, err := db.Query("select id, model_id, name from model_classes where model_id=$1", model_id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var model ModelClass
|
|
err = rows.Scan(&model.Id, &model.ModelId, &model.Name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
cls = append(cls, model)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func ModelHasDataPoints(db *sql.DB, model_id string) (result bool, err error) {
|
|
result = false
|
|
rows, err := db.Query("select mdp.id from model_data_point as mdp join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 limit 1;", model_id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
return rows.Next(), nil
|
|
}
|
|
|
|
var ClassAlreadyExists = errors.New("Class aready exists")
|
|
|
|
func CreateClass(db *sql.DB, model_id string, order int, name string) (id string, err error) {
|
|
id = ""
|
|
rows, err := db.Query("select id from model_classes where model_id=$1 and name=$2;", model_id, name)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
return id, ClassAlreadyExists
|
|
}
|
|
|
|
rows, err = db.Query("insert into model_classes (model_id, name, class_order) values ($1, $2, $3) returning id;", model_id, name, order)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
|
return id, errors.New("Insert did not return anything")
|
|
}
|
|
|
|
err = rows.Scan(&id)
|
|
return
|
|
}
|
|
|
|
func GetNumberOfWrongDataPoints(db *sql.DB, model_id string) (number int, err error) {
|
|
number = 0
|
|
rows, err := db.Query("select count(mdp.id) from model_data_point as mdp join model_classes as mc on mc.id = mdp.class_id where mc.model_id=$1 and mdp.status=-1;", model_id)
|
|
if err != nil { return }
|
|
defer rows.Close()
|
|
// TODO not an error because if there is no result means that there is no need to count
|
|
if !rows.Next() { return }
|
|
err = rows.Scan(&number)
|
|
return
|
|
}
|