feat: initialize multi-platform Family Organizer application with Flutter frontend and Node.js backend services
This commit is contained in:
113
backend/dist/controller/ShoppingItemController.js
vendored
Normal file
113
backend/dist/controller/ShoppingItemController.js
vendored
Normal file
@@ -0,0 +1,113 @@
|
||||
"use strict";
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var _a;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ShoppingItemController = void 0;
|
||||
const data_source_1 = require("../data-source");
|
||||
const ShoppingList_1 = require("../entity/ShoppingList");
|
||||
const ShoppingItem_1 = require("../entity/ShoppingItem");
|
||||
const User_1 = require("../entity/User");
|
||||
class ShoppingItemController {
|
||||
}
|
||||
exports.ShoppingItemController = ShoppingItemController;
|
||||
_a = ShoppingItemController;
|
||||
ShoppingItemController.newItem = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const listId = parseInt(req.params.listId);
|
||||
const { name, quantity } = req.body;
|
||||
const userId = res.locals.jwtPayload.userId;
|
||||
if (!name)
|
||||
return res.status(400).send("Name is required");
|
||||
const listRepository = data_source_1.AppDataSource.getRepository(ShoppingList_1.ShoppingList);
|
||||
let list;
|
||||
try {
|
||||
list = yield listRepository.findOne({ where: { id: listId }, relations: ["owner"] });
|
||||
}
|
||||
catch (e) {
|
||||
return res.status(500).send("Error finding list");
|
||||
}
|
||||
if (!list)
|
||||
return res.status(404).send("List not found");
|
||||
// Optional: Check if user has access to this list (for now list owner only)
|
||||
if (list.owner.id !== userId)
|
||||
return res.status(403).send("No access to this list");
|
||||
const item = new ShoppingItem_1.ShoppingItem();
|
||||
item.name = name;
|
||||
item.quantity = quantity || 1;
|
||||
item.list = list;
|
||||
// createdBy
|
||||
const userRepository = data_source_1.AppDataSource.getRepository(User_1.User);
|
||||
const user = yield userRepository.findOneBy({ id: userId });
|
||||
if (user)
|
||||
item.createdBy = user;
|
||||
const itemRepository = data_source_1.AppDataSource.getRepository(ShoppingItem_1.ShoppingItem);
|
||||
try {
|
||||
yield itemRepository.save(item);
|
||||
res.status(201).send(item);
|
||||
}
|
||||
catch (e) {
|
||||
res.status(500).send("Error creating item");
|
||||
}
|
||||
});
|
||||
ShoppingItemController.editItem = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const id = parseInt(req.params.id);
|
||||
const { name, quantity, isChecked } = req.body;
|
||||
const itemRepository = data_source_1.AppDataSource.getRepository(ShoppingItem_1.ShoppingItem);
|
||||
let item;
|
||||
try {
|
||||
item = yield itemRepository.findOne({ where: { id }, relations: ["list", "list.owner"] });
|
||||
}
|
||||
catch (e) {
|
||||
return res.status(500).send("Error finding item");
|
||||
}
|
||||
if (!item)
|
||||
return res.status(404).send("Item not found");
|
||||
// Check access
|
||||
const userId = res.locals.jwtPayload.userId;
|
||||
if (item.list.owner.id !== userId)
|
||||
return res.status(403).send("No access");
|
||||
if (name !== undefined)
|
||||
item.name = name;
|
||||
if (quantity !== undefined)
|
||||
item.quantity = quantity;
|
||||
if (isChecked !== undefined)
|
||||
item.isChecked = isChecked;
|
||||
try {
|
||||
yield itemRepository.save(item);
|
||||
res.send(item);
|
||||
}
|
||||
catch (e) {
|
||||
res.status(500).send("Error updating item");
|
||||
}
|
||||
});
|
||||
ShoppingItemController.deleteItem = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const id = parseInt(req.params.id);
|
||||
const itemRepository = data_source_1.AppDataSource.getRepository(ShoppingItem_1.ShoppingItem);
|
||||
let item;
|
||||
try {
|
||||
item = yield itemRepository.findOne({ where: { id }, relations: ["list", "list.owner"] });
|
||||
}
|
||||
catch (e) {
|
||||
return res.status(500).send("Error finding item");
|
||||
}
|
||||
if (!item)
|
||||
return res.status(404).send("Item not found");
|
||||
// Check access
|
||||
const userId = res.locals.jwtPayload.userId;
|
||||
if (item.list.owner.id !== userId)
|
||||
return res.status(403).send("No access");
|
||||
try {
|
||||
yield itemRepository.remove(item);
|
||||
res.status(204).send();
|
||||
}
|
||||
catch (e) {
|
||||
res.status(500).send("Error deleting item");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user