Files
yourpart3/backend/controllers/falukantController.js
2025-01-28 09:55:36 +01:00

344 lines
12 KiB
JavaScript

import FalukantService from '../services/falukantService.js';
class FalukantController {
constructor() {
this.getUser = this.getUser.bind(this);
this.createUser = this.createUser.bind(this);
this.randomFirstName = this.randomFirstName.bind(this);
this.randomLastName = this.randomLastName.bind(this);
this.getInfo = this.getInfo.bind(this);
this.getInventory = this.getInventory.bind(this);
this.sellProduct = this.sellProduct.bind(this);
this.sellAllProducts = this.sellAllProducts.bind(this);
this.moneyHistory = this.moneyHistory.bind(this);
this.getStorage = this.getStorage.bind(this);
this.buyStorage = this.buyStorage.bind(this);
this.sellStorage = this.sellStorage.bind(this);
this.getStockTypes = this.getStockTypes.bind(this);
this.getStockOverview = this.getStockOverview.bind(this);
this.getAllProductions = this.getAllProductions.bind(this);
this.getDirectorProposals = this.getDirectorProposals.bind(this);
this.convertProposalToDirector = this.convertProposalToDirector.bind(this);
this.getDirectorForBranch = this.getDirectorForBranch.bind(this);
this.setSetting = this.setSetting.bind(this);
this.getMarriageProposals = this.getMarriageProposals.bind(this);
}
async getUser(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getUser(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async createUser(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { gender, firstname: firstName, lastname: lastName } = req.body;
console.log(req.body);
const result = await FalukantService.createUser(hashedUserId, gender, firstName, lastName);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async randomFirstName(req, res) {
try {
const { gender } = req.params;
const result = await FalukantService.randomFirstName(gender);
res.status(200).json({ name: result });
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async randomLastName(req, res) {
try {
const result = await FalukantService.randomLastName();
res.status(200).json({ name: result });
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getInfo(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getInfo(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getBranches(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getBranches(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getBranch(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branch: branchId } = req.params;
console.log(branchId, req.params);
const result = await FalukantService.getBranch(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async createProduction(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId, productId, quantity } = req.body;
const result = await FalukantService.createProduction(hashedUserId, branchId, productId, quantity);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getProduction(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.params;
const result = await FalukantService.getProduction(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getStock(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.params || null;
const result = await FalukantService.getStock(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async createStock(req, res) {
console.log('build stock');
try {
const { userid: hashedUserId } = req.headers;
const { branchId, stockTypeId, stockSize } = req.body;
const result = await FalukantService.createStock(hashedUserId, branchId, stockTypeId, stockSize);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getProducts(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getProducts(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getInventory(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.params;
const result = await FalukantService.getInventory(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async sellProduct(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId, productId, quality, quantity } = req.body;
const result = await FalukantService.sellProduct(hashedUserId, branchId, productId, quality, quantity);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async sellAllProducts(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.body;
const result = await FalukantService.sellAllProducts(hashedUserId, branchId);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async moneyHistory(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { page, filter } = req.body;
if (!page) {
page = 1;
}
const result = await FalukantService.moneyHistory(hashedUserId, page, filter);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getStorage(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.params;
const result = await FalukantService.getStorage(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message});
console.log(error);
}
}
async buyStorage(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId, amount, stockTypeId } = req.body;
const result = await FalukantService.buyStorage(hashedUserId, branchId, amount, stockTypeId);
res.status(201).json(result);
} catch (error) {
res.status(500).json({ error: error.message});
console.log(error);
}
}
async sellStorage(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId, amount, stockTypeId } = req.body;
const result = await FalukantService.sellStorage(hashedUserId, branchId, amount, stockTypeId);
res.status(202).json(result);
} catch (error) {
res.status(500).json({ error: error.message});
console.log(error);
}
}
async getStockTypes(req, res) {
console.log('load stock');
try {
const result = await FalukantService.getStockTypes();
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getStockOverview(req, res) {
try {
const result = await FalukantService.getStockOverview();
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async getAllProductions(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getAllProductions(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async getDirectorProposals(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.body;
const result = await FalukantService.getDirectorProposals(hashedUserId, branchId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async convertProposalToDirector(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { proposalId } = req.body;
const result = await FalukantService.convertProposalToDirector(hashedUserId, proposalId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async getDirectorForBranch(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId } = req.params;
const result = await FalukantService.getDirectorForBranch(hashedUserId, branchId);
if (!result) {
return res.status(404).json({ message: 'No director found for this branch' });
}
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
async setSetting(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const { branchId, directorId, settingKey, value } = req.body;
const result = await FalukantService.setSetting(hashedUserId, branchId, directorId, settingKey, value);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
console.log(error);
}
}
async getMarriageProposals(req, res) {
try {
const { userid: hashedUserId } = req.headers;
const result = await FalukantService.getMarriageProposals(hashedUserId);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}
export default FalukantController;