change to GORM, add member and rank handlers
This commit is contained in:
142
api/db/course.go
Normal file
142
api/db/course.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package db
|
||||
|
||||
import "context"
|
||||
|
||||
/*
|
||||
DDL
|
||||
|
||||
CREATE TABLE `courses` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(100) NOT NULL,
|
||||
`short_name` varchar(10) NOT NULL,
|
||||
`category` varchar(100) NOT NULL,
|
||||
`description` varchar(1000) DEFAULT NULL,
|
||||
`image_url` varchar(255) DEFAULT NULL,
|
||||
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||||
`deleted` tinyint(4) DEFAULT 0,
|
||||
`prereq_id` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `name` (`name`),
|
||||
UNIQUE KEY `shortName` (`short_name`) USING BTREE
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8mb4;
|
||||
*/
|
||||
|
||||
type Course struct {
|
||||
ObjectBase
|
||||
Name string `json:"name"`
|
||||
ShortName string `json:"short_name"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
ImageURL string `json:"image_url"`
|
||||
|
||||
PassedMembers []Member `json:"passed_members" gorm:"many2many:member_courses_pass;"`
|
||||
FailedMembers []Member `json:"failed_members" gorm:"many2many:member_courses_fail;"`
|
||||
Prerequisites []*Course `json:"prerequisites" gorm:"many2many:course_prerequisites;"`
|
||||
TrainingEvents []TrainingEvent `json:"training_events"`
|
||||
}
|
||||
|
||||
// Get one by id
|
||||
func (c *Course) GetByID(id int) error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.First(c, id).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// Get one by name
|
||||
func (c *Course) GetByName(name string) error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.Where("name = ?", name).First(c).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// Get all
|
||||
func (c *Course) GetAll() ([]Course, error) {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var courses []Course
|
||||
err = db.Find(&courses).Error
|
||||
return courses, err
|
||||
}
|
||||
|
||||
// Get all training events for a course
|
||||
func (c *Course) GetTrainingEvents(ctx context.Context, id int) ([]TrainingEvent, error) {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var events []TrainingEvent
|
||||
err = db.WithContext(ctx).Where("course_id = ?", id).Find(&events).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// Get all prerequisites for a course
|
||||
func (c *Course) GetPrerequisites(ctx context.Context, id int) ([]Course, error) {
|
||||
var prereqs []Course
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = db.WithContext(ctx).Model(c).Where("id = ?", id).Association("Prerequisites").Find(&prereqs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return prereqs, nil
|
||||
}
|
||||
|
||||
// Create a new course
|
||||
func (c *Course) Create(ctx context.Context, course Course) error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.WithContext(ctx).Create(course).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update a course
|
||||
func (c *Course) Update(ctx context.Context, course Course) error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.WithContext(ctx).Save(course).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete a course
|
||||
func (c *Course) Delete(ctx context.Context, course Course) error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = db.WithContext(ctx).Delete(course).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user