Fix db relations, add sample data, show categories in frontend menu

This commit is contained in:
2024-09-05 16:19:23 +02:00
parent cfd1d29302
commit 8b1a396f68
23 changed files with 502 additions and 129 deletions

View File

@@ -1,11 +1,11 @@
import { Sequelize } from "sequelize-typescript"
// Models
import { Categories } from "./models/categories.model"
import { Category } from "./models/category.model"
import { OrderedItem } from "./models/orderedItem.model"
import { Orders } from "./models/orders.model"
import { Products } from "./models/products.model"
import { Users } from "./models/users.model"
import { Order } from "./models/order.model"
import { Product } from "./models/product.model"
import { Account } from "./models/account.model"
const dbName = "database"
const dbUser = "root"
@@ -18,12 +18,12 @@ export const sequelize = new Sequelize({
username: dbUser,
password: dbPassword,
storage: "database.sqlite",
models: [ Categories, OrderedItem, Orders, Products, Users ]
models: [ Category, Product, Account, Order, OrderedItem ]
})
export function startDatabase() {
// Create database and tables
sequelize.sync({ force: false })
sequelize.sync({ force: true })
.then(() => {
console.log(`Database & tables created!`)
})

View File

@@ -1,8 +1,8 @@
import { Table, Column, Model, HasMany } from 'sequelize-typescript';
import { Orders } from './orders.model';
import { Order } from './order.model';
@Table
export class Users extends Model {
export class Account extends Model {
@Column
username: string
@@ -19,6 +19,6 @@ export class Users extends Model {
lastName: string
// Relations
@HasMany(() => Orders)
orders: Orders[]
@HasMany(() => Order)
orders: Order[]
}

View File

@@ -1,14 +0,0 @@
import { Table, Column, Model, BelongsTo, ForeignKey } from 'sequelize-typescript';
import { Products } from './products.model';
@Table
export class Categories extends Model {
@Column
name: string
@ForeignKey(() => Products)
productId: number
@BelongsTo(() => Products)
product: Products
}

View File

@@ -0,0 +1,14 @@
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany } from 'sequelize-typescript';
import { Product } from './product.model';
@Table
export class Category extends Model {
@Column
name: string
@Column
icon: string
@HasMany(() => Product)
product: Product
}

View File

@@ -0,0 +1,21 @@
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany, BelongsToMany } from 'sequelize-typescript';
import { Account } from './account.model';
import { OrderedItem } from './orderedItem.model';
import { Product } from './product.model';
@Table
export class Order extends Model {
@Column
@ForeignKey(() => Account)
accountId: number
@Column
totalPrice: number
// Relations
@BelongsTo(() => Account)
user: Account
@BelongsToMany(() => Product, () => OrderedItem)
orderedItems: OrderedItem
}

View File

@@ -1,15 +1,15 @@
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany } from 'sequelize-typescript';
import { Orders } from './orders.model';
import { Products } from './products.model';
import { Order } from './order.model';
import { Product } from './product.model';
@Table
export class OrderedItem extends Model {
@Column
@ForeignKey(() => Orders)
@ForeignKey(() => Order)
orderId: number
@Column
@ForeignKey(() => Products)
@ForeignKey(() => Product)
productId: number
@Column
@@ -17,11 +17,4 @@ export class OrderedItem extends Model {
@Column
totalPrice: number
// Relations
@BelongsTo(() => Orders)
order: Orders
@HasMany(() => Products)
products: Products[]
}

View File

@@ -1,20 +0,0 @@
import { Table, Column, Model, BelongsTo, ForeignKey, HasMany } from 'sequelize-typescript';
import { Users } from './users.model';
import { OrderedItem } from './orderedItem.model';
@Table
export class Orders extends Model {
@Column
@ForeignKey(() => Users)
userNameId: number
@Column
totalPrice: number
// Relations
@BelongsTo(() => Users)
user: Users
@HasMany(() => OrderedItem)
orderedItems: OrderedItem
}

View File

@@ -0,0 +1,30 @@
import { Table, Column, Model, ForeignKey, BelongsTo, BelongsToMany } from 'sequelize-typescript';
import { Category } from './category.model';
import { OrderedItem } from './orderedItem.model';
import { Order } from './order.model';
@Table
export class Product extends Model {
@Column
brand: string
@Column
name: string
@Column
@ForeignKey(() => Category)
categoryId: number
@Column
price: number
@Column
discount: number
// Relations
@BelongsTo(() => Category)
category: Category
@BelongsToMany(() => Order,() => OrderedItem)
orderedItem: OrderedItem
}

View File

@@ -1,30 +0,0 @@
import { Table, Column, Model, ForeignKey, BelongsTo, HasOne } from 'sequelize-typescript';
import { Categories } from './categories.model';
import { OrderedItem } from './orderedItem.model';
@Table
export class Products extends Model {
@Column
name: string
@Column
@ForeignKey(() => Categories)
categoryId: number
@Column
@ForeignKey(() => OrderedItem)
orderedItemId: number
@Column
price: number
@Column
discount: number
// Relations
@BelongsTo(() => OrderedItem)
orderedItem: OrderedItem
@HasOne(() => Categories)
category: Categories
}

View File

@@ -0,0 +1,12 @@
import { Router, Request, Response, NextFunction } from "express";
import { Account } from "../models/account.model";
export const account = Router()
account.get("/", (req: Request, res: Response, next: NextFunction)=> {
Account.findAll()
.then(accounts => {
res.json(accounts)
})
.catch(next)
})

View File

@@ -1,7 +1,80 @@
import { Request, Response, NextFunction, Router } from 'express'
import { Category } from '../models/category.model'
import { OrderedItem } from '../models/orderedItem.model'
import { Order } from '../models/order.model'
import { Product } from '../models/product.model'
import { Account } from '../models/account.model'
export const api = Router()
api.get("/", (req: Request, res: Response, next: NextFunction) => {
res.send("Hello World!")
})
api.post("/resetdatabase", (req: Request, res: Response, next: NextFunction) => {
// Step 1: Delete all data tables
Category.destroy({ truncate: true })
OrderedItem.destroy({ truncate: true })
Order.destroy({ truncate: true })
Product.destroy({ truncate: true })
Account.destroy({ truncate: true })
// Step 2: Prepopulate with default values
Category.bulkCreate(
[
{ id: 0, icon: "mdi-chip", name: "Electronic" },
{ id: 1, icon: "mdi-soccer", name: "Sports" },
{ id: 2, icon: "mdi-tshirt-crew", name: "Clothes" },
{ id: 3, icon: "mdi-bookshelf", name: "Books" }
]
)
Product.bulkCreate(
[
{ id: 0, brand: "Tuxedo", name: "Hypherion Ultra Max", price: 999.99, categoryId: 0, discount: 0 },
{ id: 1, brand: "Puma", name: "Men's Shirt", price: 14.99, categoryId: 2, discount: 0 },
{ id: 2, brand: "Puma", name: "Woman's Shirt", price: 14.99, categoryId: 2, discount: 0 },
{ id: 3, brand: "George Orwell", name: "1984", price: 9.99, categoryId: 3, discount: 0 },
{ id: 4, brand: "Johann W. Goethe", name: "Faust", price: 4.99, categoryId: 3, discount: 0 },
{ id: 5, brand: "Theodor Sturm", name: "Der Schimmelreiter", price: 4.99, categoryId: 3, discount: 0 },
{ id: 6, brand: "Aldous Huxley", name: "Brave New World", price: 7.99, categoryId: 3, discount: 0 },
]
)
Account.bulkCreate(
[
{ id: 0, username: "hagemeister93", password: "Xjt3qb5t", address: "Laportestraße 22, 30449 Hannover", firstName: "Laurin", lastName: "Hagemeister" },
{ id: 1, username: "katjaStoiber", password: "target123", address: "Gustav-Adolf-Straße 30, 30167 Hannover", firstName: "Katja", lastName: "Stoiber" },
{ id: 2, username: "oetkerohnek", password: "iloveyou", address: "Eckermannstraße 1, 30625 Hannover", firstName: "Luna", lastName: "Oeter" },
{ id: 3, username: "duranduran", password: "H4nn0ver", address: "Schlägerstraße 36, 30171 Hannover", firstName: "Jürgen", lastName: "Durand" },
{ id: 4, username: "guitarhero", password: "gwerty123", address: "Steinmetzstraße 12, 30163 Hannover", firstName: "Frederik", lastName: "Furtwängler" },
{ id: 5, username: "herbstMareike", password: "qhsrbpgrs", address: "Allerweg 33, 30851 Langenhagen", firstName: "Mareike", lastName: "Herbst" },
{ id: 6, username: "seibertmitb", password: "{jkz+WvQe", address: "Marktstraße 26, 30880 Laatzen", firstName: "Janna", lastName: "Seibert" },
]
)
Order.bulkCreate(
[
{ id: 0, accountId: 0, totalPrice: 0 },
{ id: 1, accountId: 1, totalPrice: 0 },
{ id: 2, accountId: 1, totalPrice: 0 },
{ id: 3, accountId: 2, totalPrice: 0 },
{ id: 4, accountId: 2, totalPrice: 0 },
{ id: 5, accountId: 3, totalPrice: 0 },
{ id: 6, accountId: 3, totalPrice: 0 },
{ id: 7, accountId: 3, totalPrice: 0 },
{ id: 8, accountId: 4, totalPrice: 0 },
{ id: 9, accountId: 5, totalPrice: 0 },
{ id: 10, accountId: 6, totalPrice: 0 },
{ id: 11, accountId: 6, totalPrice: 0 },
]
)
OrderedItem.bulkCreate(
[
{ orderId: 0, productId: 4, quantity: 2, totalPrice: 0 }
]
)
res.send("Success")
})

View File

@@ -1,20 +0,0 @@
import { Router, Request, Response, NextFunction } from "express";
import { Categories } from "../models/categories.model";
export const categories = Router()
categories.get("/", (req: Request, res: Response, next: NextFunction) => {
Categories.findAll()
.then(categories => res.json(categories))
.catch(next)
})
categories.post("/", (req: Request, res: Response, next: NextFunction) => {
try {
console.log(req.body)
const category = Categories.create(req.body)
res.status(201).json(category)
} catch (e) {
next(e)
}
})

View File

@@ -0,0 +1,22 @@
import { Router, Request, Response, NextFunction } from "express";
import { Category } from "../models/category.model";
export const category = Router()
category.get("/", (req: Request, res: Response, next: NextFunction) => {
Category.findAll()
.then(categories => {
res.json(categories)
})
.catch(next)
})
category.post("/", (req: Request, res: Response, next: NextFunction) => {
try {
console.log(req.body)
const category = Category.create(req.body)
res.status(201).json(category)
} catch (e) {
next(e)
}
})

View File

@@ -0,0 +1,12 @@
import { Router, Request, Response, NextFunction } from "express";
import { Order } from "../models/order.model";
export const order = Router()
order.get("/", (req: Request, res: Response, next: NextFunction) => {
Order.findAll()
.then(orders => {
res.json(orders)
})
.catch(next)
})

View File

@@ -0,0 +1,12 @@
import { Router, Request, Response, NextFunction } from "express";
import { OrderedItem } from "../models/orderedItem.model";
export const orderedItem = Router()
orderedItem.get("/", (req: Request, res: Response, next: NextFunction) => {
OrderedItem.findAll()
.then(orderedItems => {
res.json(orderedItems)
})
.catch(next)
})

View File

@@ -0,0 +1,12 @@
import { Router, Request, Response, NextFunction } from "express";
import { Product } from "../models/product.model";
export const product = Router()
product.get("/", (req: Request, res: Response, next: NextFunction)=> {
Product.findAll()
.then(products => {
res.json(products)
})
.catch(next)
})

View File

@@ -2,8 +2,12 @@ import express from 'express'
import cors from 'cors'
import bodyParser from 'body-parser'
import { api } from './routes/api.routes'
import { categories } from './routes/categories.routes'
import { startDatabase } from './database'
import { category } from './routes/category.routes'
import { product } from './routes/product.routes'
import { order } from './routes/order.routes'
import { orderedItem } from './routes/orderedItem.routes'
import { account } from './routes/account.routes'
const app = express()
const port = 3000
@@ -19,7 +23,11 @@ startDatabase()
// Routes
app.use("/api", api)
app.use("/categories", categories)
app.use("/categories", category)
app.use("/products", product)
app.use("/orders", order)
app.use("/ordereditems", orderedItem)
app.use("/accounts", account)
// Start server
app.listen(port, () => {