mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 09:38:49 +02:00
174 lines
3.7 KiB
Go
174 lines
3.7 KiB
Go
package db
|
|
|
|
import (
|
|
"github.com/pocketbase/dbx"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"github.com/pocketbase/pocketbase/tools/types"
|
|
"htwkalender/data-manager/model"
|
|
)
|
|
|
|
// Ensure SeminarGroup satisfies RecordProxy
|
|
var _ core.RecordProxy = (*SeminarGroup)(nil)
|
|
|
|
type SeminarGroups []*SeminarGroup
|
|
|
|
type SeminarGroup struct {
|
|
core.BaseRecordProxy
|
|
}
|
|
|
|
// Getter and Setter methods
|
|
|
|
func (s *SeminarGroup) University() string {
|
|
return s.GetString("university")
|
|
}
|
|
func (s *SeminarGroup) SetUniversity(val string) {
|
|
s.Set("university", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) GroupShortcut() string {
|
|
return s.GetString("shortcut")
|
|
}
|
|
func (s *SeminarGroup) SetGroupShortcut(val string) {
|
|
s.Set("shortcut", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) GroupId() string {
|
|
return s.GetString("groupId")
|
|
}
|
|
func (s *SeminarGroup) SetGroupId(val string) {
|
|
s.Set("groupId", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) Course() string {
|
|
return s.GetString("course")
|
|
}
|
|
func (s *SeminarGroup) SetCourse(val string) {
|
|
s.Set("course", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) Faculty() string {
|
|
return s.GetString("faculty")
|
|
}
|
|
func (s *SeminarGroup) SetFaculty(val string) {
|
|
s.Set("faculty", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) FacultyId() string {
|
|
return s.GetString("facultyId")
|
|
}
|
|
func (s *SeminarGroup) SetFacultyId(val string) {
|
|
s.Set("facultyId", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) Semester() string {
|
|
return s.GetString("semester")
|
|
}
|
|
func (s *SeminarGroup) SetSemester(val string) {
|
|
s.Set("semester", val)
|
|
}
|
|
|
|
func (s *SeminarGroup) Created() types.DateTime {
|
|
return s.GetDateTime("created")
|
|
}
|
|
|
|
func (s *SeminarGroup) Updated() types.DateTime {
|
|
return s.GetDateTime("updated")
|
|
}
|
|
|
|
// ToModel Model conversion
|
|
func (s *SeminarGroup) ToModel() model.SeminarGroup {
|
|
return model.SeminarGroup{
|
|
University: s.University(),
|
|
GroupShortcut: s.GroupShortcut(),
|
|
GroupId: s.GroupId(),
|
|
Course: s.Course(),
|
|
Faculty: s.Faculty(),
|
|
FacultyId: s.FacultyId(),
|
|
Semester: s.Semester(),
|
|
}
|
|
}
|
|
|
|
func FindCourseByCourseName(app core.App, courseName string) (*SeminarGroup, error) {
|
|
group := &SeminarGroup{}
|
|
err := app.RecordQuery("groups").
|
|
AndWhere(dbx.NewExp("course = {:course}", dbx.Params{"course": courseName})).
|
|
Limit(1).
|
|
One(group)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return group, nil
|
|
}
|
|
|
|
func GetAllCourses(app core.App) ([]string, error) {
|
|
var groups []*SeminarGroup
|
|
err := app.RecordQuery("groups").All(&groups)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var courseArray []string
|
|
for _, g := range groups {
|
|
courseArray = append(courseArray, g.Course())
|
|
}
|
|
|
|
return courseArray, nil
|
|
}
|
|
|
|
func GetAllCoursesForSemester(app core.App, semester string) ([]model.SeminarGroup, error) {
|
|
var groups []*SeminarGroup
|
|
err := app.RecordQuery("groups").
|
|
AndWhere(dbx.NewExp("semester = {:semester}", dbx.Params{"semester": semester})).
|
|
All(&groups)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var models []model.SeminarGroup
|
|
for _, g := range groups {
|
|
models = append(models, g.ToModel())
|
|
}
|
|
return models, nil
|
|
}
|
|
|
|
func GetAllCoursesForSemesterWithEvents(app core.App, semester string) ([]string, error) {
|
|
var results []struct {
|
|
Course string `db:"course"`
|
|
}
|
|
|
|
err := app.DB().NewQuery(`
|
|
SELECT DISTINCT course FROM events WHERE semester = {:semester}
|
|
`).Bind(dbx.Params{"semester": semester}).All(&results)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var courses []string
|
|
for _, r := range results {
|
|
courses = append(courses, r.Course)
|
|
}
|
|
|
|
return courses, nil
|
|
}
|
|
|
|
func SaveGroups(app core.App, seminarGroups []*SeminarGroup) error {
|
|
// Delete all existing
|
|
if _, err := app.DB().Delete("groups", dbx.NewExp("1 = 1")).Execute(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Save new ones
|
|
for _, group := range seminarGroups {
|
|
err := app.Save(group)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|