feat: added sake model and routes #23
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,3 +1,3 @@
|
||||
{
|
||||
"cSpell.words": ["biodynamic", "Nostr", "npub", "RRP", "screwcap"]
|
||||
"cSpell.words": ["biodynamic", "koji", "Nostr", "npub", "RRP", "screwcap"]
|
||||
}
|
||||
|
@ -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}`)
|
||||
|
@ -2,3 +2,4 @@ export * from './user'
|
||||
export * from './nostrEvent'
|
||||
export * from './review'
|
||||
export * from './wine'
|
||||
export * from './sake'
|
||||
|
29
src/models/sake.ts
Normal file
29
src/models/sake.ts
Normal file
@ -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
|
||||
) {}
|
||||
}
|
@ -2,3 +2,4 @@ export * from './users.router'
|
||||
export * from './nostr.router'
|
||||
export * from './reviews.router'
|
||||
export * from './wines.router'
|
||||
export * from './sake.router'
|
||||
|
46
src/routes/sake.router.ts
Normal file
46
src/routes/sake.router.ts
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
})
|
@ -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:
|
||||
|
@ -2,5 +2,6 @@ export enum DBcollections {
|
||||
Users = 'users',
|
||||
NostrEvents = 'nostrEvents',
|
||||
Reviews = 'reviews',
|
||||
Wines = 'wines'
|
||||
Wines = 'wines',
|
||||
Sake = 'sake'
|
||||
}
|
||||
|
@ -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'
|
||||
|
@ -2,5 +2,6 @@ export enum Routes {
|
||||
Users = '/users',
|
||||
NostrEvents = '/nostr',
|
||||
Reviews = '/reviews',
|
||||
Wines = '/wines'
|
||||
Wines = '/wines',
|
||||
Sake = '/sake'
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user