Skip to content

Commit

Permalink
[ADD] 하루제목 전체 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
ssong915 committed Jul 17, 2022
1 parent 5e79e37 commit 50c108c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 41 deletions.
3 changes: 0 additions & 3 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
"arrowParens": "always",
"endOfLine": "lf"
}



31 changes: 31 additions & 0 deletions src/controllers/DailyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const deleteDaily = async (req: Request, res: Response) => {
}

};

/**
* @route GET /daily/keyword
* @desc get top 9 keywords
Expand Down Expand Up @@ -170,10 +171,40 @@ const deleteDaily = async (req: Request, res: Response) => {

};

/**
* @route GET /daily/title?year=&month=
* @desc Get All HappicTitle
* @access Public
*/
const getAllTitle = async (req: Request, res: Response) => {
try {
const {userId} = req.params;
const data = await DailyService.getAllTitle(userId as string,);
if (!data) {
return res
.status(statusCode.NOT_FOUND)
.send(util.fail(statusCode.NOT_FOUND, message.NOT_FOUND));
}
return res
.status(statusCode.OK)
.send(util.success(statusCode.OK, message.GET_ALLTITLE_SUCCESS, data));
} catch (error) {
console.log(error);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(
util.fail(
statusCode.INTERNAL_SERVER_ERROR,
message.INTERNAL_SERVER_ERROR,
),
);
}
};


export default {
createDaily,
deleteDaily,
postedDaily,
getTopKeyword,
getAllDaily,
getAllTitle,
};
2 changes: 1 addition & 1 deletion src/interfaces/film/FilmResponseDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { KeywordInfo } from '../keyword/KeywordInfo';

export interface FilmResponseDto {
id: String;
day: Number;
createdAt: Number;
thumbnail: String;
}
3 changes: 2 additions & 1 deletion src/modules/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const message = {
CREATE_DAILY_SUCCESS: '하루 해픽 생성 성공',
GET_POSTED_DAILY:'하루해픽 생성 여부 확인 성공',
GET_TOP9_KEYWORDS_SUCCESS: '하루해픽 최다 키워드 조회 성공',
READ_ALLDAILY_SUCCESS: '하루 해픽 전체 조회 성공',
READ_ALLDAILY_SUCCESS: '하루해픽 전체 조회 성공',
GET_ALLTITLE_SUCCESS:'하루제목 전체 조회 성공',
};

export default message;
1 change: 1 addition & 0 deletions src/routes/DailyRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ router.post(
);
router.get('/posted/:userId',DailyController.postedDaily);
router.get('/keyword/:userId',DailyController.getTopKeyword);
router.get('/title/:userId',DailyController.getAllTitle);
router.delete('/:filmId', DailyController.deleteDaily);

export default router;
110 changes: 74 additions & 36 deletions src/services/DailyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,75 +229,74 @@ const deleteDaily = async (filmId: string): Promise<void> => {
}
};

const postedDaily = async (
userId: string,
): Promise<object|null> => {
const postedDaily = async (userId: string): Promise<object | null> => {
try {
const dayjs = require('dayjs');
const films = await Film.find({writer:userId}).sort({createAt:-1});
var isPosted = false
const films = await Film.find({ writer: userId }).sort({ createAt: -1 });

var isPosted = false;

if (!films) {
return {isPosted: isPosted};
return { isPosted: isPosted };
}

const lastDate = dayjs(films[0].createdAt);
const lastDateFormat = lastDate.format("YY-MM-DD");
const lastDateFormat = lastDate.format('YY-MM-DD');

const nowDate = dayjs();
const nowDateFormat = nowDate.format("YY-MM-DD");

const todayPosted = (lastDateFormat === nowDateFormat)
return {isPosted:todayPosted };
const nowDateFormat = nowDate.format('YY-MM-DD');

const todayPosted = lastDateFormat === nowDateFormat;
return { isPosted: todayPosted };
} catch (error) {
console.log(error);
throw error;
}
};

const getTopKeyword = async (
userId: String,
): Promise<object> => {

const getTopKeyword = async (userId: String): Promise<object> => {
try {
let where_keywords: KeywordInfo[] = [];
let who_keywords: KeywordInfo[] = [];
let what_keywords: KeywordInfo[] = [];

const where: String[] =[];
const who: String[] =[];
const what: String[] =[];
const where: String[] = [];
const who: String[] = [];
const what: String[] = [];
const dayjs = require('dayjs');

where_keywords = await Keyword.find({writer:userId,category:'where'}).sort({count:-1});
who_keywords = await Keyword.find({writer:userId,category:'who'}).sort({count:-1});
what_keywords = await Keyword.find({writer:userId,category:'what'}).sort({count:-1});
where_keywords = await Keyword.find({
writer: userId,
category: 'where',
}).sort({ count: -1 });
who_keywords = await Keyword.find({ writer: userId, category: 'who' }).sort(
{ count: -1 }
);
what_keywords = await Keyword.find({
writer: userId,
category: 'what',
}).sort({ count: -1 });

for(var i =0;i<9;i++){
if(!where_keywords[i]){
for (var i = 0; i < 9; i++) {
if (!where_keywords[i]) {
break;
}
else{
} else {
where.push(where_keywords[i].content);
}
}

for(var i =0;i<9;i++){
if(!who_keywords[i]){
for (var i = 0; i < 9; i++) {
if (!who_keywords[i]) {
break;
}
else{
} else {
who.push(who_keywords[i].content);
}
}

for(var i =0;i<9;i++){
if(!what_keywords[i]){
for (var i = 0; i < 9; i++) {
if (!what_keywords[i]) {
break;
}
else{
} else {
what.push(what_keywords[i].content);
}
}
Expand All @@ -308,7 +307,7 @@ const getTopKeyword = async (
currentDate,
where,
who,
what
what,
};

return data;
Expand All @@ -318,13 +317,52 @@ const getTopKeyword = async (
}
};

const getAllTitle = async (userId: String): Promise<object[] | null> => {
try {
const films = await Film.find({ writer: userId });
const data: object[] = [];

for (var i = 0; i < films.length; i++) {
const id: String = films[i].id;
const date: Date = films[i].createdAt;
var when: String = '';
var where: String = '';
var who: String = '';
var what: String = '';

const keywordArr = films[i].keyword;
for (var j = 0; j < keywordArr.length; j++) {
const keyword = await Keyword.findOne({ _id: keywordArr[j] });
if (keyword) {
if (keyword.category == 'when') when = keyword.content;
else if (keyword.category == 'where') where = keyword.content;
else if (keyword.category == 'who') who = keyword.content;
else if (keyword.category == 'what') what = keyword.content;
}
}
data.push({ id, date, when, where, who, what });
}

if (films.length === 0) {
return null;
}

return data;
} catch (error) {
console.log(error);
throw error;
}
};

export default {
createDaily,
deleteDaily,
getAllDaily,
postedDaily,
getTopKeyword,
getAllTitle,
};

function dayjs(createdAt: Date) {
throw new Error('Function not implemented.');
}

0 comments on commit 50c108c

Please sign in to comment.