2023-09-22 19:22:36 +01:00
package model_classes
import (
"database/sql"
"errors"
2024-03-09 09:41:16 +00:00
2024-04-14 14:51:16 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
2023-09-22 19:22:36 +01:00
)
type ModelClass struct {
2024-03-01 23:03:25 +00:00
Id string ` json:"id" `
2024-03-09 09:41:16 +00:00
ModelId string ` json:"model_id" db:"model_id" `
2024-03-01 23:03:25 +00:00
Name string ` json:"name" `
2024-03-09 09:41:16 +00:00
Status int ` json:"status" `
2023-09-22 19:22:36 +01:00
}
2024-03-09 09:41:16 +00:00
func ListClasses ( c * Context , model_id string ) ( cls [ ] * ModelClass , err error ) {
2024-04-14 14:51:16 +01:00
return GetDbMultitple [ ModelClass ] ( c , "model_classes where model_id=$1" , model_id )
2023-09-22 19:22:36 +01:00
}
2023-09-26 20:15:28 +01:00
func ModelHasDataPoints ( db * sql . DB , model_id string ) ( result bool , err error ) {
2024-03-01 23:03:25 +00:00
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
2023-09-26 20:15:28 +01:00
}
2023-09-22 19:22:36 +01:00
var ClassAlreadyExists = errors . New ( "Class aready exists" )
2023-10-02 21:15:31 +01:00
func CreateClass ( db * sql . DB , model_id string , order int , name string ) ( id string , err error ) {
2024-03-01 23:03:25 +00:00
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
2023-09-22 19:22:36 +01:00
}
2023-10-10 12:28:49 +01:00
func GetNumberOfWrongDataPoints ( db * sql . DB , model_id string ) ( number int , err error ) {
2024-03-01 23:03:25 +00:00
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
2023-10-10 12:28:49 +01:00
}