From fd1d43c10441874c8f4199c25f24c098ad102e1c Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 12 Oct 2022 10:49:07 +0800 Subject: [PATCH] Add search history repo methods --- packages/api/src/services/search_history.ts | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 packages/api/src/services/search_history.ts diff --git a/packages/api/src/services/search_history.ts b/packages/api/src/services/search_history.ts new file mode 100644 index 000000000..2521ea64a --- /dev/null +++ b/packages/api/src/services/search_history.ts @@ -0,0 +1,38 @@ +import { SearchHistory } from '../entity/search_history' +import { getRepository } from '../entity/utils' + +export const getRecentSearches = async ( + userId: string +): Promise => { + // get top 10 recent searches + return getRepository(SearchHistory).find({ + where: { user: { id: userId } }, + order: { createdAt: 'DESC' }, + take: 10, + }) +} + +export const saveSearchHistory = async ( + userId: string, + term: string +): Promise => { + const searchHistory = new SearchHistory() + searchHistory.user = { id: userId } as any + searchHistory.term = term + searchHistory.createdAt = new Date() + await getRepository(SearchHistory).save(searchHistory) +} + +export const deleteSearchHistory = async (userId: string): Promise => { + await getRepository(SearchHistory).delete({ user: { id: userId } }) +} + +export const deleteSearchHistoryById = async ( + userId: string, + searchHistoryId: string +): Promise => { + await getRepository(SearchHistory).delete({ + user: { id: userId }, + id: searchHistoryId, + }) +}