mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-02 09:49:13 +02:00
fix:#65 updated db model for proxyrecord
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
"htwkalender/data-manager/model"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// Ensure SeminarGroup satisfies RecordProxy
|
||||
@@ -75,6 +76,38 @@ func (s *SeminarGroup) Updated() types.DateTime {
|
||||
return s.GetDateTime("updated")
|
||||
}
|
||||
|
||||
func (s *SeminarGroup) UniqueKey() string {
|
||||
return s.Course() + s.Semester()
|
||||
}
|
||||
|
||||
func NewSeminarGroup(collection *core.Collection, group model.SeminarGroup) (*SeminarGroup, error) {
|
||||
// Create a new SeminarGroup instance
|
||||
if collection.Name != "groups" {
|
||||
return nil, core.ErrInvalidFieldValue
|
||||
}
|
||||
|
||||
record := core.NewRecord(collection)
|
||||
record.Set("id:autogenerate", "")
|
||||
|
||||
sg := newSeminarGroup(record)
|
||||
// Set the fields from the model
|
||||
sg.SetUniversity(group.University)
|
||||
sg.SetGroupShortcut(group.GroupShortcut)
|
||||
sg.SetGroupId(group.GroupId)
|
||||
sg.SetCourse(group.Course)
|
||||
sg.SetFaculty(group.Faculty)
|
||||
sg.SetFacultyId(group.FacultyId)
|
||||
sg.SetSemester(group.Semester)
|
||||
|
||||
return sg, nil
|
||||
}
|
||||
|
||||
func newSeminarGroup(record *core.Record) *SeminarGroup {
|
||||
sg := &SeminarGroup{}
|
||||
sg.SetProxyRecord(record)
|
||||
return sg
|
||||
}
|
||||
|
||||
// ToModel Model conversion
|
||||
func (s *SeminarGroup) ToModel() model.SeminarGroup {
|
||||
return model.SeminarGroup{
|
||||
@@ -88,7 +121,34 @@ func (s *SeminarGroup) ToModel() model.SeminarGroup {
|
||||
}
|
||||
}
|
||||
|
||||
func FindCourseByCourseName(app core.App, courseName string) (*SeminarGroup, error) {
|
||||
func (s *SeminarGroups) ToModelArray() []model.SeminarGroup {
|
||||
models := make([]model.SeminarGroup, len(*s))
|
||||
for i, group := range *s {
|
||||
models[i] = group.ToModel()
|
||||
}
|
||||
return models
|
||||
}
|
||||
|
||||
func (s *SeminarGroup) FromModel(m model.SeminarGroup) {
|
||||
s.SetUniversity(m.University)
|
||||
s.SetGroupShortcut(m.GroupShortcut)
|
||||
s.SetGroupId(m.GroupId)
|
||||
s.SetCourse(m.Course)
|
||||
s.SetFaculty(m.Faculty)
|
||||
s.SetFacultyId(m.FacultyId)
|
||||
s.SetSemester(m.Semester)
|
||||
}
|
||||
|
||||
func (s *SeminarGroups) FromModelArray(m model.SeminarGroups) SeminarGroups {
|
||||
groups := make(SeminarGroups, len(m))
|
||||
for i, group := range m {
|
||||
groups[i] = &SeminarGroup{}
|
||||
groups[i].FromModel(group)
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
func FindCourseByCourseName(app core.App, courseName string) (model.SeminarGroup, error) {
|
||||
group := &SeminarGroup{}
|
||||
err := app.RecordQuery("groups").
|
||||
AndWhere(dbx.NewExp("course = {:course}", dbx.Params{"course": courseName})).
|
||||
@@ -96,10 +156,10 @@ func FindCourseByCourseName(app core.App, courseName string) (*SeminarGroup, err
|
||||
One(group)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return model.SeminarGroup{}, err
|
||||
}
|
||||
|
||||
return group, nil
|
||||
return group.ToModel(), nil
|
||||
}
|
||||
|
||||
func GetAllCourses(app core.App) ([]string, error) {
|
||||
@@ -155,19 +215,38 @@ func GetAllCoursesForSemesterWithEvents(app core.App, semester string) ([]string
|
||||
return courses, nil
|
||||
}
|
||||
|
||||
func SaveGroups(app core.App, seminarGroups []*SeminarGroup) error {
|
||||
func SaveGroups(app core.App, seminarGroups []model.SeminarGroup) (model.SeminarGroups, error) {
|
||||
// Delete all existing
|
||||
if _, err := app.DB().Delete("groups", dbx.NewExp("1 = 1")).Execute(); err != nil {
|
||||
return err
|
||||
execute, err := app.DB().Delete("groups", dbx.NewExp("1 = 1")).Execute()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Check if to delete was successful
|
||||
rowCount, _ := execute.RowsAffected()
|
||||
|
||||
// Gruppen-Collection abrufen
|
||||
collection, err := app.FindCollectionByNameOrId("groups")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Save new ones
|
||||
// Seminargruppen in die Datenbank speichern
|
||||
savedGroups := SeminarGroups{}
|
||||
for _, group := range seminarGroups {
|
||||
err := app.Save(group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
dbSeminarGroup, recordErr := NewSeminarGroup(collection, group)
|
||||
if recordErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveErr := app.Save(dbSeminarGroup)
|
||||
if saveErr != nil {
|
||||
return nil, saveErr
|
||||
}
|
||||
savedGroups = append(savedGroups, dbSeminarGroup)
|
||||
}
|
||||
slog.Info("Saved all groups to the database", "insert", len(savedGroups), "deleted", rowCount)
|
||||
|
||||
return savedGroups.ToModelArray(), nil
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user