From 4173f00432e00eef02630920cc789296bcb8e241 Mon Sep 17 00:00:00 2001 From: nostrdev-com <support@nostrdev.com> Date: Mon, 31 Mar 2025 15:58:49 +0300 Subject: [PATCH] feat: added sake model and routes --- .vscode/settings.json | 2 +- src/index.ts | 9 ++++++- src/models/index.ts | 1 + src/models/sake.ts | 29 ++++++++++++++++++++ src/routes/index.ts | 1 + src/routes/sake.router.ts | 46 ++++++++++++++++++++++++++++++++ src/services/database.service.ts | 3 +++ src/types/database.ts | 3 ++- src/types/products.ts | 8 ++++++ src/types/routes.ts | 3 ++- 10 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/models/sake.ts create mode 100644 src/routes/sake.router.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index a26694b..9ac967d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["biodynamic", "Nostr", "npub", "RRP", "screwcap"] + "cSpell.words": ["biodynamic", "koji", "Nostr", "npub", "RRP", "screwcap"] } diff --git a/src/index.ts b/src/index.ts index 9f865f2..b4166ab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,13 @@ import express, { Express } from 'express' import dotenv from 'dotenv' import { connectToDatabase } from './services/database.service' -import { usersRouter, nostrRouter, reviewsRouter, winesRouter } from './routes' +import { + usersRouter, + nostrRouter, + reviewsRouter, + winesRouter, + sakeRouter +} from './routes' import { Routes } from './types' dotenv.config() @@ -15,6 +21,7 @@ connectToDatabase() app.use(Routes.NostrEvents, nostrRouter) app.use(Routes.Reviews, reviewsRouter) app.use(Routes.Wines, winesRouter) + app.use(Routes.Sake, sakeRouter) app.listen(port, () => { console.log(`Server started at http://localhost:${port}`) diff --git a/src/models/index.ts b/src/models/index.ts index 36838dc..1a8481d 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -2,3 +2,4 @@ export * from './user' export * from './nostrEvent' export * from './review' export * from './wine' +export * from './sake' diff --git a/src/models/sake.ts b/src/models/sake.ts new file mode 100644 index 0000000..07c268e --- /dev/null +++ b/src/models/sake.ts @@ -0,0 +1,29 @@ +import { ObjectId } from 'mongodb' +import { SakeDesignation } from '../types' +import { Alpha2Code } from 'i18n-iso-countries' +import { CurrencyCode } from 'currency-codes-ts/dist/types' + +export class Sake { + constructor( + public productCodeEAN: string, // Article Number (https://en.wikipedia.org/wiki/International_Article_Number) + public productCodeUPC: string, // Product Code (https://en.wikipedia.org/wiki/Universal_Product_Code) + public productCodeSKU: string, // Stock keeping unit (https://en.wikipedia.org/wiki/Stock_keeping_unit) + public country: Alpha2Code, // two-letter country codes defined in ISO 3166-1 (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) + public region: string, // appellation, village, sub-region, vineyard + public name: string, // label + public producerId: ObjectId, // product producer + public designation: SakeDesignation, // table, pure, blended, mirin: new/true/salt + public polishRate: number, // % + public starter: string, // koji + public yeastStrain: number, + public alcohol: number, // alcohol percentage + public standardDrinks: number, // number representing an amount of standard drinks per bottle + public vintage: string, // year, nv (non-vintage) or mv (multi-vintage) + public RRPamount: number, // 20 + public RRPcurrency: CurrencyCode, // USD + public description: string, // detailed description of the product + public url?: string, // e.g. producer's website + public image?: string, // (optional image URL)cellar.social + public id?: ObjectId // database object id + ) {} +} diff --git a/src/routes/index.ts b/src/routes/index.ts index 9a7e1e0..ebf0de0 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -2,3 +2,4 @@ export * from './users.router' export * from './nostr.router' export * from './reviews.router' export * from './wines.router' +export * from './sake.router' diff --git a/src/routes/sake.router.ts b/src/routes/sake.router.ts new file mode 100644 index 0000000..4bdda38 --- /dev/null +++ b/src/routes/sake.router.ts @@ -0,0 +1,46 @@ +import express, { Request, Response } from 'express' +import { collections } from '../services/database.service' +import { Sake } from '../models' + +// Global Config +export const sakeRouter = express.Router() + +sakeRouter.use(express.json()) + +// GET +sakeRouter.get('/', async (_req: Request, res: Response) => { + try { + const sake = await collections.sake?.find({}).toArray() + + res.status(200).send(sake) + } catch (error: unknown) { + console.error(error) + + if (error instanceof Error) { + res.status(500).send(error.message) + } + } +}) + +// POST +sakeRouter.post('/', async (req: Request, res: Response) => { + try { + const sake = req.body as Sake + + const result = await collections.sake?.insertOne(sake) + + if (result) { + res + .status(201) + .send(`Successfully created a new sake with id ${result.insertedId}`) + } else { + res.status(500).send('Failed to create a new sake.') + } + } catch (error: unknown) { + console.error(error) + + if (error instanceof Error) { + res.status(400).send(error.message) + } + } +}) diff --git a/src/services/database.service.ts b/src/services/database.service.ts index 1d2691c..af6d89c 100644 --- a/src/services/database.service.ts +++ b/src/services/database.service.ts @@ -8,6 +8,7 @@ export const collections: { [DBcollections.NostrEvents]?: mongoDB.Collection [DBcollections.Reviews]?: mongoDB.Collection [DBcollections.Wines]?: mongoDB.Collection + [DBcollections.Sake]?: mongoDB.Collection } = {} // Initialize Connection @@ -34,11 +35,13 @@ export async function connectToDatabase() { DBcollections.Reviews ) const winesCollection: mongoDB.Collection = db.collection(DBcollections.Wines) + const sakeCollection: mongoDB.Collection = db.collection(DBcollections.Sake) collections.users = usersCollection collections.nostrEvents = nostrEventsCollection collections.reviews = reviewsCollection collections.wines = winesCollection + collections.sake = sakeCollection console.log( `Successfully connected to database: ${db.databaseName} and collections: diff --git a/src/types/database.ts b/src/types/database.ts index 8990fe4..e08b3ef 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -2,5 +2,6 @@ export enum DBcollections { Users = 'users', NostrEvents = 'nostrEvents', Reviews = 'reviews', - Wines = 'wines' + Wines = 'wines', + Sake = 'sake' } diff --git a/src/types/products.ts b/src/types/products.ts index b3af0b5..29c3b53 100644 --- a/src/types/products.ts +++ b/src/types/products.ts @@ -5,3 +5,11 @@ export type Viticulture = 'biodynamic' | 'organic' | 'conventional' export type BottleClosure = 'cork' | 'crown-seal' | 'screwcap' export type Availability = 'in stock' | 'out of stock' | 'discontinued' + +export type SakeDesignation = + | 'table' + | 'pure' + | 'blended' + | 'mirin:new' + | 'mirin:true' + | 'mirin:salt' diff --git a/src/types/routes.ts b/src/types/routes.ts index 843e464..013d2c0 100644 --- a/src/types/routes.ts +++ b/src/types/routes.ts @@ -2,5 +2,6 @@ export enum Routes { Users = '/users', NostrEvents = '/nostr', Reviews = '/reviews', - Wines = '/wines' + Wines = '/wines', + Sake = '/sake' }