2025-07-14 17:16:51 +01:00
|
|
|
import { sortByDate, sortByTitle } from "../lib/sorters.js"
|
2025-07-17 17:57:11 +01:00
|
|
|
import { GET_ALL_ENTRIES, GET_ENTRY, GET_ENTRIES_FOR_TAG } from "../sql/entries.js"
|
2025-07-14 17:16:51 +01:00
|
|
|
|
2025-07-14 17:41:55 +01:00
|
|
|
export default class EntriesService {
|
2025-07-14 17:16:51 +01:00
|
|
|
database
|
|
|
|
|
|
|
|
|
|
constructor(database) {
|
|
|
|
|
this.database = database
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEntry = (title) => {
|
|
|
|
|
return this.database.prepare(GET_ENTRY).get(title)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAllEntries = (sort, limit) => {
|
|
|
|
|
const entries = this.database
|
|
|
|
|
.prepare(GET_ALL_ENTRIES)
|
|
|
|
|
.all()
|
|
|
|
|
.slice(0, Number(limit) || -1)
|
|
|
|
|
|
2025-07-14 17:46:08 +01:00
|
|
|
return {
|
|
|
|
|
count: entries.length,
|
|
|
|
|
entries: sort === "date" ? sortByDate(entries) : sortByTitle(entries),
|
2025-07-14 17:16:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-17 17:57:11 +01:00
|
|
|
|
|
|
|
|
getEntriesForTag = (tag, sort) => {
|
|
|
|
|
const entries = this.database.prepare(GET_ENTRIES_FOR_TAG).all(tag)
|
|
|
|
|
return {
|
|
|
|
|
count: entries.length,
|
|
|
|
|
entries: sort === "date" ? this._sortByDate(entries) : this._sortByTitle(entries, "entry_title"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sortByTitle = (entries, fieldName) => {
|
|
|
|
|
return entries.sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sortByDate = (entries, fieldName = last_modified) => {
|
|
|
|
|
return entries.sort((a, b) => new Date(b[fieldName]) - new Date(a[fieldName]))
|
|
|
|
|
}
|
2025-02-09 18:26:53 +00:00
|
|
|
}
|