forked from pclubiitk/student-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
45 lines (37 loc) · 1.01 KB
/
router.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 main
import (
"net/http"
"github.com/pclubiitk/student-search/database"
"github.com/go-pg/pg"
"github.com/labstack/echo"
)
// StudentSearchRoute is a route for the search.
func StudentSearchRoute(e *echo.Echo, db *pg.DB) {
// A group is a set of endpoints behind the same path
api := e.Group("/api")
api.GET("/students", func(ctx echo.Context) error {
var students []database.Student
err := db.Model(&students).Select()
if err != nil {
return err
}
if students == nil {
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, students)
})
api.GET("/student", func(ctx echo.Context) error {
var students []database.Student
if ctx.QueryParam("username") == "" {
return ctx.NoContent(http.StatusBadRequest)
}
err := db.Model(&students).Where("username LIKE ?", ctx.QueryParam("username")).Select()
if err != nil {
return err
}
if len(students) < 1 {
return ctx.NoContent(http.StatusNotFound)
}
return ctx.JSON(http.StatusOK, students[0])
})
}