-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19dd1bc
commit 5ae4e2b
Showing
1 changed file
with
74 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,83 @@ | ||
import prisma from '../../prisma/client'; | ||
import prisma from "../../prisma/client"; | ||
|
||
const getDiariesBySearchKeyword = async (keyword: string, page: number) => { | ||
try { | ||
if (keyword.length === 0) { | ||
const getAllDiaries = await prisma.diary.count({ | ||
where: { | ||
isShare: true, | ||
}, | ||
orderBy: { | ||
updated_At: 'desc', | ||
}, | ||
}); | ||
const getDiariesFromKeyword = await prisma.diary.findMany({ | ||
where: { | ||
isShare: true, | ||
}, | ||
skip: page, | ||
take: 15, | ||
try { | ||
let skip = 0; | ||
const take = 15; | ||
// 페이지 번호를 기반으로 건너뛸 항목 수를 계산합니다. | ||
if (page > 1) { | ||
skip = (page - 1) * take; | ||
} | ||
|
||
orderBy: { | ||
updated_At: 'desc', | ||
}, | ||
}); | ||
return { diaries: getDiariesFromKeyword, total: getAllDiaries }; | ||
} | ||
// 키워드가 비어있는 경우의 로직 | ||
if (keyword.length === 0) { | ||
const total = await prisma.diary.count({ | ||
where: { | ||
isShare: true, | ||
}, | ||
}); | ||
|
||
const diaries = await prisma.diary.findMany({ | ||
where: { | ||
isShare: true, | ||
}, | ||
skip, | ||
take, | ||
orderBy: { | ||
updated_At: "desc", | ||
}, | ||
}); | ||
|
||
const getAllDiaries = await prisma.diary.count({ | ||
where: { | ||
isShare: true, | ||
title: { | ||
search: `${keyword}*`, | ||
}, | ||
contents: { | ||
search: `${keyword}*`, | ||
}, | ||
}, | ||
orderBy: { | ||
updated_At: 'desc', | ||
}, | ||
}); | ||
return { diaries, total }; | ||
} | ||
|
||
const getDiariesFromKeyword = await prisma.diary.findMany({ | ||
where: { | ||
isShare: true, | ||
title: { | ||
search: `${keyword}*`, | ||
}, | ||
contents: { | ||
search: `${keyword}`, | ||
}, | ||
}, | ||
skip: page, | ||
take: 15, | ||
// 키워드가 있는 경우의 로직 | ||
const total = await prisma.diary.count({ | ||
where: { | ||
isShare: true, | ||
OR: [ | ||
{ | ||
title: { | ||
contains: keyword, | ||
}, | ||
}, | ||
{ | ||
contents: { | ||
contains: keyword, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
orderBy: { | ||
updated_At: 'desc', | ||
}, | ||
}); | ||
return { diaries: getDiariesFromKeyword, total: getAllDiaries }; | ||
} catch (e) { | ||
throw e; | ||
} | ||
const diaries = await prisma.diary.findMany({ | ||
where: { | ||
isShare: true, | ||
OR: [ | ||
{ | ||
title: { | ||
contains: keyword, | ||
}, | ||
}, | ||
{ | ||
contents: { | ||
contains: keyword, | ||
}, | ||
}, | ||
], | ||
}, | ||
skip, | ||
take, | ||
orderBy: { | ||
updated_At: "desc", | ||
}, | ||
}); | ||
|
||
return { diaries, total }; | ||
} catch (e) { | ||
console.error(e); | ||
throw e; | ||
} | ||
}; | ||
|
||
export { getDiariesBySearchKeyword }; |