added category
This commit is contained in:
parent
76e7349ce5
commit
3388da7280
|
@ -34,6 +34,7 @@ CREATE TABLE IF NOT EXISTS Categories (
|
|||
CREATE TABLE IF NOT EXISTS Products (
|
||||
ID SERIAL PRIMARY KEY,
|
||||
group_id INT,
|
||||
local_id INT,
|
||||
abstract_product_id INT,
|
||||
amount INT,
|
||||
date_of_production DATE,
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
import CategoryService from "../services/category.js";
|
||||
import log
|
||||
from "../utils/log.js";
|
||||
const TAG = "controllers/category.js";
|
||||
|
||||
class CategoryController {
|
||||
async create(req, res) {
|
||||
try {
|
||||
const { localId, categoryName, groupId } = req.body;
|
||||
|
||||
await CategoryService.create(groupId, localId, categoryName);
|
||||
return res.status(200).send("Success");
|
||||
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/create: ${e}`)); }
|
||||
}
|
||||
|
||||
async update(req, res) {
|
||||
try {
|
||||
const { localId, groupId, name } = req.body;
|
||||
|
||||
await CategoryService.update(groupId, localId, name);
|
||||
return res.status(200).send("Success");
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/update: ${e}`)); }
|
||||
}
|
||||
};
|
||||
|
||||
export default new CategoryController();
|
|
@ -6,6 +6,7 @@ import log from './utils/log.js'
|
|||
|
||||
import config from '../config.json' with {type: "json"};
|
||||
import ProductRouter from './routers/product.js';
|
||||
import CategoryRouter from './routers/category.js';
|
||||
|
||||
const app = express();
|
||||
|
||||
|
@ -16,6 +17,7 @@ app.use('/api/user/', UserRouter);
|
|||
app.use('/api/group/', GroupRouter);
|
||||
app.use('/api/abstractproduct', AbstractProductRouter);
|
||||
app.use('/api/product', ProductRouter);
|
||||
app.use('/api/category', CategoryRouter);
|
||||
|
||||
app.listen(config.port, () => {
|
||||
log.info(`Application has started on port ${config.port}`)
|
||||
|
|
|
@ -2,6 +2,7 @@ import UserService from '../services/user.js';
|
|||
import GroupService from '../services/group.js';
|
||||
import AbstractProductService from '../services/abstractproduct.js';
|
||||
import ProductService from '../services/product.js';
|
||||
import CategoryService from '../services/category.js';
|
||||
import log from '../utils/log.js';
|
||||
import statuses from '../utils/status.js';
|
||||
|
||||
|
@ -79,6 +80,26 @@ const productExists = async (req, res, next) => {
|
|||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/productExists: ${e}`)) }
|
||||
}
|
||||
|
||||
const categoryNameDoesntExist = async (req, res, next) => {
|
||||
try {
|
||||
const { categoryName, localId, groupId } = req.body;
|
||||
|
||||
let result = await CategoryService.getByName(groupId, localId, categoryName);
|
||||
if (result != statuses.not_found) return res.status(400).send("Such category name exists");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/categoryNameDoesntExist: ${e}`)) }
|
||||
}
|
||||
|
||||
const categoryExists = async (req, res, next) => {
|
||||
try {
|
||||
const { localId, groupId } = req.body;
|
||||
|
||||
let result = await CategoryService.getById(groupId, localId);
|
||||
if (!result || result == statuses.not_found) return res.status(404).send("No such category");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/categoryExists: ${e}`)) }
|
||||
}
|
||||
|
||||
export default {
|
||||
usernameExists,
|
||||
usernameDoesntExist,
|
||||
|
@ -88,5 +109,8 @@ export default {
|
|||
groupNameDoesntExist,
|
||||
|
||||
abstractProductExists,
|
||||
productExists
|
||||
productExists,
|
||||
|
||||
categoryNameDoesntExist,
|
||||
categoryExists
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import { Router } from 'express';
|
||||
import auth from '../middlewares/auth.js';
|
||||
import existance from '../middlewares/existance.js';
|
||||
import CategoryController from '../controllers/category.js';
|
||||
|
||||
const CategoryRouter = new Router();
|
||||
|
||||
CategoryRouter.post('/create', auth.authenticate, existance.groupExists, existance.categoryNameDoesntExist, CategoryController.create);
|
||||
CategoryRouter.post('/update', auth.authenticate, existance.groupExists, existance.categoryExists, CategoryController.update)
|
||||
|
||||
export default CategoryRouter;
|
|
@ -0,0 +1,26 @@
|
|||
import db from '../db.js';
|
||||
import statuses from '../utils/status.js';
|
||||
|
||||
class CategoryService {
|
||||
async create(groupId, categoryId, name) {
|
||||
await db.query("INSERT INTO categories (group_id, local_id, name) VALUES ($1, $2, $3)", [groupId, categoryId, name])
|
||||
}
|
||||
|
||||
async update(groupId, categoryId, name) {
|
||||
await db.query("UPDATE categories SET name = $1 WHERE group_id = $2 AND local_id = $3", [name, groupId, categoryId]);
|
||||
}
|
||||
|
||||
async getById(groupId, localId) {
|
||||
let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]))
|
||||
if (result.rowCount == 0) return statuses.not_found;
|
||||
return result.rows[0]
|
||||
}
|
||||
|
||||
async getByName(groupId, localId, name) {
|
||||
let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2 AND name = $3", [groupId, localId, name]));
|
||||
if (result.rowCount == 0) return statuses.not_found;
|
||||
return result.rows[0];
|
||||
}
|
||||
}
|
||||
|
||||
export default new CategoryService();
|
Loading…
Reference in New Issue