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?
frontend/server/src/api/api.borrow.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
149 lines (143 sloc)
4.61 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
import express from 'express'; | |
import BorrowService from '../service/BorrowService'; | |
const router = express.Router(); | |
router | |
// authority judgment | |
.use(async (request, response, next) => { | |
try { | |
const userInfo = request.userInfo; | |
if (!userInfo) { | |
response.status('401').json({code: 401, data: 'Unauthorized'}); | |
return; | |
} | |
await next(); | |
} catch (ex) { | |
response.status('400').json({code: 400, data: ex.msg || ex.message || ex}); | |
} | |
}) | |
// add borrow record | |
.post('/', async (request, response) => { | |
try { | |
const userInfo = request.userInfo; | |
const data = request.body; | |
data.userInfo = userInfo; | |
const info = await BorrowService.AddBorrow(data); | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// delete by id | |
.delete('/:id', async (request, response) => { | |
try { | |
if (!request.token) { | |
response.status(401).json({code: 401, data: 'Unauthorized'}); | |
return; | |
} | |
const {id: request_user_id} = request.userInfo; | |
const {id} = request.params; | |
if (!id) { | |
BorrowService.failure('id is not empty'); | |
} | |
const isExists = await BorrowService.count({_id: id, request_user_id}); | |
console.log('isExists:', isExists, id, request_user_id); | |
if (isExists == 0) { | |
BorrowService.failure(`it's not your record. I don't have permission`); | |
} | |
await BorrowService.findByIdAndDelete(id); | |
response.json(BorrowService.success('delete success')); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// get borrow list | |
.get('/my', async (request, response) => { | |
try { | |
if (!request.token) { | |
response.status(401).json({code: 401, data: 'Unauthorized'}); | |
return; | |
} | |
const list = await BorrowService.find({request_user_id: request.userInfo.id}); | |
response.json(BorrowService.success(list)); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// get requester list | |
.get('/request', async (request, response) => { | |
try { | |
if (!request.token) { | |
response.status(401).json({code: 401, data: 'Unauthorized'}); | |
return; | |
} | |
const list = await BorrowService.find({book_user_id: request.userInfo.id}); | |
response.json(BorrowService.success(list)); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// get borrow detail | |
.get('/:id', async (request, response) => { | |
try { | |
const info = await BorrowService.findById(request.params.id); | |
response.json(BorrowService.success(info)); | |
} catch (ex) { | |
response.status('400').json({code: 400, data: ex.msg || ex.message || ex}); | |
} | |
}) | |
// add message | |
.put('/request/msg/:id', async (request, response) => { | |
try { | |
const {id} = request.params; | |
const {content: request_msg} = request.body; | |
const info = await BorrowService.AddRequestMsg(id, request_msg); | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// replay requester message | |
.put('/reply/msg/:id', async (request, response) => { | |
try { | |
const {id} = request.params; | |
const {content: reply_msg} = request.body; | |
const info = await BorrowService.AddReplyMsg(id, reply_msg); | |
// $push | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// refuse requester | |
.put('/refuse/:id', async (request, response) => { | |
try { | |
const {id} = request.params; | |
const {content} = request.body; | |
const info = await BorrowService.Refuse(id, content); | |
// $push | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
// confirm requester | |
.put('/confirm/:id', async (request, response) => { | |
try { | |
const {id} = request.params; | |
const {content} = request.body; | |
const info = await BorrowService.Confirm(id, content); | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}) | |
.put('/return/:id', async (request, response) => { | |
try { | |
const {id} = request.params; | |
const info = await BorrowService.Return(id); | |
response.json(info); | |
} catch (ex) { | |
response.status(400).json({code: 400, msg: ex.msg || ex.message || ex}); | |
} | |
}); | |
export default router; |