Showing
3 changed files
with
91 additions
and
0 deletions
api/endpoints/files.go
0 → 100644
1 | +package endpoints | ||
2 | + | ||
3 | +import ( | ||
4 | + "classroom/functions" | ||
5 | + "classroom/models" | ||
6 | + "database/sql" | ||
7 | + "net/http" | ||
8 | + | ||
9 | + "github.com/julienschmidt/httprouter" | ||
10 | +) | ||
11 | + | ||
12 | +// GET /files | ||
13 | +func (e *Endpoints) FilesGet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
14 | + // Get user email | ||
15 | + var email string | ||
16 | + if _email, ok := r.Header["X-User-Email"]; ok { | ||
17 | + email = _email[0] | ||
18 | + } else { | ||
19 | + functions.ResponseError(w, 401, "X-User-Email 헤더를 보내세요.") | ||
20 | + return | ||
21 | + } | ||
22 | + | ||
23 | + // Permission Check | ||
24 | + var isSuper int | ||
25 | + row := e.DB.QueryRow(` | ||
26 | + SELECT is_super FROM users WHERE email=?; | ||
27 | + `, email) | ||
28 | + if err := row.Scan(&isSuper); err != nil { | ||
29 | + if err == sql.ErrNoRows { | ||
30 | + functions.ResponseError(w, 401, "해당 유저가 존재하지 않습니다.") | ||
31 | + return | ||
32 | + } | ||
33 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
34 | + return | ||
35 | + } | ||
36 | + if isSuper == 0 { | ||
37 | + functions.ResponseError(w, 403, "접근 권한 부족. 관리자만 허용된 기능입니다.") | ||
38 | + return | ||
39 | + } | ||
40 | + | ||
41 | + // Result Resp | ||
42 | + resp := models.FilesGetResponse{} | ||
43 | + resp.Files = []models.FilesGetItem{} | ||
44 | + | ||
45 | + // Querying | ||
46 | + rows, err := e.DB.Query(` | ||
47 | + SELECT id, name, created_at FROM files ORDER BY created_at DESC;`) | ||
48 | + if err != nil { | ||
49 | + if err == sql.ErrNoRows { | ||
50 | + resp.FilesCount = 0 | ||
51 | + functions.ResponseOK(w, "success", resp) | ||
52 | + return | ||
53 | + } | ||
54 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
55 | + return | ||
56 | + } | ||
57 | + defer rows.Close() | ||
58 | + | ||
59 | + for rows.Next() { | ||
60 | + var fileID, fileName, createdAtStr string | ||
61 | + err := rows.Scan(&fileID, &fileName, &createdAtStr) | ||
62 | + if err != nil { | ||
63 | + continue | ||
64 | + } | ||
65 | + | ||
66 | + temp := models.FilesGetItem{ | ||
67 | + FileID: fileID, | ||
68 | + FileName: fileName, | ||
69 | + CreatedAt: functions.ToKST(createdAtStr), | ||
70 | + } | ||
71 | + resp.Files = append(resp.Files, temp) | ||
72 | + } | ||
73 | + | ||
74 | + // Struct for response | ||
75 | + resp.FilesCount = len(resp.Files) | ||
76 | + | ||
77 | + functions.ResponseOK(w, "success", resp) | ||
78 | +} |
... | @@ -74,6 +74,7 @@ func main() { | ... | @@ -74,6 +74,7 @@ func main() { |
74 | router.GET("/api", ep.IndexGet) | 74 | router.GET("/api", ep.IndexGet) |
75 | router.GET("/api/users", ep.UsersGet) | 75 | router.GET("/api/users", ep.UsersGet) |
76 | router.POST("/api/users", ep.UsersPost) | 76 | router.POST("/api/users", ep.UsersPost) |
77 | + router.GET("/api/files", ep.FilesGet) | ||
77 | router.GET("/api/files/:file_id/:sheet_id/cell", ep.CellGet) | 78 | router.GET("/api/files/:file_id/:sheet_id/cell", ep.CellGet) |
78 | router.POST("/api/files/:file_id/:sheet_id/reservation", ep.ReservationPost) | 79 | router.POST("/api/files/:file_id/:sheet_id/reservation", ep.ReservationPost) |
79 | router.DELETE("/api/files/:file_id/:sheet_id/reservation/:reservation_id", ep.ReservationDelete) | 80 | router.DELETE("/api/files/:file_id/:sheet_id/reservation/:reservation_id", ep.ReservationDelete) | ... | ... |
api/models/files.go
0 → 100644
-
Please register or login to post a comment