Skip to content

Commit 807b63b

Browse files
committed
timetableController 함수 단위로 구조 변경
1 parent 35fbeee commit 807b63b

File tree

2 files changed

+63
-51
lines changed

2 files changed

+63
-51
lines changed

index.zip

37 Bytes
Binary file not shown.

src/timetableController.js

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,58 +53,11 @@ module.exports.POST = function(dynamo, queryparam, postbody, callback) {
5353
token_validation = body["valid"];
5454
// 토큰이 검증 되었으면
5555
if (token_validation) {
56-
//데이터 중복 검사
57-
var params = {
58-
TableName: 'programmers_timetable',
59-
KeyConditionExpression: "user_key = :key and lecture_code = :code",
60-
ExpressionAttributeValues: {
61-
":key": postbody["user_key"],
62-
":code": postbody["code"]
63-
}
64-
};
65-
66-
dynamo.query(params, (err, data) => {
67-
var insertState = true
68-
if (data["Count"] !== 0) insertState = false;
69-
70-
if (insertState) {
71-
//데이터 추가 작업
72-
var params = {
73-
TableName: 'programmers_timetable',
74-
Item: {
75-
"user_key": postbody["user_key"],
76-
"lecture_code": postbody["code"]
77-
}
78-
}
79-
var text = "";
80-
var status = 200;
81-
82-
dynamo.put(params, (err, data) => {
83-
if (err) {
84-
text = JSON.stringify({
85-
"message": "강의 코드 삽입 에러 - " + err
86-
});
87-
status = 500;
88-
} else text = JSON.stringify({
89-
"user_key": postbody["user_key"],
90-
"code" : postbody["code"],
91-
"message": "강의 코드 삽입 성공 !"
92-
});
93-
94-
callback(null, {
95-
'statusCode': status,
96-
'headers': {},
97-
'body': text
98-
});
99-
});
100-
} else callback(null, {
101-
'statusCode': 422,
102-
'body': errorMessage("/programmers/timetable", "POST", "중복되는 데이터가 존재합니다.")
103-
});
104-
});
56+
// 강의 중복검사 & 강의 코드 추가
57+
searchOverlapLecture(dynamo, postbody, callback);
10558
}
10659
// token이 유효하지 않을 때
107-
else{
60+
else {
10861
callback(null, {
10962
'statusCode': 422,
11063
'body': errorMessage("/programmers/timetable", "POST", "유효한 사용자 ID 토큰이 아닙니다. 정확한 프로그래머스 사용자 ID 토큰을 요청해주세요.")
@@ -159,7 +112,7 @@ module.exports.DELETE = function(dynamo, queryparam, postbody, callback) {
159112
status = 422;
160113
} else text = JSON.stringify({
161114
"user_key": postbody["user_key"],
162-
"code" : postbody["code"],
115+
"code": postbody["code"],
163116
"message": "강의 코드 삭제 성공 !"
164117
});
165118
callback(null, {
@@ -180,5 +133,64 @@ module.exports.DELETE = function(dynamo, queryparam, postbody, callback) {
180133
'statusCode': 400,
181134
'body': errorMessage("/programmers/timetable", "DELETE", "user_key, code 요청 변수가 없어 데이터를 삭제 할 수 없습니다.")
182135
});
136+
}
137+
138+
//강의 코드 추가
139+
function insertLecture(dynamo, postbody, callback) {
140+
var params = {
141+
TableName: 'programmers_timetable',
142+
Item: {
143+
"user_key": postbody["user_key"],
144+
"lecture_code": postbody["code"]
145+
}
146+
};
147+
148+
var text = "";
149+
var status = 200;
150+
151+
dynamo.put(params, (err, data) => {
152+
if (err) {
153+
text = JSON.stringify({
154+
"message": "강의 코드 삽입 에러 - " + err
155+
});
156+
status = 500;
157+
} else text = JSON.stringify({
158+
"user_key": postbody["user_key"],
159+
"code": postbody["code"],
160+
"message": "강의 코드 삽입 성공 !"
161+
});
183162

163+
callback(null, {
164+
'statusCode': status,
165+
'headers': {},
166+
'body': text
167+
});
168+
});
169+
}
170+
171+
// 강의 코드 중복 검사
172+
function searchOverlapLecture(dynamo, postbody, callback) {
173+
174+
var params = {
175+
TableName: 'programmers_timetable',
176+
KeyConditionExpression: "user_key = :key and lecture_code = :code",
177+
ExpressionAttributeValues: {
178+
":key": postbody["user_key"],
179+
":code": postbody["code"]
180+
}
181+
};
182+
183+
dynamo.query(params, (err, data) => {
184+
var insertState = true
185+
if (data["Count"] !== 0) insertState = false;
186+
187+
if (insertState) {
188+
//강의 코드 추가 작업
189+
insertLecture(dynamo, postbody, callback);
190+
191+
} else callback(null, {
192+
'statusCode': 422,
193+
'body': errorMessage("/programmers/timetable", "POST", "중복되는 데이터가 존재합니다.")
194+
});
195+
});
184196
}

0 commit comments

Comments
 (0)