/
offering.go
61 lines (51 loc) · 1.6 KB
/
offering.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
package public
import (
"net/http"
"sort"
"github.com/Projeto-USPY/uspy-backend/entity/models"
"github.com/Projeto-USPY/uspy-backend/entity/views"
"github.com/Projeto-USPY/uspy-backend/utils"
"github.com/gin-gonic/gin"
)
// GetOfferingComments takes the comments models and returns their view response objects
//
// It also sorts comments to display most upvoted (and least downvoted) on top
func GetOfferingComments(ctx *gin.Context, comments []*models.Comment) {
sort.Slice(comments, func(i, j int) bool {
if comments[i].Upvotes == comments[j].Upvotes {
return comments[i].Downvotes < comments[j].Downvotes
}
return comments[i].Upvotes > comments[j].Upvotes
})
results := make([]*views.Comment, 0)
for _, c := range comments {
results = append(results, views.NewCommentFromModel(c))
}
ctx.JSON(http.StatusOK, results)
}
// GetOfferingsWithStats takes the offering models and returns their view response objects
//
// It also takes IDs of the professors for each offering (given that this information is not stored in the model directly)
// Finally, it sorts the offerings to display most interesting ones on top
func GetOfferingsWithStats(
ctx *gin.Context,
IDs []string,
offerings []*models.Offering,
stats []*models.OfferingStats,
limit int,
) {
results := make([]*views.Offering, 0, 20)
for i := 0; i < len(offerings); i++ {
results = append(results,
views.NewOfferingFromModel(
IDs[i],
offerings[i],
stats[i].Approval,
stats[i].Disapproval,
stats[i].Neutral,
),
)
}
views.SortOfferings(results)
ctx.JSON(http.StatusOK, results[:utils.Min(limit, len(results))])
}