feat: added nostr and review routes #19
3
.vscode/settings.json
vendored
Normal file
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": "eslint . --ext ts --report-unused-disable-directives --max-warnings 0",
|
||||||
"lint:fix": "eslint . --fix --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": "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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import express, { Express } from 'express'
|
import express, { Express } from 'express'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import { connectToDatabase } from './services/database.service'
|
import { connectToDatabase } from './services/database.service'
|
||||||
import { usersRouter } from './routes/users.router'
|
import { usersRouter, nostrRouter, reviewRouter } from './routes'
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
@ -11,6 +11,8 @@ const port = process.env.PORT || 3000
|
|||||||
connectToDatabase()
|
connectToDatabase()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
app.use('/users', usersRouter)
|
app.use('/users', usersRouter)
|
||||||
|
app.use('/nostr', nostrRouter)
|
||||||
|
app.use('/review', reviewRouter)
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server started at http://localhost:${port}`)
|
console.log(`Server started at http://localhost:${port}`)
|
||||||
|
3
src/models/index.ts
Normal file
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
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
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'
|
import { ObjectId } from 'mongodb'
|
||||||
|
|
||||||
export default class User {
|
export class User {
|
||||||
constructor(
|
constructor(
|
||||||
public name: string,
|
public name: string,
|
||||||
public id?: ObjectId
|
public id?: ObjectId
|
||||||
|
3
src/routes/index.ts
Normal file
3
src/routes/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './users.router'
|
||||||
|
export * from './nostr.router'
|
||||||
|
export * from './review.router'
|
48
src/routes/nostr.router.ts
Normal file
48
src/routes/nostr.router.ts
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
45
src/routes/review.router.ts
Normal file
45
src/routes/review.router.ts
Normal file
@ -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
|
// External Dependencies
|
||||||
import express, { Request, Response } from 'express'
|
import express, { Request, Response } from 'express'
|
||||||
import { collections } from '../services/database.service'
|
import { collections } from '../services/database.service'
|
||||||
import User from '../models/user'
|
import { User } from '../models'
|
||||||
|
|
||||||
// Global Config
|
// Global Config
|
||||||
export const usersRouter = express.Router()
|
export const usersRouter = express.Router()
|
||||||
@ -14,9 +14,12 @@ usersRouter.get('/', async (_req: Request, res: Response) => {
|
|||||||
const users = await collections.users?.find({}).toArray()
|
const users = await collections.users?.find({}).toArray()
|
||||||
|
|
||||||
res.status(200).send(users)
|
res.status(200).send(users)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
} catch (error: unknown) {
|
||||||
} catch (error: any) {
|
console.error(error)
|
||||||
res.status(500).send(error.message)
|
|
||||||
|
if (error instanceof Error) {
|
||||||
|
res.status(500).send(error.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -33,9 +36,11 @@ usersRouter.post('/', async (req: Request, res: Response) => {
|
|||||||
} else {
|
} else {
|
||||||
res.status(500).send('Failed to create a new user.')
|
res.status(500).send('Failed to create a new user.')
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
} catch (error) {
|
||||||
} catch (error: any) {
|
|
||||||
console.error(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 mongoDB from 'mongodb'
|
||||||
import * as dotenv from 'dotenv'
|
import * as dotenv from 'dotenv'
|
||||||
|
import { DBcollections } from '../types'
|
||||||
|
|
||||||
// Global Variables
|
// 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
|
// Initialize Connection
|
||||||
export async function connectToDatabase() {
|
export async function connectToDatabase() {
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
const { DB_CONN_STRING } = process.env
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
process.env.DB_CONN_STRING as any
|
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()
|
await client.connect()
|
||||||
|
|
||||||
const db: mongoDB.Db = client.db(process.env.DB_NAME)
|
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.users = usersCollection
|
||||||
|
collections.nostrEvents = nostrEventsCollection
|
||||||
|
collections.reviews = reviewsCollection
|
||||||
|
|
||||||
console.log(
|
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
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
1
src/types/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './database'
|
Loading…
x
Reference in New Issue
Block a user