Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
shic4-340CT-sem2/routes.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
189 lines (178 sloc)
6.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* routes.js */ | |
import { Router } from 'https://deno.land/x/oak@v6.5.1/mod.ts' | |
import { Handlebars } from 'https://deno.land/x/handlebars/mod.ts' | |
// import { upload } from 'https://cdn.deno.land/oak_upload_middleware/versions/v2/raw/mod.ts' | |
// import { parse } from 'https://deno.land/std/flags/mod.ts' | |
import { login, register } from './modules/accounts.js' | |
import { createCommodity, getCommodities, getCommodityById, deleteCommodityById, offerToCommodity, getOffersByUserId, acceptOffer, confirmOffer } from './modules/commodities.js' | |
import { saveFile, parseQueryString, transformCommodity } from './util.js' | |
const handle = new Handlebars({ defaultLayout: '' }) | |
const router = new Router() | |
// the routes defined here | |
router.get('/', async context => { | |
const authorised = context.cookies.get('authorised') | |
const username = context.cookies.get('username') | |
// judge isSeller when username exist | |
const isSeller = username && username.startsWith("artist") | |
const commodities = await getCommodities(authorised, isSeller) | |
console.log('@@@@@',commodities) | |
const data = { authorised, username, isSeller, commodities } | |
// const result = await getOffersByUserId(authorised, isSeller) | |
const body = await handle.renderView('foo', data) | |
context.response.body = body | |
}) | |
//Login | |
router.get('/login', async context => { | |
const body = await handle.renderView('login') | |
context.response.body = body | |
}) | |
//register | |
router.get('/register', async context => { | |
const body = await handle.renderView('register') | |
context.response.body = body | |
}) | |
// handle register request | |
router.post('/register', async context => { | |
console.log('POST /register') | |
const body = context.request.body({ type: 'form' }) | |
const value = await body.value | |
const obj = Object.fromEntries(value) | |
console.log(obj) | |
await register(obj) | |
context.response.redirect('/login') | |
}) | |
// handle logout request | |
router.get('/logout', context => { | |
// context.cookies.set('authorised', null) // this does the same | |
// clear cookies | |
context.cookies.delete('authorised') | |
context.cookies.delete('username') | |
context.response.redirect('/') | |
}) | |
// handle login request | |
router.post('/login', async context => { | |
const body = context.request.body({ type: 'form' }) | |
const value = await body.value | |
const obj = Object.fromEntries(value) | |
console.log(obj) | |
try { | |
const {id, user} = await login(obj) | |
// save user id and username in cookie after login successfully | |
context.cookies.set('authorised', id) | |
context.cookies.set('username', user) | |
// redirect to home page | |
context.response.redirect('/') | |
} catch(err) { | |
console.log(err) | |
context.response.redirect('/login') | |
} | |
}) | |
// render and return the create commodity page | |
router.get('/create', async context => { | |
const authorised = context.cookies.get('authorised') | |
if(authorised === undefined) context.response.redirect('/') | |
const data = { authorised } | |
const body = await handle.renderView('create', data) | |
context.response.body = body | |
}) | |
// handle create commodity request | |
router.post('/createCommodity', async context => { | |
const authorised = context.cookies.get('authorised') | |
if(authorised === undefined) context.response.redirect('/') | |
try { | |
const body = await context.request.body() | |
const data = await body.value | |
const { picture, ...rest } = data | |
const { base64 } = picture | |
const userId = authorised | |
// save picture to specific path | |
const picturePath = saveFile(base64, userId) | |
// create commodity, picture path is also stored in database | |
const { affectedRows } = await createCommodity({ | |
...rest, | |
picturePath, | |
userId | |
}) | |
const message = affectedRows === 1 ? 'create commodities successfully...' : 'create commodities failed...' | |
context.response.status = 201 | |
context.response.body = JSON.stringify( | |
{ | |
data: { | |
message | |
} | |
} | |
) | |
} catch(err) { | |
context.response.status = 400 | |
context.response.body = JSON.stringify( | |
{ | |
errors: [ | |
{ | |
title: 'a problem occurred', | |
detail: err.message | |
} | |
] | |
} | |
) | |
} | |
}) | |
// get commodity list | |
router.get('/getCommodities', async context => { | |
context.response.body = await getCommodities() | |
}) | |
// get the detail of commodity | |
router.get('/commodity/detail', async context => { | |
const authorised = context.cookies.get('authorised') | |
const username = context.cookies.get('username') | |
const isSeller = username.startsWith("artist") | |
// context.request.url.search can get query string from such as '?id=1',parseQueryString is a function to parse it into an object | |
const {id, buyerId} = parseQueryString(context.request.url.search) | |
const {commodity, offer} = await getCommodityById(id, buyerId) | |
// transform commodity because the detail page only need to show the created date of the commodity but not the create time | |
const result = transformCommodity(commodity) | |
console.log(commodity) | |
const body = await handle.renderView('detail', { | |
authorised, | |
username, | |
commodity: result, | |
offer, | |
isSeller | |
}) | |
context.response.body = body | |
}) | |
//Delete commodity | |
router.get('/commodity/delete', async context => { | |
// context.request.url.search can get query string from such as '?id=1',parseQueryString is a function to parse it into an object | |
const {id} = parseQueryString(context.request.url.search) | |
const result = await deleteCommodityById(id) | |
console.log(result) | |
const commodities = await getCommodities() | |
const body = await handle.renderView('foo', {commodities}) | |
context.response.body = body | |
}) | |
//offer | |
router.post("/offer", async context => { | |
const body = await context.request.body() | |
const {fields:{buyerId, bid, commodityId}} = await body.value.read() | |
const affectedRows = await offerToCommodity({ | |
commodityId, | |
buyerId, | |
bid | |
}) | |
context.response.redirect(`/commodity/detail?id=${commodityId}&buyerId=${buyerId}`) | |
}) | |
//Accept offer | |
router.get('/acceptOffer', async context => { | |
// context.request.url.search can get query string from such as '?id=1',parseQueryString is a function to parse it into an object | |
const {id, buyerId, offerId} = parseQueryString(context.request.url.search) | |
const result = await acceptOffer(id, offerId) | |
context.response.redirect(`/commodity/detail?id=${id}&buyerId=${buyerId}`) | |
}) | |
//confirm offer | |
router.get('/confirmOffer', async context => { | |
// context.request.url.search can get query string from such as '?id=1',parseQueryString is a function to parse it into an object | |
const {id, buyerId} = parseQueryString(context.request.url.search) | |
const result = await confirmOffer(id) | |
context.response.redirect(`/commodity/detail?id=${id}&buyerId=${buyerId}`) | |
}) | |
export default router |