Skip to content

Commit

Permalink
feat: added courses table
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSilentSage committed Dec 28, 2024
1 parent 774383e commit 36c1c90
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions vitty-backend-api/cli/commands/timetableCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions vitty-backend-api/internal/models/courses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type Courses struct {
CourseId string `gorm:"primaryKey"`
CourseName string
}
1 change: 1 addition & 0 deletions vitty-backend-api/internal/models/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func InitializeModels() {
"User": &User{},
"Timetable": &Timetable{},
"Friend Requests": &FriendRequest{},
"Courses": &Courses{},
}

for name, model := range MODELS {
Expand Down

0 comments on commit 36c1c90

Please sign in to comment.