Merge pull request 'feat: added coffee model and routes' () from schemas into staging

Reviewed-on: 
This commit is contained in:
Otto 2025-03-31 14:02:33 +00:00
commit 638dc9cd42
10 changed files with 103 additions and 4 deletions

10
.vscode/settings.json vendored

@ -1,3 +1,11 @@
{
"cSpell.words": ["biodynamic", "koji", "Nostr", "npub", "RRP", "screwcap"]
"cSpell.words": [
"biodynamic",
"koji",
"Nostr",
"npub",
"RRP",
"screwcap",
"sundried"
]
}

@ -7,7 +7,8 @@ import {
reviewsRouter,
winesRouter,
sakeRouter,
spiritsRouter
spiritsRouter,
coffeeRouter
} from './routes'
import { Routes } from './types'
@ -24,6 +25,7 @@ connectToDatabase()
app.use(Routes.Wines, winesRouter)
app.use(Routes.Sake, sakeRouter)
app.use(Routes.Spirits, spiritsRouter)
app.use(Routes.Coffee, coffeeRouter)
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`)

26
src/models/coffee.ts Normal file

@ -0,0 +1,26 @@
import { ObjectId } from 'mongodb'
import { CoffeeProcessingType } from '../types'
import { Alpha2Code } from 'i18n-iso-countries'
import { CurrencyCode } from 'currency-codes-ts/dist/types'
export class Coffee {
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 origin: string, // origin
public variety: string, // ?
public processingType: CoffeeProcessingType, // processing type
public name: string, // label
public producerId: ObjectId, // product producer
public roast: string, // ?
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
) {}
}

@ -4,3 +4,4 @@ export * from './review'
export * from './wine'
export * from './sake'
export * from './spirit'
export * from './coffee'

@ -0,0 +1,46 @@
import express, { Request, Response } from 'express'
import { collections } from '../services/database.service'
import { Coffee } from '../models'
// Global Config
export const coffeeRouter = express.Router()
coffeeRouter.use(express.json())
// GET
coffeeRouter.get('/', async (_req: Request, res: Response) => {
try {
const coffee = await collections.coffee?.find({}).toArray()
res.status(200).send(coffee)
} catch (error: unknown) {
console.error(error)
if (error instanceof Error) {
res.status(500).send(error.message)
}
}
})
// POST
coffeeRouter.post('/', async (req: Request, res: Response) => {
try {
const coffee = req.body as Coffee
const result = await collections.coffee?.insertOne(coffee)
if (result) {
res
.status(201)
.send(`Successfully created a new coffee with id ${result.insertedId}`)
} else {
res.status(500).send('Failed to create a new coffee.')
}
} catch (error: unknown) {
console.error(error)
if (error instanceof Error) {
res.status(400).send(error.message)
}
}
})

@ -4,3 +4,4 @@ export * from './reviews.router'
export * from './wines.router'
export * from './sake.router'
export * from './spirits.router'
export * from './coffee.router'

@ -10,6 +10,7 @@ export const collections: {
[DBcollections.Wines]?: mongoDB.Collection
[DBcollections.Sake]?: mongoDB.Collection
[DBcollections.Spirits]?: mongoDB.Collection
[DBcollections.Coffee]?: mongoDB.Collection
} = {}
// Initialize Connection
@ -40,6 +41,9 @@ export async function connectToDatabase() {
const spiritsCollection: mongoDB.Collection = db.collection(
DBcollections.Spirits
)
const coffeeCollection: mongoDB.Collection = db.collection(
DBcollections.Coffee
)
collections.users = usersCollection
collections.nostrEvents = nostrEventsCollection
@ -47,6 +51,7 @@ export async function connectToDatabase() {
collections.wines = winesCollection
collections.sake = sakeCollection
collections.spirits = spiritsCollection
collections.coffee = coffeeCollection
console.log(
`Successfully connected to database: ${db.databaseName} and collections:

@ -4,5 +4,6 @@ export enum DBcollections {
Reviews = 'reviews',
Wines = 'wines',
Sake = 'sake',
Spirits = 'spirits'
Spirits = 'spirits',
Coffee = 'coffee'
}

@ -15,3 +15,11 @@ export type SakeDesignation =
| 'mirin:salt'
export type SpiritType = 'white' | 'dark' | 'liqueurs'
export type CoffeeProcessingType =
| 'de-caff'
| 'honey'
| 'semi-dry'
| 'swiss water'
| 'sundried'
| 'washed'

@ -4,5 +4,6 @@ export enum Routes {
Reviews = '/reviews',
Wines = '/wines',
Sake = '/sake',
Spirits = '/spirits'
Spirits = '/spirits',
Coffee = '/coffee'
}