Skip to content

Commit

Permalink
feat(api): add wallet API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jun 6, 2024
1 parent e11dbe3 commit 2fa6bc8
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apps/api/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Hono } from 'hono'
import { authMiddleware } from './middlewares/auth'
import authApp from './routes/auth'
import usersApp from './routes/users'
import walletsApp from './routes/wallets'

export const hono = new Hono()

hono.use('*', authMiddleware)

hono.route('/auth', authApp)
hono.route('/users', usersApp)
hono.route('/wallets', walletsApp)
111 changes: 111 additions & 0 deletions apps/api/v1/routes/wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { object, string } from 'zod'
import { getAuthUserStrict } from '../middlewares/auth'
import {
canUserCreateWallet,
canUserDeleteWallet,
canUserUpdateWallet,
createWallet,
deleteWallet,
findUserWallet,
findUserWallets,
getWalletBalance,
updateWallet,
walletWithBalance,
} from '../services/wallet.service'
import { zCreateWallet, zUpdateWallet } from '../validation'

const router = new Hono()

router.get('/wallets', async (c) => {
const user = getAuthUserStrict(c)

const wallets = (await findUserWallets({ user })).map(walletWithBalance)

return c.json(wallets, 200)
})

router.post('/wallets', zValidator('json', zCreateWallet), async (c) => {
const user = getAuthUserStrict(c)

if (!(await canUserCreateWallet({ user }))) {
return c.json({ message: 'forbidden' }, 403)
}

const data = c.req.valid('json')

const wallet = await createWallet({ user, data })

return c.json(walletWithBalance(wallet), 201)
})

router.put(
'/wallets/:walletId',
zValidator('param', object({ walletId: string() })),
zValidator('json', zUpdateWallet),
async (c) => {
const user = getAuthUserStrict(c)
const { walletId } = c.req.valid('param')

const wallet = await findUserWallet({ user, walletId })

if (!wallet) {
return c.json({ message: 'wallet not found' }, 404)
}

if (!(await canUserUpdateWallet({ user, walletId }))) {
return c.json({ message: 'forbidden' }, 403)
}

const data = c.req.valid('json')

const updatedWallet = await updateWallet({ walletId, data })

return c.json(walletWithBalance(updatedWallet), 200)
},
)

router.delete(
'/wallets/:walletId',
zValidator('param', object({ walletId: string() })),
async (c) => {
const user = getAuthUserStrict(c)
const { walletId } = c.req.valid('param')

const wallet = await findUserWallet({ user, walletId })

if (!wallet) {
return c.json({ message: 'wallet not found' }, 404)
}

if (!(await canUserDeleteWallet({ user, walletId }))) {
return c.json({ message: 'forbidden' }, 403)
}

await deleteWallet({ walletId })

return c.json(wallet, 200)
},
)

router.get(
'/wallets/:walletId/balance',
zValidator('param', object({ walletId: string() })),
async (c) => {
const user = getAuthUserStrict(c)
const { walletId } = c.req.valid('param')

const wallet = await findUserWallet({ user, walletId })

if (!wallet) {
return c.json({ message: 'wallet not found' }, 404)
}

const balance = await getWalletBalance({ wallet })

return c.json({ wallet, balance }, 200)
},
)

export default router

0 comments on commit 2fa6bc8

Please sign in to comment.