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-11 18:02:16 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
03349a9e3fcdae66cf1cd6073485e2b7f2d17e36
03349a9e
1 parent
ae71c3c7
Add: new endpoint /api/users
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
100 additions
and
1 deletions
api/endpoints/users.go
api/main.go
api/models/users.go
api/endpoints/users.go
0 → 100644
View file @
03349a9
package
endpoints
import
(
"classroom/functions"
"classroom/models"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
)
// POST /users
func
(
e
*
Endpoints
)
UsersPost
(
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
}
// Check Permission
var
_count
int64
row
:=
e
.
DB
.
QueryRow
(
`
SELECT count(id)
FROM users
WHERE is_super=1
AND email=?;
`
,
email
)
if
err
:=
row
.
Scan
(
&
_count
);
err
==
nil
{
if
_count
<=
0
{
functions
.
ResponseError
(
w
,
403
,
"관리자 권한 부족."
)
return
}
}
else
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
// Parse Request Data
var
isSuper
bool
type
reqDataStruct
struct
{
Email
*
string
`json:"email"`
IsSuper
*
bool
`json:"is_super"`
}
var
reqData
reqDataStruct
if
strings
.
Contains
(
r
.
Header
.
Get
(
"Content-Type"
),
"application/json"
)
{
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
err
.
Error
())
}
json
.
Unmarshal
(
body
,
&
reqData
)
}
else
{
functions
.
ResponseError
(
w
,
400
,
"JSON 형식만 가능합니다."
)
return
}
if
reqData
.
Email
==
nil
{
functions
.
ResponseError
(
w
,
400
,
"파라미터를 전부 보내주세요."
)
return
}
if
reqData
.
IsSuper
==
nil
{
isSuper
=
false
}
else
{
isSuper
=
*
(
reqData
.
IsSuper
)
}
// Querying
result
,
err
:=
e
.
DB
.
Exec
(
`
INSERT INTO users (email, is_super)
VALUES (?, ?);
`
,
*
(
reqData
.
Email
),
isSuper
)
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
// Result
resp
:=
models
.
UsersPostResponse
{}
resp
.
UserID
,
err
=
result
.
LastInsertId
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
functions
.
ResponseOK
(
w
,
"success"
,
resp
)
return
}
api/main.go
View file @
03349a9
...
...
@@ -61,6 +61,7 @@ func main() {
// Router Setting
router
:=
httprouter
.
New
()
router
.
GET
(
"/api"
,
ep
.
IndexGet
)
router
.
POST
(
"/api/users"
,
ep
.
UsersPost
)
router
.
GET
(
"/api/timetables/:file_id/:sheet_id/cell"
,
ep
.
CellGet
)
router
.
POST
(
"/api/timetables/:file_id/:sheet_id/reservation"
,
ep
.
ReservationPost
)
router
.
DELETE
(
"/api/timetables/:file_id/:sheet_id/reservation/:reservation_id"
,
ep
.
ReservationDelete
)
...
...
@@ -69,10 +70,12 @@ func main() {
portStr
:=
strconv
.
Itoa
(
cfg
.
Server
.
Port
)
if
cfg
.
Server
.
LocalMode
{
handler
:=
cors
.
AllowAll
()
.
Handler
(
router
)
hs
:=
make
(
HostSwitch
)
hs
[
"icns.frec.kr:8080"
]
=
handler
// Start Server in Local Mode
log
.
Println
(
"[Local Mode] Starting HTTP API Server on port"
,
portStr
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
portStr
,
h
andler
))
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
portStr
,
h
s
))
}
else
{
// Release Mode
handler
:=
cors
.
AllowAll
()
.
Handler
(
router
)
...
...
api/models/users.go
0 → 100644
View file @
03349a9
package
models
type
UsersPostResponse
struct
{
UserID
int64
`json:"user_id"`
}
Please
register
or
login
to post a comment