From de9fcf13f22d2069a9618045ef25dac62c399574 Mon Sep 17 00:00:00 2001 From: shaoh4 Date: Wed, 23 Dec 2020 10:11:24 +0800 Subject: [PATCH] return books --- client/src/actions/index.js | 221 ++++++++++---------- client/src/components/component.header.js | 2 +- client/src/components/component.module.scss | 8 + client/src/pages/page.book.detail.js | 8 +- client/src/pages/page.borrow.list.js | 14 +- client/src/reducers/reducer.borrow.js | 8 + server/src/api/api.borrow.js | 11 +- server/src/model/model.borrow.js | 2 +- server/src/service/BorrowService.js | 38 +++- 9 files changed, 187 insertions(+), 125 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index e6fa0d9..937b9b1 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -1,15 +1,13 @@ import axios from 'axios'; -import { Subject } from 'rxjs'; +import {Subject} from 'rxjs'; const ApiMap = { - 'development': 'http://127.0.0.1:4900', - 'production': '', -} + development: 'http://127.0.0.1:4900', + production: '', +}; // console.log('process.env.NODE_ENV:', process.env.NODE_ENV); export const ApiService = ApiMap[process.env.NODE_ENV] || ''; -// console.log('api:', ApiService) - export const TypeMap = { LOADING: 'COMMON_LOADING', LOADING_HIDE: 'COMMON_LOADING_HIDE', @@ -40,6 +38,7 @@ export const TypeMap = { BORROW_REQUEST_MSG: 'BORROW_REQUEST_MSG', BORROW_REPLY_MSG: 'BORROW_REPLY_MSG', BORROW_REFUSE: 'BORROW_REFUSE', + BORROW_RETURN: 'BORROW_RETURN', }; export class Utility { @@ -102,9 +101,9 @@ export class Utility { return null; } - static Toast() { } + static Toast() {} - static LoadingHide() { } + static LoadingHide() {} static IsArray(obj) { if (!obj || !Array.isArray(obj) || obj.length === 0) { @@ -115,7 +114,7 @@ export class Utility { } export class Httphelper { - static async __request({ method = 'get', url, headers, params, data, dispatch }) { + static async __request({method = 'get', url, headers, params, data, dispatch}) { try { if (!url) { Utility.Toast('url not found'); @@ -127,24 +126,23 @@ export class Httphelper { if (token) { _headers.token = token; } - const opt = { method, url: _url, headers: _headers, params, data }; + const opt = {method, url: _url, headers: _headers, params, data}; const response = await axios(opt); - const { data: body } = response; + const {data: body} = response; return body.data || body; } catch (ex) { - const { response } = ex; - const { data: errData, status, statusText } = response || {}; - const { msg } = errData || {}; + const {response} = ex; + const {data: errData, status, statusText} = response || {}; + const {msg} = errData || {}; if (status === 401) { Utility.Token = null; Utility.UserInfo = null; alert(msg || statusText); - if (dispatch) { - dispatch({ type: TypeMap.USER_401, payload: '' }); + dispatch({type: TypeMap.USER_401, payload: ''}); } - Utility.NotifyLogout.next({ isLogin: true }); + Utility.NotifyLogout.next({isLogin: true}); console.log(statusText); return Promise.reject(msg); } @@ -163,8 +161,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onPost(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'post', url, headers, params, data, dispatch }); + static async onPost(url, {headers, params, data}, dispatch) { + return this.__request({method: 'post', url, headers, params, data, dispatch}); } /** @@ -174,8 +172,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onPut(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'put', url, headers, params, data, dispatch }); + static async onPut(url, {headers, params, data}, dispatch) { + return this.__request({method: 'put', url, headers, params, data, dispatch}); } /** @@ -185,8 +183,8 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onDelete(url, { headers, params, data }, dispatch) { - return this.__request({ method: 'delete', url, headers, params, data, dispatch }); + static async onDelete(url, {headers, params, data}, dispatch) { + return this.__request({method: 'delete', url, headers, params, data, dispatch}); } /** @@ -196,14 +194,14 @@ export class Httphelper { * @returns * @memberof Httphelper */ - static async onGet(url, { headers, params }, dispatch) { - return this.__request({ method: 'get', url, headers, params, dispatch }); + static async onGet(url, {headers, params}, dispatch) { + return this.__request({method: 'get', url, headers, params, dispatch}); } } // update user info export const userUpdate = (userInfo) => { - return { type: TypeMap.USER_UPDATE, payload: userInfo }; + return {type: TypeMap.USER_UPDATE, payload: userInfo}; }; /** * user login @@ -211,10 +209,10 @@ export const userUpdate = (userInfo) => { */ export const userLogin = (info) => { return async (dispatch) => { - const item = await Httphelper.onPost('/api/user/signin', { data: info }, dispatch); + const item = await Httphelper.onPost('/api/user/signin', {data: info}, dispatch); Utility.Token = item.token; Utility.UserInfo = item; - dispatch({ type: TypeMap.USER_LOGIN, payload: item }); + dispatch({type: TypeMap.USER_LOGIN, payload: item}); }; }; @@ -224,13 +222,13 @@ export const userLogin = (info) => { */ export const userSignUp = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPost('/api/user/signup', { data }, dispatch); - dispatch({ type: TypeMap.USER_SIGNUP, payload: item }); + const item = await Httphelper.onPost('/api/user/signup', {data}, dispatch); + dispatch({type: TypeMap.USER_SIGNUP, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -238,26 +236,26 @@ export const userSignUp = (data) => { // get category list export const categoryList = (params) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/category', { params }, dispatch); - dispatch({ type: TypeMap.CATEGORY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/category', {params}, dispatch); + dispatch({type: TypeMap.CATEGORY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; // get category list export const categoryListAll = (params) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/category/all', { params }, dispatch); - dispatch({ type: TypeMap.CATEGORY_LIST_ALL, payload: item }); + const item = await Httphelper.onGet('/api/category/all', {params}, dispatch); + dispatch({type: TypeMap.CATEGORY_LIST_ALL, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -265,13 +263,13 @@ export const categoryListAll = (params) => { // edit category by id export const categoryEdit = (id, data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/category/' + id, { data }, dispatch); - dispatch({ type: TypeMap.CATEGORY_DELETE, payload: item }); + const item = await Httphelper.onPut('/api/category/' + id, {data}, dispatch); + dispatch({type: TypeMap.CATEGORY_DELETE, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -279,15 +277,15 @@ export const categoryEdit = (id, data) => { // search book by keyword export const bookSearch = (keyword, page = 1, size = 20) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - dispatch({ type: TypeMap.BOOK_SEARCH_KEYWORD, payload: { keyword, page, size } }); - const item = await Httphelper.onGet('/api/book/search', { params: { page, size, keyword } }, dispatch); + dispatch({type: TypeMap.BOOK_SEARCH_KEYWORD, payload: {keyword, page, size}}); + const item = await Httphelper.onGet('/api/book/search', {params: {page, size, keyword}}, dispatch); - dispatch({ type: TypeMap.BOOK_SEARCH, payload: item }); + dispatch({type: TypeMap.BOOK_SEARCH, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -295,13 +293,13 @@ export const bookSearch = (keyword, page = 1, size = 20) => { // search book by keyword export const bookListByMy = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/book/my', { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_MY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/book/my', {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_MY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -309,13 +307,13 @@ export const bookListByMy = () => { // get book detail info by id export const bookDetail = (id) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/book/' + id, { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_DETAIL, payload: item }); + const item = await Httphelper.onGet('/api/book/' + id, {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_DETAIL, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -323,13 +321,13 @@ export const bookDetail = (id) => { // delete book by id export const bookDelete = (id, index) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onDelete('/api/book/' + id, { params: {} }, dispatch); - dispatch({ type: TypeMap.BOOK_MY_DELETE, payload: { id, index } }); + const item = await Httphelper.onDelete('/api/book/' + id, {params: {}}, dispatch); + dispatch({type: TypeMap.BOOK_MY_DELETE, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -337,17 +335,17 @@ export const bookDelete = (id, index) => { // add book export const bookAdd = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { let formData = new FormData(); for (let key in data) { formData.append(key, data[key]); } - const item = await Httphelper.onPost('/api/book/', { data: formData }); - dispatch({ type: TypeMap.BOOK_MY_RELEASE, payload: item }, dispatch); + const item = await Httphelper.onPost('/api/book/', {data: formData}); + dispatch({type: TypeMap.BOOK_MY_RELEASE, payload: item}, dispatch); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -355,86 +353,86 @@ export const bookAdd = (data) => { // edit book export const bookEdit = (id, data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { let formData = new FormData(); for (let key in data) { formData.append(key, data[key]); } - const item = await Httphelper.onPut('/api/book/' + id, { data: formData }); - dispatch({ type: TypeMap.BOOK_MY_UPDATE, payload: item }, dispatch); + const item = await Httphelper.onPut('/api/book/' + id, {data: formData}); + dispatch({type: TypeMap.BOOK_MY_UPDATE, payload: item}, dispatch); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; // book search keyword export const bookSearchKeyword = (keyword, page = 1, size = 20) => { - return { type: TypeMap.BOOK_SEARCH_KEYWORD, payload: { keyword, page, size } }; + return {type: TypeMap.BOOK_SEARCH_KEYWORD, payload: {keyword, page, size}}; }; export const borrowAdd = (data) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPost('/api/borrow/', { data }, dispatch); - dispatch({ type: TypeMap.BORROW_ADD, payload: item }); + const item = await Httphelper.onPost('/api/borrow/', {data}, dispatch); + dispatch({type: TypeMap.BORROW_ADD, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowDelete = (id, index) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const item = await Httphelper.onDelete('/api/borrow/' + id, {}, dispatch); - dispatch({ type: TypeMap.BORROW_DELETE, payload: { id, index } }); + dispatch({type: TypeMap.BORROW_DELETE, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowDetail = (id) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const detail = await Httphelper.onGet('/api/borrow/' + id, {}, dispatch); - dispatch({ type: TypeMap.BORROW_DETAIL, payload: detail }); + dispatch({type: TypeMap.BORROW_DETAIL, payload: detail}); return detail; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowMyList = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onGet('/api/borrow/my', { params: {} }, dispatch); - dispatch({ type: TypeMap.BORROW_MY_LIST, payload: item }); + const item = await Httphelper.onGet('/api/borrow/my', {params: {}}, dispatch); + dispatch({type: TypeMap.BORROW_MY_LIST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; export const borrowRequest = () => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { const item = await Httphelper.onGet('/api/borrow/request', {}, dispatch); - dispatch({ type: TypeMap.BORROW_REQUEST, payload: item }); + dispatch({type: TypeMap.BORROW_REQUEST, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -442,13 +440,13 @@ export const borrowRequest = () => { export const borrowRequestMsg = (id, content) => { return async (dispatch) => { //BORROW_REPLY_TO - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/request/msg/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REQUEST_MSG, payload: item }); + const item = await Httphelper.onPut('/api/borrow/request/msg/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REQUEST_MSG, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; @@ -456,39 +454,52 @@ export const borrowRequestMsg = (id, content) => { export const borrowReplyMsg = (id, content) => { return async (dispatch) => { //BORROW_REPLY_TO - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/reply/msg/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REPLY_MSG, payload: item }); + const item = await Httphelper.onPut('/api/borrow/reply/msg/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REPLY_MSG, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; - +// refuse borrow book export const borrowRefuse = (id, content) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/refuse/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_REFUSE, payload: item }); + const item = await Httphelper.onPut('/api/borrow/refuse/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_REFUSE, payload: item}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; - +// confirm borrow book export const borrowConfirm = (id, content) => { return async (dispatch) => { - dispatch({ type: TypeMap.LOADING }); + dispatch({type: TypeMap.LOADING}); + try { + const item = await Httphelper.onPut('/api/borrow/confirm/' + id, {data: {content}}, dispatch); + dispatch({type: TypeMap.BORROW_CONFIRM, payload: item}); + return item; + } finally { + dispatch({type: TypeMap.LOADING_HIDE}); + } + }; +}; +// return borrow book +export const borrowReturn = (id, index) => { + return async (dispatch) => { + dispatch({type: TypeMap.LOADING}); try { - const item = await Httphelper.onPut('/api/borrow/confirm/' + id, { data: { content } }, dispatch); - dispatch({ type: TypeMap.BORROW_CONFIRM, payload: item }); + const item = await Httphelper.onPut('/api/borrow/return/' + id, {data: {}}, dispatch); + dispatch({type: TypeMap.BORROW_RETURN, payload: {id, index}}); return item; } finally { - dispatch({ type: TypeMap.LOADING_HIDE }); + dispatch({type: TypeMap.LOADING_HIDE}); } }; }; diff --git a/client/src/components/component.header.js b/client/src/components/component.header.js index 003d965..f776a16 100644 --- a/client/src/components/component.header.js +++ b/client/src/components/component.header.js @@ -94,7 +94,7 @@ class Header extends React.Component { return (
this.props.history.push('/my/home')}> - {username || email} + Welcome {username || email}
Logout diff --git a/client/src/components/component.module.scss b/client/src/components/component.module.scss index 71e4dc1..f1e32e2 100644 --- a/client/src/components/component.module.scss +++ b/client/src/components/component.module.scss @@ -80,6 +80,14 @@ div { .username { margin-bottom: 10px; margin-right: 10px; + cursor: pointer; + border: 1px solid pink; + padding-right: 10px; + padding-left: 10px; + > span { + font-weight: bold; + text-shadow: 5px 5px 5px pink; + } } .myNav { diff --git a/client/src/pages/page.book.detail.js b/client/src/pages/page.book.detail.js index 67d7faf..74d966c 100644 --- a/client/src/pages/page.book.detail.js +++ b/client/src/pages/page.book.detail.js @@ -30,8 +30,8 @@ class PageBookDetail extends React.Component { } render() { - const {image, book_name, price, ISBN, author, category, describe} = this.props.book.detail || {}; - console.log('this.props.book.detail:', this.props.book.detail); + const {image, book_name, price, ISBN, author, category, describe, is_borrow = false} = this.props.book.detail || {}; + return (
@@ -66,7 +66,9 @@ class PageBookDetail extends React.Component {
- +
diff --git a/client/src/pages/page.borrow.list.js b/client/src/pages/page.borrow.list.js index 5d2fd6e..87581d2 100644 --- a/client/src/pages/page.borrow.list.js +++ b/client/src/pages/page.borrow.list.js @@ -4,6 +4,7 @@ import * as allAction from '../actions'; import MyNav from '../components/component.mynav'; import Message from '../components/component.message'; import styles from './pages.module.scss'; +import {config} from 'rxjs'; class BorrowList extends React.Component { constructor(props) { @@ -34,7 +35,7 @@ class BorrowList extends React.Component { ); } return mylist.map((item, index) => { - const stateMap = {1: 'Normal', 2: 'Confirm', 3: 'Refuse'}; + const stateMap = {1: 'Normal', 2: 'Confirm', 3: 'Refuse', 4: 'Return'}; return ( {item.book_username} @@ -45,6 +46,15 @@ class BorrowList extends React.Component { diff --git a/client/src/reducers/reducer.borrow.js b/client/src/reducers/reducer.borrow.js index 075fb5c..ecea9e1 100644 --- a/client/src/reducers/reducer.borrow.js +++ b/client/src/reducers/reducer.borrow.js @@ -25,6 +25,14 @@ export default (state = {}, action) => { case TypeMap.BORROW_REFUSE: state.refuse = payload; break; + case TypeMap.BORROW_RETURN: + const {index, id} = payload; + // debugger + if (state.mylist[index]) { + state.mylist[index].state = 4; + } + state.return = payload; + break; default: } diff --git a/server/src/api/api.borrow.js b/server/src/api/api.borrow.js index 875558a..6e8f0ea 100644 --- a/server/src/api/api.borrow.js +++ b/server/src/api/api.borrow.js @@ -131,12 +131,19 @@ router const {id} = request.params; const {content} = request.body; const info = await BorrowService.Confirm(id, content); - // $push 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; diff --git a/server/src/model/model.borrow.js b/server/src/model/model.borrow.js index d347210..b22dff1 100644 --- a/server/src/model/model.borrow.js +++ b/server/src/model/model.borrow.js @@ -15,7 +15,7 @@ export default mongoose.model( request_username: {type: String, default: ''}, request_address: {type: String, default: ''}, message: {type: Array, default: []}, - state: {type: Number, default: 1, commit: '1:normal,2:confirm,3:refuse'}, + state: {type: Number, default: 1, commit: '1:normal,2:confirm,3:refuse,4: return'}, }, { timestamps: {createdAt: 'create_time', updatedAt: 'update_time'}, diff --git a/server/src/service/BorrowService.js b/server/src/service/BorrowService.js index e32741a..b904e69 100644 --- a/server/src/service/BorrowService.js +++ b/server/src/service/BorrowService.js @@ -1,4 +1,4 @@ -import { ModelBorrow } from '../model/model'; +import {ModelBorrow} from '../model/model'; import BaseService from './BaseService'; import UserService from './UserService'; import BookService from './BookService'; @@ -16,10 +16,10 @@ class BorrowService extends BaseService { * @memberof BorrowService */ async AddBorrow(data) { - const { userInfo, id: book_id, book_name, user_id: book_user_id, username: book_username, price: book_price, message } = data; - const { id: request_user_id, username: request_username, address: request_address } = userInfo || {}; + const {userInfo, id: book_id, book_name, user_id: book_user_id, username: book_username, price: book_price, message} = data; + const {id: request_user_id, username: request_username, address: request_address} = userInfo || {}; // judge is exists - const isExists = await this.findOne({ book_id, request_user_id }); + const isExists = await this.findOne({book_id, request_user_id, state: 1}); if (isExists) { this.failure('you have added'); } @@ -39,7 +39,7 @@ class BorrowService extends BaseService { request_user_id, request_address, request_username, - message: [{ request_msg: message, create_time: new Date(), reply_msg: '' }], + message: [{request_msg: message, create_time: new Date(), reply_msg: ''}], }); return this.success('add success'); } @@ -57,7 +57,7 @@ class BorrowService extends BaseService { this.failure('message content is not empty'); } console.log('id:', id, 'content:', request_msg); - await this.updateByCondition({ _id: id }, { $push: { message: { request_msg, create_time: new Date(), reply_msg: '' } } }); + await this.updateByCondition({_id: id}, {$push: {message: {request_msg, create_time: new Date(), reply_msg: ''}}}); return this.success('add success'); } @@ -73,7 +73,7 @@ class BorrowService extends BaseService { if (!reply_msg) { this.failure('reply message is not empty'); } - await this.updateByCondition({ _id: id }, { $push: { message: { request_msg: '', reply_msg, create_time: new Date() } } }); + await this.updateByCondition({_id: id}, {$push: {message: {request_msg: '', reply_msg, create_time: new Date()}}}); return this.success('add success'); } @@ -89,12 +89,12 @@ class BorrowService extends BaseService { if (!content) { this.failure('please fill in the reason for rejection'); } - await this.updateByCondition({ _id: id }, { state: 3, $push: { message: { request_msg: '', reply_msg: content, create_time: new Date() } } }); + await this.updateByCondition({_id: id}, {state: 3, $push: {message: {request_msg: '', reply_msg: content, create_time: new Date()}}}); return this.success('operator success'); } /** - * confirm requester + * confirm requester * * @param {*} id * @param {*} content @@ -108,10 +108,26 @@ class BorrowService extends BaseService { if (item.is_borrow) { this.failure('error, the book has been loaned'); } - await BookService.findByIdAndUpdate(item.id, { is_borrow: true }); - await this.updateByCondition({ _id: id }, { state: 2, $push: { message: { request_msg: '', reply_msg: content, create_time: new Date() } } }); + await BookService.findByIdAndUpdate(item.id, {is_borrow: true}); + await this.updateByCondition({_id: id}, {state: 2, $push: {message: {request_msg: '', reply_msg: content, create_time: new Date()}}}); return this.success('operator success'); } + + /** + * return book + * + * @param {*} id + * @memberof BorrowService + */ + async Return(id) { + const info = await this.findById(id); + if (!info) { + this.failure('error:book not found'); + } + await BookService.findByIdAndUpdate(info.book_id, {is_borrow: false}); + await this.updateByCondition({_id: id}, {state: 4, $push: {message: {request_msg: 'return books', reply_msg: '', create_time: new Date()}}}); + return this.success('return success'); + } } export default new BorrowService();