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-13 17:38:16 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9d95ad054c03e6582ae40676e4bad9f87baf49a4
9d95ad05
1 parent
03349a9e
Update: using db transaction
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
9 deletions
api/endpoints/go.mod
api/endpoints/reservation.go
api/endpoints/users.go
api/endpoints/go.mod
View file @
9d95ad0
...
...
@@ -4,7 +4,11 @@ go 1.14
require (
classroom/functions v0.0.0
classroom/models v0.0.0
github.com/julienschmidt/httprouter v1.3.0
)
replace classroom/functions v0.0.0 => ../functions
replace (
classroom/functions v0.0.0 => ../functions
classroom/models v0.0.0 => ../models
)
...
...
api/endpoints/reservation.go
View file @
9d95ad0
...
...
@@ -83,9 +83,17 @@ func (e *Endpoints) ReservationPost(w http.ResponseWriter, r *http.Request, ps h
return
}
// Querying with Transaction
tx
,
err
:=
e
.
DB
.
Begin
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
defer
tx
.
Rollback
()
// Querying (Cell Validation Check)
isPossible
:=
true
rows
,
err
:=
e
.
DB
.
Query
(
`
rows
,
err
:=
tx
.
Query
(
`
SELECT cell_start, cell_end
FROM transactions
WHERE transaction_type=1
...
...
@@ -120,8 +128,8 @@ loopCheckingValidation:
// Result Resp
resp
:=
models
.
ReservationPostResponse
{}
// Querying
(Making a Transaction)
res
,
err
:=
e
.
DB
.
Exec
(
`
// Querying
res
,
err
:=
tx
.
Exec
(
`
INSERT INTO transactions (transaction_type, user_id, timetable_id, lecture, capacity, cell_column, cell_start, cell_end, professor)
VALUES (1, (
SELECT id FROM users WHERE email=?
...
...
@@ -132,6 +140,12 @@ loopCheckingValidation:
return
}
err
=
tx
.
Commit
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
resp
.
TransactionID
,
err
=
res
.
LastInsertId
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
err
.
Error
())
...
...
@@ -193,16 +207,24 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps
}
}
// Querying with Transaction
tx
,
err
:=
e
.
DB
.
Begin
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
defer
tx
.
Rollback
()
// Check Transaction Permission
var
_transactionType
int64
var
_email
string
row
=
e
.
DB
.
QueryRow
(
`
row
=
tx
.
QueryRow
(
`
SELECT u.email, t.transaction_type
FROM transactions AS t, users AS u
WHERE t.user_id=u.id
AND t.transaction_id=?;
`
,
reservationID
)
err
:
=
row
.
Scan
(
&
_email
,
&
_transactionType
)
err
=
row
.
Scan
(
&
_email
,
&
_transactionType
)
if
err
!=
nil
{
if
err
==
sql
.
ErrNoRows
{
functions
.
ResponseError
(
w
,
404
,
"존재하지 않는 예약"
)
...
...
@@ -221,7 +243,7 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps
}
// Querying
res
,
err
:=
e
.
DB
.
Exec
(
`
res
,
err
:=
tx
.
Exec
(
`
UPDATE transactions SET transaction_type=0 WHERE transaction_id=?
`
,
reservationID
)
if
err
!=
nil
{
...
...
@@ -233,5 +255,11 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps
return
}
err
=
tx
.
Commit
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
functions
.
ResponseOK
(
w
,
"success"
,
nil
)
}
...
...
api/endpoints/users.go
View file @
9d95ad0
...
...
@@ -68,8 +68,27 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou
isSuper
=
*
(
reqData
.
IsSuper
)
}
// Querying
result
,
err
:=
e
.
DB
.
Exec
(
`
// Querying with Transaction
tx
,
err
:=
e
.
DB
.
Begin
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
defer
tx
.
Rollback
()
row
=
tx
.
QueryRow
(
`
SELECT count(id)
FROM users
WHERE email=?;
`
,
*
(
reqData
.
Email
))
if
err
=
row
.
Scan
(
&
_count
);
err
==
nil
{
if
_count
>
0
{
functions
.
ResponseError
(
w
,
500
,
"이미 존재하는 이메일입니다."
)
return
}
}
result
,
err
:=
tx
.
Exec
(
`
INSERT INTO users (email, is_super)
VALUES (?, ?);
`
,
*
(
reqData
.
Email
),
isSuper
)
...
...
@@ -78,6 +97,12 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou
return
}
err
=
tx
.
Commit
()
if
err
!=
nil
{
functions
.
ResponseError
(
w
,
500
,
"예기치 못한 에러 : "
+
err
.
Error
())
return
}
// Result
resp
:=
models
.
UsersPostResponse
{}
resp
.
UserID
,
err
=
result
.
LastInsertId
()
...
...
Please
register
or
login
to post a comment