74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
import BaseController from '../../common/base.controller'
|
|
import { permission, router } from '../../decorators/router'
|
|
import { ZError } from '../../common/ZError'
|
|
import { Coupon } from '../../models/shop/Coupon'
|
|
|
|
class CouponController extends BaseController{
|
|
@permission('coupon:read')
|
|
@router('post /coupons')
|
|
async list(req, res) {
|
|
let { start, limit } = req.params
|
|
start = +start || 0
|
|
limit = +limit || 10
|
|
let { opt, sort } = Coupon.parseQueryParam(req.params)
|
|
let articles = await Coupon.find(opt)
|
|
.sort(sort)
|
|
.skip(start)
|
|
.limit(limit)
|
|
let count = await Coupon.count(opt)
|
|
let records = []
|
|
for (let record of articles) {
|
|
records.push(record.toJson())
|
|
}
|
|
return {
|
|
total: count,
|
|
start,
|
|
limit,
|
|
records
|
|
}
|
|
}
|
|
|
|
@permission('coupon:read')
|
|
@router('get /coupon/:id')
|
|
async detail(req, res) {
|
|
let { id } = req.params
|
|
const record = await Coupon.findById(id)
|
|
if (!record) {
|
|
throw new ZError(11, 'record not found')
|
|
}
|
|
return record.toJson()
|
|
}
|
|
|
|
@permission('coupon:read')
|
|
@router('post /coupon/save')
|
|
async save(req: any) {
|
|
let { _id } = req.params
|
|
let user = req.user
|
|
let record
|
|
if (!_id) {
|
|
record = new Coupon(req.params)
|
|
record.createdBy = user.id
|
|
} else {
|
|
record = await Coupon.findById(_id)
|
|
record.updateFromReq(req.params)
|
|
}
|
|
await record.save()
|
|
return {}
|
|
}
|
|
@permission('coupon:read')
|
|
@router('post /coupon/:id/delete')
|
|
async delete(req: any) {
|
|
let { id } = req.params
|
|
if (!id) {
|
|
throw new ZError(11, 'params mismatch')
|
|
}
|
|
await Coupon.findByIdAndUpdate(id, {
|
|
$set: {
|
|
deleted: true,
|
|
deleteTime: new Date()
|
|
}
|
|
})
|
|
return {}
|
|
}
|
|
}
|