Files
yourpart3/backend/controllers/falukantController.js

165 lines
5.7 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);
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
async createStock(req, res) {
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 });
}
}
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 });
}
}
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 });
}
}
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 });
}
}
}
export default FalukantController;