Merge pull request 'feat: added nostr and review routes' () from schemas into staging

Reviewed-on: 
This commit is contained in:
Otto 2025-03-27 14:44:11 +00:00
commit 35c2debfae
14 changed files with 174 additions and 18 deletions

3
.vscode/settings.json vendored Normal file

@ -0,0 +1,3 @@
{
"cSpell.words": ["Nostr"]
}

@ -15,7 +15,8 @@
"lint": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
"lint:fix": "eslint . --fix --ext ts --report-unused-disable-directives --max-warnings 0",
"lint:staged": "eslint --fix --ext ts --report-unused-disable-directives --max-warnings 0",
"lint-staged": "lint-staged"
"lint-staged": "lint-staged",
"start:db": "docker compose -f mongo-docker-compose.yml up -d"
},
"repository": {
"type": "git",

@ -1,7 +1,7 @@
import express, { Express } from 'express'
import dotenv from 'dotenv'
import { connectToDatabase } from './services/database.service'
import { usersRouter } from './routes/users.router'
import { usersRouter, nostrRouter, reviewRouter } from './routes'
dotenv.config()
@ -11,6 +11,8 @@ const port = process.env.PORT || 3000
connectToDatabase()
.then(() => {
app.use('/users', usersRouter)
app.use('/nostr', nostrRouter)
app.use('/review', reviewRouter)
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`)

3
src/models/index.ts Normal file

@ -0,0 +1,3 @@
export * from './user'
export * from './nostrEvent'
export * from './review'

10
src/models/nostrEvent.ts Normal file

@ -0,0 +1,10 @@
export class NostrEvent {
constructor(
public id: string,
public pubkey: string,
public created_at: number,
public kind: number,
public tags: [string][],
public content: string
) {}
}

12
src/models/review.ts Normal file

@ -0,0 +1,12 @@
import { ObjectId } from 'mongodb'
export class Review {
constructor(
public eventId: string,
public productId: string,
public rating: number,
public reviewText: string,
public testingNotes: string[],
public id?: ObjectId
) {}
}

@ -1,6 +1,6 @@
import { ObjectId } from 'mongodb'
export default class User {
export class User {
constructor(
public name: string,
public id?: ObjectId

3
src/routes/index.ts Normal file

@ -0,0 +1,3 @@
export * from './users.router'
export * from './nostr.router'
export * from './review.router'

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

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

@ -1,7 +1,7 @@
// External Dependencies
import express, { Request, Response } from 'express'
import { collections } from '../services/database.service'
import User from '../models/user'
import { User } from '../models'
// Global Config
export const usersRouter = express.Router()
@ -14,9 +14,12 @@ usersRouter.get('/', async (_req: Request, res: Response) => {
const users = await collections.users?.find({}).toArray()
res.status(200).send(users)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
res.status(500).send(error.message)
} catch (error: unknown) {
console.error(error)
if (error instanceof Error) {
res.status(500).send(error.message)
}
}
})
@ -33,9 +36,11 @@ usersRouter.post('/', async (req: Request, res: Response) => {
} else {
res.status(500).send('Failed to create a new user.')
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
} catch (error) {
console.error(error)
res.status(400).send(error.message)
if (error instanceof Error) {
res.status(400).send(error.message)
}
}
})

@ -1,28 +1,46 @@
// External Dependencies
import * as mongoDB from 'mongodb'
import * as dotenv from 'dotenv'
import { DBcollections } from '../types'
// Global Variables
export const collections: { users?: mongoDB.Collection } = {}
export const collections: {
[DBcollections.Users]?: mongoDB.Collection
[DBcollections.NostrEvents]?: mongoDB.Collection
[DBcollections.Reviews]?: mongoDB.Collection
} = {}
// Initialize Connection
export async function connectToDatabase() {
dotenv.config()
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
process.env.DB_CONN_STRING as any
)
const { DB_CONN_STRING } = process.env
if (typeof DB_CONN_STRING !== 'string') {
throw new Error(`DB_CONN_STRING environmental variable is missing.`)
}
const client: mongoDB.MongoClient = new mongoDB.MongoClient(DB_CONN_STRING)
await client.connect()
const db: mongoDB.Db = client.db(process.env.DB_NAME)
const usersCollection: mongoDB.Collection = db.collection('users')
const usersCollection: mongoDB.Collection = db.collection(DBcollections.Users)
const nostrEventsCollection: mongoDB.Collection = db.collection(
DBcollections.NostrEvents
)
const reviewsCollection: mongoDB.Collection = db.collection(
DBcollections.Reviews
)
collections.users = usersCollection
collections.nostrEvents = nostrEventsCollection
collections.reviews = reviewsCollection
console.log(
`Successfully connected to database: ${db.databaseName} and collection: ${usersCollection.collectionName}`
`Successfully connected to database: ${db.databaseName} and collections:
${Object.keys(collections)
.map((collection) => '- ' + collection)
.join('\n')}`
)
}

5
src/types/database.ts Normal file

@ -0,0 +1,5 @@
export enum DBcollections {
Users = 'users',
NostrEvents = 'nostrEvents',
Reviews = 'reviews'
}

1
src/types/index.ts Normal file

@ -0,0 +1 @@
export * from './database'