Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김명현
/
Classroom-Reservation
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
freckie
2020-12-19 17:49:25 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
0acf04c762fe4c8879804bcb636ad64beb095f95
0acf04c7
1 parent
9cb5ad61
Add: endpoint files
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
0 deletions
api/endpoints/files.go
api/main.go
api/models/files.go
api/endpoints/files.go
0 → 100644
View file @
0acf04c
package
endpoints
import
(
"classroom/functions"
"classroom/models"
"database/sql"
"net/http"
"github.com/julienschmidt/httprouter"
)
// GET /files
func
(
e
*
Endpoints
)
FilesGet
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
ps
httprouter
.
Params
)
{
// Get user email
var
email
string
if
_email
,
ok
:=
r
.
Header
[
"X-User-Email"
];
ok
{
email
=
_email
[
0
]
}
else
{
functions
.
ResponseError
(
w
,
401
,
"X-User-Email 헤더를 보내세요."
)
return
}
// Permission Check
var
isSuper
int
row
:=
e
.
DB
.
QueryRow
(
`
SELECT is_super FROM users WHERE email=?;
`
,
email
)
if
err
:=
row
.
Scan
(
&
isSuper
);
err
!=
nil
{
if
err
==
sql
.
ErrNoRows
{
functions
.
ResponseError
(
w
,
401
,
"해당 유저가 존재하지 않습니다."
)
return
}
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
if
isSuper
==
0
{
functions
.
ResponseError
(
w
,
403
,
"접근 권한 부족. 관리자만 허용된 기능입니다."
)
return
}
// Result Resp
resp
:=
models
.
FilesGetResponse
{}
resp
.
Files
=
[]
models
.
FilesGetItem
{}
// Querying
rows
,
err
:=
e
.
DB
.
Query
(
`
SELECT id, name, created_at FROM files ORDER BY created_at DESC;`
)
if
err
!=
nil
{
if
err
==
sql
.
ErrNoRows
{
resp
.
FilesCount
=
0
functions
.
ResponseOK
(
w
,
"success"
,
resp
)
return
}
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
defer
rows
.
Close
()
for
rows
.
Next
()
{
var
fileID
,
fileName
,
createdAtStr
string
err
:=
rows
.
Scan
(
&
fileID
,
&
fileName
,
&
createdAtStr
)
if
err
!=
nil
{
continue
}
temp
:=
models
.
FilesGetItem
{
FileID
:
fileID
,
FileName
:
fileName
,
CreatedAt
:
functions
.
ToKST
(
createdAtStr
),
}
resp
.
Files
=
append
(
resp
.
Files
,
temp
)
}
// Struct for response
resp
.
FilesCount
=
len
(
resp
.
Files
)
functions
.
ResponseOK
(
w
,
"success"
,
resp
)
}
api/main.go
View file @
0acf04c
...
...
@@ -74,6 +74,7 @@ func main() {
router
.
GET
(
"/api"
,
ep
.
IndexGet
)
router
.
GET
(
"/api/users"
,
ep
.
UsersGet
)
router
.
POST
(
"/api/users"
,
ep
.
UsersPost
)
router
.
GET
(
"/api/files"
,
ep
.
FilesGet
)
router
.
GET
(
"/api/files/:file_id/:sheet_id/cell"
,
ep
.
CellGet
)
router
.
POST
(
"/api/files/:file_id/:sheet_id/reservation"
,
ep
.
ReservationPost
)
router
.
DELETE
(
"/api/files/:file_id/:sheet_id/reservation/:reservation_id"
,
ep
.
ReservationDelete
)
...
...
api/models/files.go
0 → 100644
View file @
0acf04c
package
models
type
FilesGetResponse
struct
{
FilesCount
int
`json:"files_count"`
Files
[]
FilesGetItem
`json:"files"`
}
type
FilesGetItem
struct
{
FileID
string
`json:"file_id"`
FileName
string
`json:"file_name"`
CreatedAt
string
`json:"created_at"`
}
Please
register
or
login
to post a comment