/
classes.go
45 lines (37 loc) 路 1.17 KB
/
classes.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
package api
import (
"net/http"
"github.com/aureleoules/epitaf/models"
"github.com/gin-gonic/gin"
)
func handleClasses() {
router.GET("/classes", getClassesHandler)
}
// @Summary Get classes
// @Tags classes
// @Description Get list of all registered classes
// @Success 200 "OK"
// @Failure 401 "Unauthorized"
// @Failure 500 "Server error"
// @Router /classes [GET]
func getClassesHandler(c *gin.Context) {
classes, err := models.GetClasses()
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
type region map[string][]string
type semester map[string]region
type f map[int]semester
var formatted f = make(f)
for _, c := range classes {
if formatted[int(c.Promotion.Int64Value())] == nil {
formatted[int(c.Promotion.Int64Value())] = make(semester)
}
if formatted[int(c.Promotion.Int64Value())][c.Semester.String()] == nil {
formatted[int(c.Promotion.Int64Value())][c.Semester.String()] = make(region)
}
formatted[int(c.Promotion.Int64Value())][c.Semester.String()][c.Region.String()] = append(formatted[int(c.Promotion.Int64Value())][c.Semester.String()][c.Region.String()], c.Class.String())
}
c.JSON(http.StatusOK, formatted)
}