Showing
3 changed files
with
66 additions
and
9 deletions
... | @@ -4,7 +4,11 @@ go 1.14 | ... | @@ -4,7 +4,11 @@ go 1.14 |
4 | 4 | ||
5 | require ( | 5 | require ( |
6 | classroom/functions v0.0.0 | 6 | classroom/functions v0.0.0 |
7 | + classroom/models v0.0.0 | ||
7 | github.com/julienschmidt/httprouter v1.3.0 | 8 | github.com/julienschmidt/httprouter v1.3.0 |
8 | ) | 9 | ) |
9 | 10 | ||
10 | -replace classroom/functions v0.0.0 => ../functions | 11 | +replace ( |
12 | + classroom/functions v0.0.0 => ../functions | ||
13 | + classroom/models v0.0.0 => ../models | ||
14 | +) | ... | ... |
... | @@ -83,9 +83,17 @@ func (e *Endpoints) ReservationPost(w http.ResponseWriter, r *http.Request, ps h | ... | @@ -83,9 +83,17 @@ func (e *Endpoints) ReservationPost(w http.ResponseWriter, r *http.Request, ps h |
83 | return | 83 | return |
84 | } | 84 | } |
85 | 85 | ||
86 | + // Querying with Transaction | ||
87 | + tx, err := e.DB.Begin() | ||
88 | + if err != nil { | ||
89 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
90 | + return | ||
91 | + } | ||
92 | + defer tx.Rollback() | ||
93 | + | ||
86 | // Querying (Cell Validation Check) | 94 | // Querying (Cell Validation Check) |
87 | isPossible := true | 95 | isPossible := true |
88 | - rows, err := e.DB.Query(` | 96 | + rows, err := tx.Query(` |
89 | SELECT cell_start, cell_end | 97 | SELECT cell_start, cell_end |
90 | FROM transactions | 98 | FROM transactions |
91 | WHERE transaction_type=1 | 99 | WHERE transaction_type=1 |
... | @@ -120,8 +128,8 @@ loopCheckingValidation: | ... | @@ -120,8 +128,8 @@ loopCheckingValidation: |
120 | // Result Resp | 128 | // Result Resp |
121 | resp := models.ReservationPostResponse{} | 129 | resp := models.ReservationPostResponse{} |
122 | 130 | ||
123 | - // Querying (Making a Transaction) | 131 | + // Querying |
124 | - res, err := e.DB.Exec(` | 132 | + res, err := tx.Exec(` |
125 | INSERT INTO transactions (transaction_type, user_id, timetable_id, lecture, capacity, cell_column, cell_start, cell_end, professor) | 133 | INSERT INTO transactions (transaction_type, user_id, timetable_id, lecture, capacity, cell_column, cell_start, cell_end, professor) |
126 | VALUES (1, ( | 134 | VALUES (1, ( |
127 | SELECT id FROM users WHERE email=? | 135 | SELECT id FROM users WHERE email=? |
... | @@ -132,6 +140,12 @@ loopCheckingValidation: | ... | @@ -132,6 +140,12 @@ loopCheckingValidation: |
132 | return | 140 | return |
133 | } | 141 | } |
134 | 142 | ||
143 | + err = tx.Commit() | ||
144 | + if err != nil { | ||
145 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
146 | + return | ||
147 | + } | ||
148 | + | ||
135 | resp.TransactionID, err = res.LastInsertId() | 149 | resp.TransactionID, err = res.LastInsertId() |
136 | if err != nil { | 150 | if err != nil { |
137 | functions.ResponseError(w, 500, err.Error()) | 151 | functions.ResponseError(w, 500, err.Error()) |
... | @@ -193,16 +207,24 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps | ... | @@ -193,16 +207,24 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps |
193 | } | 207 | } |
194 | } | 208 | } |
195 | 209 | ||
210 | + // Querying with Transaction | ||
211 | + tx, err := e.DB.Begin() | ||
212 | + if err != nil { | ||
213 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
214 | + return | ||
215 | + } | ||
216 | + defer tx.Rollback() | ||
217 | + | ||
196 | // Check Transaction Permission | 218 | // Check Transaction Permission |
197 | var _transactionType int64 | 219 | var _transactionType int64 |
198 | var _email string | 220 | var _email string |
199 | - row = e.DB.QueryRow(` | 221 | + row = tx.QueryRow(` |
200 | SELECT u.email, t.transaction_type | 222 | SELECT u.email, t.transaction_type |
201 | FROM transactions AS t, users AS u | 223 | FROM transactions AS t, users AS u |
202 | WHERE t.user_id=u.id | 224 | WHERE t.user_id=u.id |
203 | AND t.transaction_id=?; | 225 | AND t.transaction_id=?; |
204 | `, reservationID) | 226 | `, reservationID) |
205 | - err := row.Scan(&_email, &_transactionType) | 227 | + err = row.Scan(&_email, &_transactionType) |
206 | if err != nil { | 228 | if err != nil { |
207 | if err == sql.ErrNoRows { | 229 | if err == sql.ErrNoRows { |
208 | functions.ResponseError(w, 404, "존재하지 않는 예약") | 230 | functions.ResponseError(w, 404, "존재하지 않는 예약") |
... | @@ -221,7 +243,7 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps | ... | @@ -221,7 +243,7 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps |
221 | } | 243 | } |
222 | 244 | ||
223 | // Querying | 245 | // Querying |
224 | - res, err := e.DB.Exec(` | 246 | + res, err := tx.Exec(` |
225 | UPDATE transactions SET transaction_type=0 WHERE transaction_id=? | 247 | UPDATE transactions SET transaction_type=0 WHERE transaction_id=? |
226 | `, reservationID) | 248 | `, reservationID) |
227 | if err != nil { | 249 | if err != nil { |
... | @@ -233,5 +255,11 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps | ... | @@ -233,5 +255,11 @@ func (e *Endpoints) ReservationDelete(w http.ResponseWriter, r *http.Request, ps |
233 | return | 255 | return |
234 | } | 256 | } |
235 | 257 | ||
258 | + err = tx.Commit() | ||
259 | + if err != nil { | ||
260 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
261 | + return | ||
262 | + } | ||
263 | + | ||
236 | functions.ResponseOK(w, "success", nil) | 264 | functions.ResponseOK(w, "success", nil) |
237 | } | 265 | } | ... | ... |
... | @@ -68,8 +68,27 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou | ... | @@ -68,8 +68,27 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou |
68 | isSuper = *(reqData.IsSuper) | 68 | isSuper = *(reqData.IsSuper) |
69 | } | 69 | } |
70 | 70 | ||
71 | - // Querying | 71 | + // Querying with Transaction |
72 | - result, err := e.DB.Exec(` | 72 | + tx, err := e.DB.Begin() |
73 | + if err != nil { | ||
74 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
75 | + return | ||
76 | + } | ||
77 | + defer tx.Rollback() | ||
78 | + | ||
79 | + row = tx.QueryRow(` | ||
80 | + SELECT count(id) | ||
81 | + FROM users | ||
82 | + WHERE email=?; | ||
83 | + `, *(reqData.Email)) | ||
84 | + if err = row.Scan(&_count); err == nil { | ||
85 | + if _count > 0 { | ||
86 | + functions.ResponseError(w, 500, "이미 존재하는 이메일입니다.") | ||
87 | + return | ||
88 | + } | ||
89 | + } | ||
90 | + | ||
91 | + result, err := tx.Exec(` | ||
73 | INSERT INTO users (email, is_super) | 92 | INSERT INTO users (email, is_super) |
74 | VALUES (?, ?); | 93 | VALUES (?, ?); |
75 | `, *(reqData.Email), isSuper) | 94 | `, *(reqData.Email), isSuper) |
... | @@ -78,6 +97,12 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou | ... | @@ -78,6 +97,12 @@ func (e *Endpoints) UsersPost(w http.ResponseWriter, r *http.Request, ps httprou |
78 | return | 97 | return |
79 | } | 98 | } |
80 | 99 | ||
100 | + err = tx.Commit() | ||
101 | + if err != nil { | ||
102 | + functions.ResponseError(w, 500, "예기치 못한 에러 : "+err.Error()) | ||
103 | + return | ||
104 | + } | ||
105 | + | ||
81 | // Result | 106 | // Result |
82 | resp := models.UsersPostResponse{} | 107 | resp := models.UsersPostResponse{} |
83 | resp.UserID, err = result.LastInsertId() | 108 | resp.UserID, err = result.LastInsertId() | ... | ... |
-
Please register or login to post a comment