This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
/
routers.go
119 lines (98 loc) · 3.18 KB
/
routers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Mandatory exercise 1
*
* Mandatory exercse 1
*
* API version: 1.0.0
*/
package main
import (
"log"
"strconv"
"github.com/ArneProductions/DISYS-exercise-1/endpoints"
"github.com/ArneProductions/DISYS-exercise-1/repository"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func SetupRouter(db *gorm.DB) {
router := gin.Default()
setupRoutes(router, db)
err := router.Run()
if err != nil {
log.Fatal(err)
}
}
func convertToUInt(name string) gin.HandlerFunc {
return func(ctx *gin.Context) {
val := ctx.Params.ByName(name)
if val == "" {
return
}
conv, err := strconv.ParseUint(val, 10, 64)
if err != nil {
ctx.Error(err)
}
ctx.Set(name+"_int", conv)
ctx.Next()
}
}
func setupRoutes(router *gin.Engine, db *gorm.DB) {
// Define repositories
userRepository := repository.NewSqliteUserRepository(db)
satisfactionRepository := repository.NewSqliteSatisfactionRepository(db)
courseRepository := repository.NewSqliteCourseRepository(db)
// Create controllers
userController := endpoints.NewUserController(userRepository)
satisfactionController := endpoints.NewSatisfactionController(satisfactionRepository)
courseController := endpoints.NewCourseController(courseRepository)
workloadRepository := repository.NewSqliteWorkloadRepository(db)
studentWorkloadRepository := repository.NewSqliteStudentWorkloadRepository(db)
workloadController := endpoints.NewWorkloadController(workloadRepository, studentWorkloadRepository)
v1 := router.Group("/v1")
{
users := v1.Group("users")
{
users.POST("/", userController.CreateUser)
users.GET("/", userController.GetUsers)
usersWithId := users.Group(":userId")
{
usersWithId.Use(convertToUInt("userId"))
usersWithId.PUT("/", userController.UpdateUser)
usersWithId.GET("/", userController.GetUser)
usersWithId.DELETE("/", userController.DeleteUser)
}
}
courses := v1.Group("course")
{
courses.POST("/", courseController.AddCourse)
courses.GET("/", courseController.GetCourses)
coursesWithId := courses.Group(":courseId")
{
coursesWithId.Use(convertToUInt("courseId"))
coursesWithId.PUT("/addStudent", courseController.AddStudentsToCourse)
coursesWithId.DELETE("/", courseController.DeleteCourse)
coursesWithIdAndStudentId := coursesWithId.Group("student/:studentId")
{
coursesWithIdAndStudentId.Use(convertToUInt("studentId"))
coursesWithIdAndStudentId.DELETE("/", courseController.RemoveStudentFromCourse)
}
}
}
satisfactions := v1.Group("satisfaction")
{
satisfactions.Use(convertToUInt("courseId"))
satisfactions.Use(convertToUInt("studentId"))
satisfactions.GET("/course/:courseId", satisfactionController.GetCourseSatisfaction)
satisfactions.POST("/", satisfactionController.AddSatisfaction)
satisfactions.GET("/student/:studentId", satisfactionController.GetStudentSatisfaction)
}
workloads := v1.Group("workload")
{
workloads.Use(convertToUInt("courseId"))
workloads.Use(convertToUInt("studentId"))
workloads.GET("/:courseId/:studentId", workloadController.GetStudentWorkloadFromCourse)
workloads.POST("/", workloadController.AddWorkload)
workloads.POST("/student", workloadController.AddStudentWorkload)
}
}
}