/
subject.go
83 lines (67 loc) · 2.58 KB
/
subject.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
package public
import (
"net/http"
"github.com/Projeto-USPY/uspy-backend/entity/models"
"github.com/Projeto-USPY/uspy-backend/entity/views"
"github.com/gin-gonic/gin"
)
// GetInstitutes takes the institute models and returns its response view objects
func GetInstitutes(ctx *gin.Context, institutes []models.Institute) {
viewInstitutes := make([]*views.Institute, 0, len(institutes))
for _, inst := range institutes {
viewInstitutes = append(viewInstitutes, views.NewInstituteFromModel(&inst))
}
ctx.JSON(http.StatusOK, viewInstitutes)
}
// GetCourses takes the course models and returns its response view objects
func GetCourses(ctx *gin.Context, courses []*models.Course) {
viewCourses := make([]*views.Course, 0, len(courses))
for _, course := range courses {
viewCourses = append(viewCourses, views.NewCourseFromModel(course))
}
ctx.JSON(http.StatusOK, viewCourses)
}
// GetProfessors takes the professor models and returns its response view objects
func GetProfessors(ctx *gin.Context, professors []*models.Professor) {
viewProfessors := make([]*views.Professor, 0, len(professors))
for _, prof := range professors {
viewProfessors = append(viewProfessors, views.NewProfessorFromModel(prof))
}
ctx.JSON(http.StatusOK, viewProfessors)
}
func ListSubjects(ctx *gin.Context, model *models.Course) {
ctx.JSON(http.StatusOK, views.NewCourseFromModel(model))
}
// SearchSubjects takes the course model and returns its response view object.
func SearchSubjects(ctx *gin.Context, model *models.Course) {
ctx.JSON(http.StatusOK, views.NewCourseFromModel(model))
}
// Get takes the subject model and returns its response view object
func Get(ctx *gin.Context, model *models.Subject) {
ctx.JSON(http.StatusOK, views.NewSubjectFromModel(model))
}
// GetRelations takes the subject model and its weak and strong successors
// It returns the view object for its graph
func GetRelations(ctx *gin.Context, model *models.Subject, weak, strong []models.Subject) {
subView := views.NewSubjectFromModel(model)
graph := views.SubjectGraph{Predecessors: subView.Requirements}
for _, w := range weak {
graph.Successors = append(graph.Successors, views.Requirement{
Subject: w.Code,
Name: w.Name,
Strong: false,
})
}
for _, w := range strong {
graph.Successors = append(graph.Successors, views.Requirement{
Subject: w.Code,
Name: w.Name,
Strong: true,
})
}
ctx.JSON(http.StatusOK, graph)
}
// GetSiblingSubjects is a dummy function that returns the sibling subjects
func GetSiblingSubjects(ctx *gin.Context, siblings []*views.SubjectSibling) {
ctx.JSON(http.StatusOK, siblings)
}