470 lines
16 KiB
JavaScript
470 lines
16 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.getFamily = this.getFamily.bind(this);
|
|
this.acceptMarriageProposal = this.acceptMarriageProposal.bind(this);
|
|
this.getGifts = this.getGifts.bind(this);
|
|
this.sendGift = this.sendGift.bind(this);
|
|
this.getHouseTypes = this.getHouseTypes.bind(this);
|
|
this.getTitelsOfNobility = this.getTitelsOfNobility.bind(this);
|
|
this.getMoodAffect = this.getMoodAffect.bind(this);
|
|
this.getCharacterAffect = this.getCharacterAffect.bind(this);
|
|
this.getUserHouse = this.getUserHouse.bind(this);
|
|
this.getBuyableHouses = this.getBuyableHouses.bind(this);
|
|
this.buyUserHouse = this.buyUserHouse.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);
|
|
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 getFamily(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getFamily(hashedUserId);
|
|
if (!result) {
|
|
res.status(404).json({ error: 'No family data found' });
|
|
}
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async acceptMarriageProposal(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const { proposalId } = req.body;
|
|
const result = await FalukantService.acceptMarriageProposal(hashedUserId, proposalId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getGifts(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getGifts(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async sendGift(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const { giftId } = req.body;
|
|
const result = await FalukantService.sendGift(hashedUserId, giftId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
const status = error.status === 412 ? 412 : 500;
|
|
res.status(status).json({ error: error.message });
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
async getTitelsOfNobility(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getTitlesOfNobility(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getHouseTypes(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getHouseTypes(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getMoodAffect(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getMoodAffect(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getCharacterAffect(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getCharacterAffect(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getUserHouse(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getUserHouse(hashedUserId);
|
|
console.log(result);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getBuyableHouses(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const result = await FalukantService.getBuyableHouses(hashedUserId);
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async buyUserHouse(req, res) {
|
|
try {
|
|
const { userid: hashedUserId } = req.headers;
|
|
const { houseId } = req.body;
|
|
const result = await FalukantService.buyUserHouse(hashedUserId, houseId);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
console.log(error);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default FalukantController;
|