Skip to content
Permalink
Browse files
return books
  • Loading branch information
shaoh4 committed Dec 23, 2020
1 parent 5a6be2c commit de9fcf13f22d2069a9618045ef25dac62c399574
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 125 deletions.

Large diffs are not rendered by default.

@@ -94,7 +94,7 @@ class Header extends React.Component {
return (
<div className={styles.col0 + ' ' + styles.row}>
<div className={styles.col0 + ' ' + styles.username} onClick={() => this.props.history.push('/my/home')}>
{username || email}
Welcome <span> {username || email}</span>
</div>
<div className={styles.col0 + ' ' + styles.categoryName} onClick={this.logout.bind(this)} >
Logout
@@ -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 {
@@ -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 (
<div className={styles.row + ' ' + styles.detail}>
<div className={styles.left}>
@@ -66,7 +66,9 @@ class PageBookDetail extends React.Component {
<div className={styles.row}>
<div className={styles.detailLabel}> </div>
<div className={styles.describe}>
<button onClick={() => this.setShowConfirm(true)}>Borrow</button>
<button disabled={is_borrow} onClick={() => this.setShowConfirm(true)}>
Borrow
</button>
</div>
</div>
</div>
@@ -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 (
<tr key={item.id}>
<td>{item.book_username}</td>
@@ -45,6 +46,15 @@ class BorrowList extends React.Component {
<td>
<button
onClick={async () => {
if (item.state === 2) {
const isReturn = confirm('do you really want to return it');
if (!isReturn) {
return;
}
await this.props.borrowReturn(item.id, index);
this.update();
return;
}
const isDelete = confirm('do you really want to delete it?');
if (!isDelete) {
return;
@@ -53,7 +63,7 @@ class BorrowList extends React.Component {
this.update();
}}
>
Delete
{item.state === 2 ? 'Return' : 'Delete'}
</button>
<button onClick={() => this.setState({currentItem: item})}>Message</button>
</td>
@@ -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:
}

@@ -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;
@@ -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'},
@@ -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();

0 comments on commit de9fcf1

Please sign in to comment.