/
degree.go
95 lines (74 loc) · 2.89 KB
/
degree.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
package controllers
import (
"context"
"net/http"
"time"
"github.com/UTDNebula/nebula-api/api/common/log"
"github.com/UTDNebula/nebula-api/api/configs"
"github.com/UTDNebula/nebula-api/api/responses"
"github.com/UTDNebula/nebula-api/api/schema"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var degreeCollection *mongo.Collection = configs.GetCollection(configs.DB, "degrees")
func DegreeSearch() gin.HandlerFunc {
return func(c *gin.Context) {
//name := c.Query("name") // value of specific query parameter: string
//queryParams := c.Request.URL.Query() // map of all query params: map[string][]string
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var degrees []schema.Degree
defer cancel()
// build query key value pairs (only one value per key)
query, err := schema.FilterQuery[schema.Degree](c)
if err != nil {
c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "schema validation error", Data: err.Error()})
return
}
optionLimit, err := configs.GetOptionLimit(&query, c)
if err != nil {
log.WriteErrorWithMsg(err, log.OffsetNotTypeInteger)
c.JSON(http.StatusConflict, responses.ErrorResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
return
}
// get cursor for query results
cursor, err := degreeCollection.Find(ctx, query, optionLimit)
if err != nil {
log.WriteError(err)
c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "error", Data: err.Error()})
return
}
// retrieve and parse all valid documents
if err = cursor.All(ctx, °rees); err != nil {
log.WritePanic(err)
panic(err)
}
// return result
c.JSON(http.StatusOK, responses.MultiDegreeResponse{Status: http.StatusOK, Message: "success", Data: degrees})
}
}
func DegreeById() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
degreeId := c.Param("id")
var degree schema.Degree
defer cancel()
// parse object id from id parameter
objId, err := primitive.ObjectIDFromHex(degreeId)
if err != nil {
log.WriteError(err)
c.JSON(http.StatusBadRequest, responses.ErrorResponse{Status: http.StatusBadRequest, Message: "error", Data: err.Error()})
return
}
// find and parse matching degree
err = degreeCollection.FindOne(ctx, bson.M{"_id": objId}).Decode(°ree)
if err != nil {
log.WriteError(err)
c.JSON(http.StatusInternalServerError, responses.ErrorResponse{Status: http.StatusInternalServerError, Message: "error", Data: err.Error()})
return
}
// return result
c.JSON(http.StatusOK, responses.SingleDegreeResponse{Status: http.StatusOK, Message: "success", Data: degree})
}
}