node-server/src/handlers/updatepoint.ts

68 lines
2.0 KiB
TypeScript

import prisma from "../db";
import { Request, Response } from "express";
export const getUpdatePoints = async (req:Request, res:Response) => {
const update = await prisma.update.findUnique({
where: {id: req.body.updateId},
include: {updatePoints: true}
});
res.json({data: update.updatePoints});
}
export const getUpdatePointById = async (req:Request, res:Response) => {
const updatePoint = await prisma.updatePoint.findUnique({
where: {id: req.params.id}
});
res.json({data: updatePoint});
}
export const createUpdatePoint = async (req:Request, res:Response) => {
const update = await prisma.update.findUnique({
where: {id: req.body.updateId},
include: {product: true}
});
if (!update || update.product.belongsToId !== req.user.id) {
return res.status(401).json({message: "Unauthorized"});
}
const updatePoint = await prisma.updatePoint.create({
data: req.body
});
res.json({data: updatePoint});
}
export const updateUpdatePoint = async (req:Request, res:Response) => {
const updatePoint = await prisma.updatePoint.findUnique({
where: {id: req.params.id},
include: {update: {include: {product: true}}}
});
if (!updatePoint || updatePoint.update.product.belongsToId !== req.user.id) {
return res.status(401).json({message: "Unauthorized"});
}
const updatedUpdatePoint = await prisma.updatePoint.update({
where: {id: req.params.id},
data: req.body
});
res.json({data: updatedUpdatePoint});
}
export const deleteUpdatePoint = async (req:Request, res:Response) => {
const updatePoint = await prisma.updatePoint.findUnique({
where: {id: req.params.id},
include: {update: {include: {product: true}}}
});
if (!updatePoint || updatePoint.update.product.belongsToId !== req.user.id) {
return res.status(401).json({message: "Unauthorized"});
}
const deleted = await prisma.updatePoint.delete({
where: {id: req.params.id}
});
res.json({data: deleted});
}