Added the Sheepstar API to the Archive
This commit is contained in:
81
SheepstarAPIV1/routes/shop.js
Normal file
81
SheepstarAPIV1/routes/shop.js
Normal file
@ -0,0 +1,81 @@
|
||||
const app = require('express').Router();
|
||||
const checkPermission = require("../middlewares/checkPermission");
|
||||
const shopValidation = require("../validation/shopValidation");
|
||||
const Article = require("../models/Article");
|
||||
const ActivatedArticle = require("../models/ActivatedArticle");
|
||||
const {owns} = require("../validation/shopValidation");
|
||||
|
||||
app.get("/", checkPermission("shop.info"), async (req, res) => {
|
||||
// Validate request
|
||||
const {error} = shopValidation.info.validate(req.query);
|
||||
if (error) return res.status(400).json({message: error.details[0].message});
|
||||
|
||||
// Get article info
|
||||
const info = await ActivatedArticle.findAndCountAll({where: req.query});
|
||||
|
||||
// Return info
|
||||
if (info) {
|
||||
res.status(200).json(info.rows);
|
||||
} else res.json(404).json({message: "Article not found"});
|
||||
});
|
||||
|
||||
app.get("/owns", checkPermission("shop.info"), async (req, res) => {
|
||||
// Validate request
|
||||
const {error} = shopValidation.owns.validate(req.query);
|
||||
if (error) return res.status(400).json({message: error.details[0].message});
|
||||
|
||||
if (await Article.findByPk(req.query.articleID) === null) return res.status(404).json({message: "Article not found"});
|
||||
|
||||
// Get the article information
|
||||
const info = await ActivatedArticle.findOne({
|
||||
where: {
|
||||
articleID: req.query.articleID,
|
||||
guildID: req.query.guildID
|
||||
}
|
||||
});
|
||||
|
||||
const premiumInfo = await ActivatedArticle.findOne({
|
||||
where: {
|
||||
articleID: 1,
|
||||
guildID: req.query.guildID
|
||||
}
|
||||
});
|
||||
|
||||
res.status(200).json(info !== null || premiumInfo !== null);
|
||||
});
|
||||
|
||||
app.delete("/", checkPermission("shop.delete"), async (req, res) => {
|
||||
// Validate request
|
||||
const {error} = shopValidation.owns.validate(req.body);
|
||||
if (error) return res.status(400).json({message: error.details[0].message});
|
||||
|
||||
// Check if already exists
|
||||
const {count: articleCount} = await ActivatedArticle.findAndCountAll({where: req.body});
|
||||
if (articleCount === 0) return res.status(400).json({message: "Article does not exist"});
|
||||
|
||||
// Delete the article
|
||||
await ActivatedArticle.destroy({where: req.body});
|
||||
res.status(200).json({message: "Article deleted successfully"});
|
||||
});
|
||||
|
||||
app.put("/", checkPermission("shop.buy"), async (req, res) => {
|
||||
// Validate request
|
||||
const {error} = shopValidation.buy.validate(req.body);
|
||||
if (error) return res.status(400).json({message: error.details[0].message});
|
||||
|
||||
// Check if article exists
|
||||
|
||||
const article = await Article.findByPk(req.body.articleID);
|
||||
|
||||
if (!article) return res.status(404).json({message: "Article does not exist"});
|
||||
|
||||
if (!req.body.expiry_date) req.body.expiry_date = new Date("4000-01-01");
|
||||
|
||||
// Check if article limit reached
|
||||
const {count} = await ActivatedArticle.findAndCountAll({where: req.body});
|
||||
if (count >= article.maxOwnCount) return res.status(400).json({message: "Maximum limit of this item reached"});
|
||||
|
||||
res.status(200).json(await ActivatedArticle.create(req.body));
|
||||
});
|
||||
|
||||
module.exports = app;
|
Reference in New Issue
Block a user