Migrate fully to sqlite based backend

This commit is contained in:
2023-09-22 14:34:32 +01:00
parent 03fb7268a8
commit 9cff775cf4
15 changed files with 478 additions and 158 deletions
@@ -1,24 +0,0 @@
import { NumistaType } from "@/types/numista";
import { readFile, readdir } from "fs/promises";
import path from "path";
export const getCountryBanknotes = async (country: string) => {
try {
const banknoteIds = await readdir(path.join("./public/banknotes", country));
const banknotes: NumistaType[] = [];
for (const banknoteId of banknoteIds) {
const banknote = await readFile(
path.join("./public/banknotes", country, banknoteId),
{ encoding: "utf-8" },
);
banknotes.push(JSON.parse(banknote));
}
return banknotes;
} catch {
return [];
}
};
-24
View File
@@ -1,24 +0,0 @@
import { NumistaType } from "@/types/numista";
import { readFile, readdir } from "fs/promises";
import path from "path";
export const getCountryCoins = async (country: string) => {
try {
const coinIds = await readdir(path.join("./public/coins", country));
const coins: NumistaType[] = [];
for (const coinId of coinIds) {
const coin = await readFile(
path.join("./public/coins", country, coinId),
{ encoding: "utf-8" },
);
coins.push(JSON.parse(coin));
}
return coins;
} catch {
return [];
}
};
+68
View File
@@ -0,0 +1,68 @@
import { prisma } from "@/api";
export const getCountryCounts = async (category?: "coins" | "banknotes") => {
const countryBanknoteCounts =
category !== "coins"
? await prisma.numistaType.groupBy({
by: ["issuer_code"],
_sum: {
count: true,
},
_count: true,
where: {
category: {
equals: "banknote",
},
},
})
: [];
const countryCoinCounts =
category !== "banknotes"
? await prisma.numistaType.groupBy({
by: ["issuer_code"],
_sum: {
count: true,
},
_count: true,
where: {
category: {
not: "banknote",
},
},
})
: [];
const countries = await prisma.issuer.findMany({
orderBy: {
name: "asc",
},
});
const map = countries.map((country) => {
const banknoteCounts = countryBanknoteCounts.find(
({ issuer_code }) => issuer_code === country.code,
);
const coinCounts = countryCoinCounts.find(
({ issuer_code }) => issuer_code === country.code,
);
return {
...country,
...(banknoteCounts && {
banknotes: {
sum: banknoteCounts?._sum?.count,
count: banknoteCounts?._count,
},
}),
...(coinCounts && {
coins: {
sum: coinCounts?._sum?.count,
count: coinCounts?._count,
},
}),
};
});
return map;
};
+3
View File
@@ -0,0 +1,3 @@
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
+14
View File
@@ -0,0 +1,14 @@
import { NumistaType } from "@/types/numista";
import { prisma } from "@/api";
import { include } from "./relations";
export const getType = async (id: number) => {
const type = await prisma.numistaType.findUnique({
include,
where: {
id,
},
});
return type as unknown as NumistaType | null;
};
+165
View File
@@ -0,0 +1,165 @@
import { NumistaCategory, NumistaType } from "@/types/numista";
import { Prisma } from "@prisma/client";
import { prisma } from "@/api";
import { include } from "./relations";
type Props = {
search?: string;
category?: Exclude<NumistaCategory, "exonumia">;
country?: string;
currency?: string;
faceValue?: number;
yearRange?: [number, number];
special?: "yes" | "no" | "both";
};
export const getTypes = async ({
search,
category,
country,
currency,
faceValue,
yearRange,
special,
}: Props) => {
const AND: Prisma.NumistaTypeWhereInput[] = [];
const OR: Prisma.NumistaTypeWhereInput[] = [];
if (category === "banknote") {
AND.push({
category,
});
}
if (category === "coin") {
AND.push({
category: {
not: "banknote",
},
});
}
if (country) {
AND.push({
issuer: {
OR: [
{
code: country,
},
{
name: country,
},
],
},
});
}
if (currency) {
AND.push({
value: {
currency: {
OR: [{ name: currency }, { full_name: currency }],
},
},
});
}
if (faceValue) {
AND.push({
value: {
numeric_value: {
equals: faceValue,
},
},
});
}
if (yearRange?.length === 2) {
AND.push({
AND: {
min_year: {
lte: yearRange[0],
},
max_year: {
gte: yearRange[1],
},
},
});
}
if (special === "yes") {
AND.push({
NOT: {
type: {
contains: "Standard",
},
},
});
}
if (special === "no") {
AND.push({
type: {
contains: "Standard",
},
});
}
if (search) {
OR.push({ title: { contains: search } });
OR.push({
issuer: {
OR: [
{
code: country,
},
{
name: country,
},
],
},
});
OR.push({
value: {
currency: {
OR: [{ name: currency }, { full_name: currency }],
},
},
});
}
if (AND.length) {
OR.push({ AND });
}
const types = await prisma.numistaType.findMany({
include,
...(OR.length > 0
? {
where: {
OR,
},
}
: {}),
orderBy: [
{
issuer: {
name: "asc",
},
},
{
min_year: "asc",
},
{
max_year: "asc",
},
{
value: {
numeric_value: "asc",
},
},
],
});
return types as unknown as NumistaType[];
};
+13
View File
@@ -0,0 +1,13 @@
import { Prisma } from "@prisma/client";
export const include: Prisma.NumistaTypeInclude = {
issuer: true,
obverse: true,
reverse: true,
edge: true,
value: {
include: {
currency: true,
},
},
};