From 774383ec3f48717ad31d921da809aeb5a01021c8 Mon Sep 17 00:00:00 2001 From: TheSilentSage Date: Tue, 11 Jun 2024 19:26:21 +0530 Subject: [PATCH 1/2] fix: merged slot for lab while fetching timetable --- .../internal/models/timetable.go | 63 ++++++++++++------- .../internal/utils/timetableDetection.go | 6 -- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/vitty-backend-api/internal/models/timetable.go b/vitty-backend-api/internal/models/timetable.go index 62d04f1..deeb9a9 100644 --- a/vitty-backend-api/internal/models/timetable.go +++ b/vitty-backend-api/internal/models/timetable.go @@ -29,10 +29,12 @@ func (t Timetable) GetDaySlots(day time.Weekday) map[string][]Slot { resp := make(map[string][]Slot) var data []Slot daySlots := DailySlots[day.String()] + labSlot := "" var err error // Theory slots for _, slot := range t.Slots { + if slot.Type == "Theory" && slices.Contains(daySlots["Theory"], slot.Slot) { index := slices.Index(daySlots["Theory"], slot.Slot) slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, TheoryTimings[index].StartTime, time.Local) @@ -51,19 +53,27 @@ func (t Timetable) GetDaySlots(day time.Weekday) map[string][]Slot { data = append(data, slot) } else if slot.Type == "Lab" && slices.Contains(daySlots["Lab"], slot.Slot) { index := slices.Index(daySlots["Lab"], slot.Slot) - slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].StartTime, time.Local) - if err != nil { - log.Println("Error parsing time: ", err) - return nil - } - slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) + if labSlot == "" { + labSlot += slot.Slot + "+" + continue + } else { + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index-1].StartTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } - if err != nil { - log.Println("Error parsing time: ", err) - return nil + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + slot.Slot = labSlot + slot.Slot + labSlot = "" + data = append(data, slot) } - data = append(data, slot) + } } resp[day.String()] = data @@ -72,9 +82,11 @@ func (t Timetable) GetDaySlots(day time.Weekday) map[string][]Slot { func (t Timetable) GetDaywiseTimetable() map[string][]Slot { resp := make(map[string][]Slot) + labSlot := "" for _, slot := range t.Slots { for day, value := range DailySlots { + if slices.Contains(value["Theory"], slot.Slot) { index := slices.Index(value["Theory"], slot.Slot) var err error @@ -91,18 +103,27 @@ func (t Timetable) GetDaywiseTimetable() map[string][]Slot { resp[day] = append(resp[day], slot) } else if slices.Contains(value["Lab"], slot.Slot) { index := slices.Index(value["Lab"], slot.Slot) - var err error - slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].StartTime, time.Local) - if err != nil { - log.Println("Error parsing time: ", err) - return nil - } - slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) - if err != nil { - log.Println("Error parsing time: ", err) - return nil + + if labSlot == "" { + labSlot += slot.Slot + "+" + continue + } else { + var err error + slot.StartTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index-1].StartTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + slot.EndTime, err = time.ParseInLocation(STD_REF_TIME, LabTimings[index].EndTime, time.Local) + if err != nil { + log.Println("Error parsing time: ", err) + return nil + } + + slot.Slot = labSlot + slot.Slot + labSlot = "" + resp[day] = append(resp[day], slot) } - resp[day] = append(resp[day], slot) } } } diff --git a/vitty-backend-api/internal/utils/timetableDetection.go b/vitty-backend-api/internal/utils/timetableDetection.go index 19ad91d..17bd513 100644 --- a/vitty-backend-api/internal/utils/timetableDetection.go +++ b/vitty-backend-api/internal/utils/timetableDetection.go @@ -48,12 +48,6 @@ func DetectTimetable(text string) ([]TimetableSlotV1, error) { } if len(Slots) == 0 { - return Slots, nil - } - - var err error - - if err != nil { return Slots, errors.New("error in detecting timetable") } From 36c1c902bcee7032f9394a48e54cce9ddc600a4e Mon Sep 17 00:00:00 2001 From: TheSilentSage Date: Sat, 28 Dec 2024 18:41:22 +0530 Subject: [PATCH 2/2] feat: added courses table --- .../cli/commands/timetableCommands.go | 35 +++++++++++++++++++ vitty-backend-api/internal/models/courses.go | 6 ++++ .../internal/models/initialize.go | 1 + 3 files changed, 42 insertions(+) create mode 100644 vitty-backend-api/internal/models/courses.go diff --git a/vitty-backend-api/cli/commands/timetableCommands.go b/vitty-backend-api/cli/commands/timetableCommands.go index daac625..99d2a41 100644 --- a/vitty-backend-api/cli/commands/timetableCommands.go +++ b/vitty-backend-api/cli/commands/timetableCommands.go @@ -22,6 +22,13 @@ var TimetableCommands = []*cli.Command{ Usage: "Fix slot times", Action: fixSlotTimes, }, + { + + Name: "seed-course-table", + Aliases: []string{"sct"}, + Usage: "populate course table", + Action: seedCourseTable, + }, } func parseTimetable(c *cli.Context) error { @@ -73,3 +80,31 @@ func fixSlotTimes(c *cli.Context) error { } return nil } + +func seedCourseTable(c *cli.Context) error { + reset := "\033[0m" + red := "\033[31m" + green := "\033[32m" + cyan := "\033[36m " + + fmt.Print(cyan, "Seeding.. ", reset) + err := database.DB.Exec(` + INSERT INTO courses (course_id, course_name) + SELECT + DISTINCT ON(elems.data->>'code') + elems.data->>'code' AS CourseCode, + elems.data->>'name' AS CourseName + FROM + timetables, + jsonb_array_elements(timetables.slots::jsonb) AS elems(data) + `).Error + + if err != nil { + fmt.Println(red, "Failed") + fmt.Println("Error: ", err, reset) + } + + fmt.Println(green, "Done", reset) + + return nil +} diff --git a/vitty-backend-api/internal/models/courses.go b/vitty-backend-api/internal/models/courses.go new file mode 100644 index 0000000..450d853 --- /dev/null +++ b/vitty-backend-api/internal/models/courses.go @@ -0,0 +1,6 @@ +package models + +type Courses struct { + CourseId string `gorm:"primaryKey"` + CourseName string +} diff --git a/vitty-backend-api/internal/models/initialize.go b/vitty-backend-api/internal/models/initialize.go index 4181fd3..61f7665 100644 --- a/vitty-backend-api/internal/models/initialize.go +++ b/vitty-backend-api/internal/models/initialize.go @@ -11,6 +11,7 @@ func InitializeModels() { "User": &User{}, "Timetable": &Timetable{}, "Friend Requests": &FriendRequest{}, + "Courses": &Courses{}, } for name, model := range MODELS {