mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 01:28:48 +02:00
fix:#65 reworked new Record Model from Pocketbase Upgrade 0.23
This commit is contained in:
@ -19,7 +19,7 @@ package main
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
||||
// _ "htwkalender/data-manager/migrations"
|
||||
_ "htwkalender/data-manager/migrations"
|
||||
"htwkalender/data-manager/model/serviceModel"
|
||||
"htwkalender/data-manager/service"
|
||||
"htwkalender/data-manager/service/events"
|
||||
|
@ -1,162 +1,173 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
"htwkalender/data-manager/model"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type SeminarGroup struct {
|
||||
University string `db:"university" json:"university"`
|
||||
GroupShortcut string `db:"shortcut" json:"shortcut"`
|
||||
GroupId string `db:"groupId" json:"groupId"`
|
||||
Course string `db:"course" json:"course"`
|
||||
Faculty string `db:"faculty" json:"faculty"`
|
||||
FacultyId string `db:"facultyId" json:"facultyId"`
|
||||
Semester string `db:"semester" json:"semester"`
|
||||
core.BaseModel
|
||||
}
|
||||
|
||||
func (s *SeminarGroup) TableName() string {
|
||||
return "groups"
|
||||
}
|
||||
|
||||
// UniqueKey Should be same as unique constraint in the database
|
||||
func (s *SeminarGroup) UniqueKey() string {
|
||||
return s.Course + s.Semester
|
||||
}
|
||||
|
||||
func (s *SeminarGroup) toSeminarGroupModel() 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 (s *SeminarGroups) toSeminarGroupModels() []model.SeminarGroup {
|
||||
var seminarGroups []model.SeminarGroup
|
||||
for _, group := range *s {
|
||||
seminarGroups = append(seminarGroups, group.toSeminarGroupModel())
|
||||
}
|
||||
return seminarGroups
|
||||
}
|
||||
// Ensure SeminarGroup satisfies RecordProxy
|
||||
var _ core.RecordProxy = (*SeminarGroup)(nil)
|
||||
|
||||
type SeminarGroups []*SeminarGroup
|
||||
|
||||
func SaveGroups(seminarGroups SeminarGroups, app *pocketbase.PocketBase) (SeminarGroups, error) {
|
||||
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)
|
||||
|
||||
// delete all groups from the database
|
||||
execute, err := app.DB().Delete("groups", dbx.NewExp("1 = 1")).Execute()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rowCount, _ := execute.RowsAffected()
|
||||
|
||||
savedGroups := SeminarGroups{}
|
||||
for _, group := range seminarGroups {
|
||||
saveErr := app.Save(group)
|
||||
if saveErr != nil {
|
||||
return nil, saveErr
|
||||
}
|
||||
savedGroups = append(savedGroups, group)
|
||||
}
|
||||
slog.Info("Saved all groups to the database", "insert", len(savedGroups), "deleted", rowCount)
|
||||
|
||||
return savedGroups, nil
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func GetAllCourses(app *pocketbase.PocketBase) []string {
|
||||
|
||||
var courses []struct {
|
||||
CourseShortcut string `db:"course" json:"course"`
|
||||
}
|
||||
|
||||
// get all rooms from event records in the events collection
|
||||
err := app.DB().Select("course").From("groups").All(&courses)
|
||||
func GetAllCourses(app core.App) ([]string, error) {
|
||||
var groups []*SeminarGroup
|
||||
err := app.RecordQuery("groups").All(&groups)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return []string{}
|
||||
}
|
||||
|
||||
var courseArray []string
|
||||
|
||||
for _, course := range courses {
|
||||
courseArray = append(courseArray, course.CourseShortcut)
|
||||
}
|
||||
|
||||
return courseArray
|
||||
}
|
||||
|
||||
func GetAllCoursesForSemester(app *pocketbase.PocketBase, semester string) []model.SeminarGroup {
|
||||
|
||||
var courses SeminarGroups
|
||||
|
||||
// get all courses for a specific semester
|
||||
err := app.DB().Select("*").From("groups").Where(dbx.NewExp("semester = {:semester}", dbx.Params{"semester": semester})).All(&courses)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return courses.toSeminarGroupModels()
|
||||
|
||||
}
|
||||
|
||||
func GetAllCoursesForSemesterWithEvents(app *pocketbase.PocketBase, semester string) ([]string, error) {
|
||||
|
||||
var courses []struct {
|
||||
CourseShortcut string `db:"course" json:"course"`
|
||||
}
|
||||
|
||||
// get all courses from events distinct for a specific semester
|
||||
err := app.DB().Select("course").From("events").Where(dbx.NewExp("semester = {:semester}", dbx.Params{"semester": semester})).Distinct(true).All(&courses)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting groups from database: ", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var courseArray []string
|
||||
|
||||
for _, course := range courses {
|
||||
courseArray = append(courseArray, course.CourseShortcut)
|
||||
for _, g := range groups {
|
||||
courseArray = append(courseArray, g.Course())
|
||||
}
|
||||
|
||||
return courseArray, nil
|
||||
}
|
||||
|
||||
func FindCourseByCourseName(app *pocketbase.PocketBase, courseName string) (model.SeminarGroup, error) {
|
||||
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)
|
||||
|
||||
var course SeminarGroup
|
||||
|
||||
// get the course by its name
|
||||
err := app.DB().Select("*").From("groups").Where(dbx.NewExp("course = {:course}", dbx.Params{"course": courseName})).One(&course)
|
||||
if err != nil {
|
||||
slog.Error("Error while getting group from database: ", "error", err)
|
||||
return model.SeminarGroup{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return course.toSeminarGroupModel(), nil
|
||||
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
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func getSeminarHTML(semester string) (string, error) {
|
||||
|
||||
}
|
||||
|
||||
func FetchSeminarGroups(app *pocketbase.PocketBase) (db.SeminarGroups, error) {
|
||||
func FetchSeminarGroups(base *pocketbase.PocketBase) (db.SeminarGroups, error) {
|
||||
var groups db.SeminarGroups
|
||||
|
||||
semesterString := functions.CalculateSemesterList(time.RealClock{})
|
||||
@ -76,7 +76,7 @@ func FetchSeminarGroups(app *pocketbase.PocketBase) (db.SeminarGroups, error) {
|
||||
// filter duplicates
|
||||
groups = removeDuplicates(groups)
|
||||
|
||||
insertedGroups, dbError := db.SaveGroups(groups, app)
|
||||
insertedGroups, dbError := db.SaveGroups(base.App, groups)
|
||||
if dbError != nil {
|
||||
slog.Error("FetchSeminarGroups", "error", dbError)
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user