Rewriting database and API to transform to a ticket shop

This commit is contained in:
2024-09-26 11:04:27 +02:00
parent 080610cd7e
commit 787c5a61e5
88 changed files with 1040 additions and 751 deletions

View File

@@ -0,0 +1,16 @@
import { Member } from "../models/member.model";
import { Band } from "../models/band.model";
import { Request, Response, Router } from "express";
import { Rating } from "../models/rating.model";
import { Genre } from "../models/genre.model";
export const band = Router()
band.get("/", (req: Request, res: Response) => {
Band.findAll({
include: [ Member, Rating, Genre ]
})
.then(bands => {
res.status(200).json(bands)
})
})

View File

@@ -1,12 +0,0 @@
import { Brand } from "../models/brand.model"
import { Request, Router, Response } from "express"
export const brand = Router()
// Get all brands
brand.get("/", (req: Request, res: Response) => {
Brand.findAll()
.then(brands => {
res.status(200).json(brands)
})
})

View File

@@ -1,42 +0,0 @@
import { Router, Request, Response, NextFunction } from "express";
import { Category } from "../models/category.model";
export const category = Router()
// Get all categories
category.get("/", (req: Request, res: Response, next: NextFunction) => {
Category.findAll()
.then(categories => {
res.status(200).json(categories)
})
})
// Add new category
category.post("/", (req: Request, res: Response, next: NextFunction) => {
Category.create(req.body)
.then(category => {
res.status(201).json(category)
})
.catch(error => {
res.status(400).json({
code: 400,
message: error
})
})
})
// Delete category
category.delete("/:id", (req: Request, res: Response, next: NextFunction) => {
Category.destroy({
where: { id: req.params.id }
})
.then(category => {
res.status(200).json(category)
})
.catch(error => {
res.status(400).json({
code: 400,
message: error
})
})
})

View File

@@ -0,0 +1,11 @@
import { Genre } from "../models/genre.model";
import { Request, Response, Router } from "express";
export const genre = Router()
genre.get("/", (req: Request, res: Response) => {
Genre.findAll()
.then(genres => {
res.status(200).json(genres)
})
})

View File

@@ -0,0 +1,11 @@
import { Location } from "../models/location.model";
import { Request, Response, Router } from "express";
export const location = Router()
location.get("/", (req: Request, res: Response) => {
Location.findAll()
.then(locations => {
res.status(200).json(locations)
})
})

View File

@@ -1,11 +1,11 @@
import { Router, Request, Response } from "express";
import { Order } from "../models/order.model";
import { Product } from "../models/product.model";
import { Show } from "../models/show.model";
import { OrderItem } from "../models/orderItem.model";
import { Brand } from "../models/brand.model";
import { Category } from "../models/category.model";
import { Payment } from "../models/payment.model";
import { Address } from "../models/address.model";
import { Band } from "../models/band.model";
import { Location } from "../models/location.model";
export const order = Router()
@@ -18,8 +18,8 @@ order.get("/:id", (req: Request, res: Response) => {
model: OrderItem,
include: [
{
model: Product,
include: [ Brand, Category ],
model: Show,
include: [ Band, Location ],
attributes: {
exclude: [
"categoryId",
@@ -50,7 +50,7 @@ order.post("/", (req: Request, res: Response) => {
productId: orderItem.productId
})
Product.decrement(
Show.decrement(
"inStock",
{
by: orderItem.quantity,

View File

@@ -1,71 +0,0 @@
import { Router, Request, Response, NextFunction } from "express";
import { Product } from "../models/product.model";
import { Category } from "../models/category.model";
import { Brand } from "../models/brand.model";
export const product = Router()
// Get all products
product.get("/", (req: Request, res: Response) => {
Product.findAll({
include: [ Category, Brand ],
attributes: {
exclude: [
"categoryId",
"brandId"
]
}
})
.then(products => {
res.status(200).json(products)
})
})
// Get a product by id
product.get("/:productId", (req: Request, res: Response) => {
Product.findByPk(
req.params.productId,
{
include: [ Category, Brand ],
attributes: {
exclude: [
"categoryId",
"brandId"
]
}
}
)
.then(product => {
res.status(200).json(product)
})
})
// Add a new product
product.post("/", (req: Request, res: Response) => {
Product.create(req.body)
.then(product => {
res.status(201).json(product)
})
.catch(error => {
res.status(400).json({
code: 400,
message: error
})
})
})
// Remove a product
product.delete("/:id", (req: Request, res: Response) => {
Product.destroy({
where: { id: req.params.id }
})
.then(product => {
res.status(200).json(product)
})
.catch(error => {
res.status(400).json({
code: 400,
message: error
})
})
})

View File

@@ -0,0 +1,11 @@
import { Show } from "../models/show.model";
import { Request, Response, Router } from "express";
export const show = Router()
show.get("/", (req: Request, res: Response) => {
Show.findAll()
.then(shows => {
res.status(200).json(shows)
})
})