mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Add search history repo methods
This commit is contained in:
parent
ff12675713
commit
fd1d43c104
1 changed files with 38 additions and 0 deletions
38
packages/api/src/services/search_history.ts
Normal file
38
packages/api/src/services/search_history.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { SearchHistory } from '../entity/search_history'
|
||||
import { getRepository } from '../entity/utils'
|
||||
|
||||
export const getRecentSearches = async (
|
||||
userId: string
|
||||
): Promise<SearchHistory[]> => {
|
||||
// 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<void> => {
|
||||
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<void> => {
|
||||
await getRepository(SearchHistory).delete({ user: { id: userId } })
|
||||
}
|
||||
|
||||
export const deleteSearchHistoryById = async (
|
||||
userId: string,
|
||||
searchHistoryId: string
|
||||
): Promise<void> => {
|
||||
await getRepository(SearchHistory).delete({
|
||||
user: { id: userId },
|
||||
id: searchHistoryId,
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue