Node.js
간단한 Todolist Select API를 만들어보았다!
개발자 갸갸
2019. 8. 26. 18:22
public todolist = async (req: express.Request, res: express.Response) => {
let result: CommunicationResult = new CommunicationResult();
try {
const param_date: any = req.query.date;
let query: any = {};
if(param_date != null) {
const start: Date = new Date(param_date + " 00:00:00");
const end: Date = new Date(param_date + " 23:59:59");
query["date"] = {
$gte: start,
$lte: end
};
}
else {
result.set(CommunicationCode.FAIL_EMPTY_PARAMETER);
result.msg = "파라미터를 검출하지 못했습니다.";
throw result;
}
console.log("query : ", query);
// SQL의 Select로 본다.
// find : query로 검색된 결과를 보여준다. (return array)
const todo: TodoModel = await this.db.findOne(this.TODO_DB_ID, query);
if(todo == undefined) {
result.set(CommunicationCode.DB_FAIL_NO_DATA);
result.msg = "데이터가 없음.";
throw result;
}
let searchQuery: any = {};
searchQuery["date"] = {
$gte: ( req.query.start != undefined) ? moment(req.query.start).toDate() : moment("1970-01-01").toDate(),
$lte: (req.query.end != undefined) ? moment(req.query.end).toDate() : moment("2999-12-31").toDate()
};
const remainderList: ReMmindersInnerModel[] = await this.db.find(this.REMINDER_DB_ID, searchQuery);
res.send({ _COM: result, data: {
date: todo.date,
todo: todo,
remainder: remainderList
}});
} catch (e) {
if (e instanceof CommunicationResult) {
result = e;
Common.log(LogType.error, `[/api/todo/todolist], try ~ catch : ${e.msg}`);
} else {
result.set(CommunicationCode.FAIL_UNKNOWN);
result.msg = e.message;
Common.log(LogType.sev_error, `[/api/todo/todolist], try ~ catch : ${e}`);
}
res.send({ _COM: result });
}
}
간단하게 SELECT 하는 코드를 만들어보았다
DB- MongoDB
NodeJS
NoSQL 쿼리는 정말 편한 것 같다~ 앞으로 crud 도 만들 예정이다