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/service/BorrowService.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
133 lines (122 sloc)
3.64 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 {ModelBorrow} from '../model/model'; | |
import BaseService from './BaseService'; | |
import UserService from './UserService'; | |
import BookService from './BookService'; | |
class BorrowService extends BaseService { | |
constructor() { | |
super(ModelBorrow); | |
} | |
/** | |
* add borrow | |
* | |
* @param {*} data | |
* @returns | |
* @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 || {}; | |
// judge is exists | |
const isExists = await this.findOne({book_id, request_user_id, state: 1}); | |
if (isExists) { | |
this.failure('you have added'); | |
} | |
if (book_user_id == request_user_id) { | |
this.failure('wrong, the current book is your own'); | |
} | |
const bookUser = await UserService.findById(book_user_id); | |
await this.create({ | |
book_id, | |
book_user_id, | |
book_username, | |
book_name, | |
book_price, | |
book_address: bookUser.address, | |
request_user_id, | |
request_address, | |
request_username, | |
message: [{request_msg: message, create_time: new Date(), reply_msg: ''}], | |
}); | |
return this.success('add success'); | |
} | |
/** | |
* requester msg | |
* | |
* @param {*} id | |
* @param {*} request_msg | |
* @returns | |
* @memberof BorrowService | |
*/ | |
async AddRequestMsg(id, request_msg) { | |
if (!request_msg) { | |
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: ''}}}); | |
return this.success('add success'); | |
} | |
/** | |
* reply msg | |
* | |
* @param {*} id | |
* @param {*} reply_msg | |
* @returns | |
* @memberof BorrowService | |
*/ | |
async AddReplyMsg(id, reply_msg) { | |
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()}}}); | |
return this.success('add success'); | |
} | |
/** | |
* refuse requester | |
* | |
* @param {*} id | |
* @param {*} content | |
* @returns | |
* @memberof BorrowService | |
*/ | |
async Refuse(id, content) { | |
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()}}}); | |
return this.success('operator success'); | |
} | |
/** | |
* confirm requester | |
* | |
* @param {*} id | |
* @param {*} content | |
* @returns | |
* @memberof BorrowService | |
*/ | |
async Confirm(id, content) { | |
const info = await this.findById(id); | |
const item = await BookService.findById(info.book_id); | |
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()}}}); | |
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(); |