198 lines
4.0 KiB
JavaScript
198 lines
4.0 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { auth } = require('../middleware/auth');
|
|
const Equity = require('../models/Equity');
|
|
|
|
router.get('/', auth, async (req, res, next) => {
|
|
try {
|
|
const {
|
|
page = 1,
|
|
limit = 10,
|
|
status,
|
|
platform,
|
|
type,
|
|
sortBy = 'createdAt',
|
|
order = 'desc'
|
|
} = req.query;
|
|
|
|
const query = { owner: req.user._id };
|
|
|
|
if (status) query.status = status;
|
|
if (platform) query.platform = platform;
|
|
if (type) query.type = type;
|
|
|
|
const sortOrder = order === 'asc' ? 1 : -1;
|
|
const skip = (parseInt(page) - 1) * parseInt(limit);
|
|
|
|
const [equities, total] = await Promise.all([
|
|
Equity.find(query)
|
|
.sort({ [sortBy]: sortOrder })
|
|
.skip(skip)
|
|
.limit(parseInt(limit))
|
|
.lean(),
|
|
Equity.countDocuments(query)
|
|
]);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
list: equities,
|
|
pagination: {
|
|
page: parseInt(page),
|
|
limit: parseInt(limit),
|
|
total,
|
|
pages: Math.ceil(total / parseInt(limit))
|
|
}
|
|
}
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.get('/:id', auth, async (req, res, next) => {
|
|
try {
|
|
const equity = await Equity.findOne({
|
|
_id: req.params.id,
|
|
owner: req.user._id
|
|
});
|
|
|
|
if (!equity) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: '权益不存在'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: equity
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.post('/', auth, async (req, res, next) => {
|
|
try {
|
|
const {
|
|
title,
|
|
description,
|
|
type,
|
|
platform,
|
|
value,
|
|
unit,
|
|
validStart,
|
|
validEnd,
|
|
images,
|
|
tags,
|
|
isTransferable,
|
|
transferPrice,
|
|
metadata
|
|
} = req.body;
|
|
|
|
if (!title || !type || !platform || !validStart || !validEnd) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: '缺少必填字段'
|
|
});
|
|
}
|
|
|
|
const equity = await Equity.create({
|
|
title,
|
|
description,
|
|
type,
|
|
platform,
|
|
value,
|
|
unit,
|
|
validStart: new Date(validStart),
|
|
validEnd: new Date(validEnd),
|
|
owner: req.user._id,
|
|
images,
|
|
tags,
|
|
isTransferable,
|
|
transferPrice,
|
|
metadata
|
|
});
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: equity
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.put('/:id', auth, async (req, res, next) => {
|
|
try {
|
|
const equity = await Equity.findOneAndUpdate(
|
|
{ _id: req.params.id, owner: req.user._id },
|
|
{ ...req.body, updatedAt: new Date() },
|
|
{ new: true, runValidators: true }
|
|
);
|
|
|
|
if (!equity) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: '权益不存在或无权限修改'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: equity
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.delete('/:id', auth, async (req, res, next) => {
|
|
try {
|
|
const equity = await Equity.findOneAndDelete({
|
|
_id: req.params.id,
|
|
owner: req.user._id
|
|
});
|
|
|
|
if (!equity) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: '权益不存在或无权限删除'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '权益已删除'
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.get('/platforms/summary', auth, async (req, res, next) => {
|
|
try {
|
|
const summary = await Equity.aggregate([
|
|
{ $match: { owner: req.user._id } },
|
|
{
|
|
$group: {
|
|
_id: '$platform',
|
|
count: { $sum: 1 },
|
|
totalValue: { $sum: '$value' }
|
|
}
|
|
},
|
|
{ $sort: { count: -1 } }
|
|
]);
|
|
|
|
res.json({
|
|
success: true,
|
|
data: summary
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|