Skip to content

Commit

Permalink
get shop by id
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamc4t committed Apr 4, 2024
1 parent 589ba5f commit 96b5a3d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 51 deletions.
3 changes: 1 addition & 2 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { User } from '../models'
import { createToken } from './jwt'
import jwt, { JwtPayload, VerifyErrors } from 'jsonwebtoken'
import { getJwtFromReq } from '../utils'
import { maxAge } from '../constants/maxAge'

// TODO BEFORE PUBLISH
// more user friendly error messages
Expand All @@ -16,8 +17,6 @@ interface RequestWithBody extends Request {
}
}

const maxAge = 3 * 24 * 60 * 60 // 3 days

async function signup(req: RequestWithBody, res: Response) {
try {
const { username, password } = req.body
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ app.listen(port, () => {
})

app.use('/api/item', itemRouter)
app.use('/api/shop', requireAuthenticated, shopRouter) // can't see if not logged in
app.use('/api/shop', shopRouter)

// app.use('/api/shop', shopRouter)
app.use('/api/user', userRouter)
Expand Down
1 change: 0 additions & 1 deletion src/models/Shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { itemSchema } from './Item'
const shopSchema = new mongoose.Schema({
name: String,
items: [itemSchema],
isActive: { type: Boolean, default: false },
})

const Shop = mongoose.model('Shop', shopSchema)
Expand Down
62 changes: 15 additions & 47 deletions src/routes/ShopRouter.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,32 @@
import express, { Request, Response } from 'express'
import { Request, Response, Router } from 'express'
import { Shop } from '../models'
// import { requireAuthenticated } from '../middleware'

const shopRouter = express.Router()
const shopRouter = Router()

async function setActiveShop(shopId: string) {
try {
await Shop.updateMany({}, { isActive: false })

await Shop.findByIdAndUpdate(shopId, { isActive: true })

console.log(`Shop ${shopId} is now the active shop.`)
} catch (error) {
console.error('Failed to set active shop:', error)
}
}

shopRouter.get('/active', async (req: Request, res: Response) => {
shopRouter.get('/all', async (req: Request, res: Response) => {
try {
const activeShop = await Shop.findOne({ isActive: true })
if (!activeShop) {
return res.status(204).json({ message: 'No active shop found.' })
}
res.status(200).json(activeShop)
const shops = await Shop.find({})
res.status(200).json(shops)
} catch (error) {
res.status(500).json({ message: (error as Error).message })
const message = (error as Error).message
res.status(500).json({ message })
}
})

shopRouter.post('/activate/:id', async (req: Request, res: Response) => {
shopRouter.get('/:id', async (req: Request, res: Response) => {
const { id } = req.params
try {
await setActiveShop(id)
res.status(200).json({ message: `Shop ${id} is now the active shop.` })
const shop = await Shop.findById(id)
if (!shop) {
return res.status(204).json({ message: 'No shop found with that ID' })
}
res.status(200).json(shop)
} catch (error) {
res.status(500).json({ message: (error as Error).message })
}
})

shopRouter.get('/all', async (req: Request, res: Response) => {
try {
const shops = await Shop.find({})
res.status(200).json(shops)
} catch (error) {
const message = (error as Error).message
res.status(500).json({ message })
}
})

shopRouter.post('/new', async (req, res) => {
try {
const newShop = new Shop(req.body)
Expand All @@ -58,23 +38,11 @@ shopRouter.post('/new', async (req, res) => {
}
})

shopRouter.get('/:id', async (req: Request, res: Response) => {
try {
const shop = await Shop.findById(req.params.id)
if (!shop) {
return res.status(404).json({ message: 'Shop not found' })
}
res.status(200).json(shop)
} catch (error) {
const message = (error as Error).message
res.status(500).json({ message })
}
})
shopRouter.patch('/update/:id', async (req: Request, res: Response) => {
try {
const shop = await Shop.findByIdAndUpdate(req.params.id, req.body, { new: true })
if (!shop) {
return res.status(404).json({ message: 'Shop not found' })
return res.status(404).json({ message: 'No shop found with that ID' })
}
res.status(200).json(shop)
} catch (error) {
Expand Down

0 comments on commit 96b5a3d

Please sign in to comment.