From b577ddf47dc1c78423b94ae39d458781cc4f0b33 Mon Sep 17 00:00:00 2001 From: TobiZog Date: Mon, 9 Sep 2024 13:45:26 +0200 Subject: [PATCH] Moving exmple datasets to json files --- software/backend/data/accounts.json | 60 ++++++++++++++ software/backend/data/categories.json | 8 ++ software/backend/data/orderedItems.json | 10 +++ software/backend/data/orders.json | 16 ++++ software/backend/data/products.json | 92 ++++++++++++++++++++++ software/backend/routes/api.routes.ts | 80 +------------------ software/backend/scripts/categories.json | 5 ++ software/backend/scripts/databaseHelper.ts | 33 ++++++++ software/tsconfig.json | 1 + software/tsconfig.server.json | 6 +- 10 files changed, 233 insertions(+), 78 deletions(-) create mode 100644 software/backend/data/accounts.json create mode 100644 software/backend/data/categories.json create mode 100644 software/backend/data/orderedItems.json create mode 100644 software/backend/data/orders.json create mode 100644 software/backend/data/products.json create mode 100644 software/backend/scripts/categories.json create mode 100644 software/backend/scripts/databaseHelper.ts diff --git a/software/backend/data/accounts.json b/software/backend/data/accounts.json new file mode 100644 index 0000000..eb1a498 --- /dev/null +++ b/software/backend/data/accounts.json @@ -0,0 +1,60 @@ +{ + "data": [ + { + "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" + } + ] +} \ No newline at end of file diff --git a/software/backend/data/categories.json b/software/backend/data/categories.json new file mode 100644 index 0000000..3ced7fd --- /dev/null +++ b/software/backend/data/categories.json @@ -0,0 +1,8 @@ +{ + "data": [ + { "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" } + ] +} \ No newline at end of file diff --git a/software/backend/data/orderedItems.json b/software/backend/data/orderedItems.json new file mode 100644 index 0000000..df6300c --- /dev/null +++ b/software/backend/data/orderedItems.json @@ -0,0 +1,10 @@ +{ + "data": [ + { + "orderId": 0, + "productId": 4, + "quantity": 2, + "totalPrice": 0 + } + ] +} \ No newline at end of file diff --git a/software/backend/data/orders.json b/software/backend/data/orders.json new file mode 100644 index 0000000..cc44c8e --- /dev/null +++ b/software/backend/data/orders.json @@ -0,0 +1,16 @@ +{ + "data": [ + { "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 } + ] +} \ No newline at end of file diff --git a/software/backend/data/products.json b/software/backend/data/products.json new file mode 100644 index 0000000..d203121 --- /dev/null +++ b/software/backend/data/products.json @@ -0,0 +1,92 @@ +{ + "data": [ + { + "id": 0, + "brand": "Lenovo", + "name": "Thinkpad T14", + "price": 799.99, + "categoryId": 0, + "discount": 10, + "rating": 4.6, + "description": "Die stabile Arbeitsmaschine. Mit AMD Ryzen 7 89029U, 128 GB RAM und 8 TB M.2 SSD!", + "imageUrl": "https://f.media-amazon.com/images/I/51OHlMuJ5pL._AC_SL1280_.jpg" + }, + { + "id": 1, + "brand": "Puma", + "name": "Men's Shirt", + "price": 14.99, + "categoryId": 2, + "discount": 0, + "rating": 3.8, + "description": "", + "imageUrl": "https://images.unsplash.com/photo-1496346236646-50e985b31ea4?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NHx8dCUyMHNoaXJ0JTIwbWVufGVufDB8fDB8fHww" + }, + { + "id": 2, + "brand": "Puma", + "name": "Woman's Shirt", + "price": 14.99, + "categoryId": 2, + "discount": 0, + "rating": 4.0, + "description": "", + "imageUrl": "https://images.unsplash.com/photo-1485218126466-34e6392ec754?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8N3x8dCUyMHNoaXJ0JTIwd29tYW58ZW58MHx8MHx8fDA%3D" + }, + { + "id": 3, + "brand": "George Orwell", + "name": "1984", + "price": 9.99, + "categoryId": 3, + "discount": 0, + "rating": 4.9, + "imageUrl": "https://f.media-amazon.com/images/I/81BExVLr8sL._SL1500_.jpg", + "description": "BIG BROTHER IS WATCHING YOU! George Orwells 1984 ist längst zu einer scheinbar nicht mehr erklärungsbedürftigen Metapher für totalitäre Verhältnisse geworden. Mit atemberaubender Unerbittlichkeit zeichnet der Autor das erschreckende Bild einer durch und..." + }, + { + "id": 4, + "brand": "Johann W. Goethe", + "name": "Faust", + "price": 4.99, + "categoryId": 3, + "discount": 0, + "rating": 4.2, + "description": "", + "imageUrl": "https://f.media-amazon.com/images/I/71p1k4JwDqL._SL1500_.jpg" + }, + { + "id": 5, + "brand": "Theodor Sturm", + "name": "Der Schimmelreiter", + "price": 4.99, + "categoryId": 3, + "discount": 0, + "rating": 3.5, + "description": "", + "imageUrl": "https://f.media-amazon.com/images/I/81uUWtGmKtL._SL1500_.jpg" + }, + { + "id": 6, + "brand": "Aldous Huxley", + "name": "Brave New World", + "price": 7.99, + "categoryId": 3, + "discount": 0, + "rating": 4.4, + "imageUrl": "https://f.media-amazon.com/images/I/917t3Joq2WL._SL1500_.jpg", + "description": "Brave New World beschreibt eine genormte Gesellschaft, in der Föten genetisch manipuliert und Menschen konditioniert werden. Ziel des Staates ist Zufriedenheit und Stabilität, und dies wird durch Gleichheit, Drogen und Propaganda erreicht. Gott und Religion..." + }, + { + "id": 7, + "brand": "Ankermann", + "name": "Gaming Work V3", + "price": 1299.99, + "categoryId": 0, + "discount": 0, + "rating": 4.7, + "description": "", + "imageUrl": "https://f.media-amazon.com/images/I/81gpDyNWhzL._AC_SL1500_.jpg" + } + ] +} \ No newline at end of file diff --git a/software/backend/routes/api.routes.ts b/software/backend/routes/api.routes.ts index f833e36..806391c 100644 --- a/software/backend/routes/api.routes.ts +++ b/software/backend/routes/api.routes.ts @@ -1,9 +1,5 @@ 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' +import { deleteAllTables, prepopulateDatabase } from '../scripts/databaseHelper' export const api = Router() @@ -13,79 +9,11 @@ api.get("/", (req: Request, res: Response, next: NextFunction) => { api.get("/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 }) + deleteAllTables() // 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: "Lenovo", name: "Thinkpad T14", price: 799.99, categoryId: 0, discount: 10, - rating: 4.6, description: "Die stabile Arbeitsmaschine. Mit AMD Ryzen 7 89029U, 128 GB RAM und 8 TB M.2 SSD!", imageUrl: "https://f.media-amazon.com/images/I/51OHlMuJ5pL._AC_SL1280_.jpg" }, - { id: 1, brand: "Puma", name: "Men's Shirt", price: 14.99, categoryId: 2, discount: 0, - rating: 3.8, description: "", imageUrl: "https://images.unsplash.com/photo-1496346236646-50e985b31ea4?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NHx8dCUyMHNoaXJ0JTIwbWVufGVufDB8fDB8fHww" }, - { id: 2, brand: "Puma", name: "Woman's Shirt", price: 14.99, categoryId: 2, discount: 0, - rating: 4.0, description: "", imageUrl: "https://images.unsplash.com/photo-1485218126466-34e6392ec754?w=800&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8N3x8dCUyMHNoaXJ0JTIwd29tYW58ZW58MHx8MHx8fDA%3D" }, - { id: 3, brand: "George Orwell", name: "1984", price: 9.99, categoryId: 3, discount: 0, - rating: 4.9, imageUrl: "https://f.media-amazon.com/images/I/81BExVLr8sL._SL1500_.jpg", - description: "BIG BROTHER IS WATCHING YOU! George Orwells 1984 ist längst zu einer scheinbar nicht mehr erklärungsbedürftigen Metapher für totalitäre Verhältnisse geworden. Mit atemberaubender Unerbittlichkeit zeichnet der Autor das erschreckende Bild einer durch und..." }, - { id: 4, brand: "Johann W. Goethe", name: "Faust", price: 4.99, categoryId: 3, discount: 0, - rating: 4.2, description: "", imageUrl: "https://f.media-amazon.com/images/I/71p1k4JwDqL._SL1500_.jpg" }, - { id: 5, brand: "Theodor Sturm", name: "Der Schimmelreiter", price: 4.99, categoryId: 3, discount: 0, - rating: 3.5, description: "", imageUrl: "https://f.media-amazon.com/images/I/81uUWtGmKtL._SL1500_.jpg" }, - { id: 6, brand: "Aldous Huxley", name: "Brave New World", price: 7.99, categoryId: 3, discount: 0, - rating: 4.4, imageUrl: "https://f.media-amazon.com/images/I/917t3Joq2WL._SL1500_.jpg", - description: "Brave New World beschreibt eine genormte Gesellschaft, in der Föten genetisch manipuliert und Menschen konditioniert werden. Ziel des Staates ist Zufriedenheit und Stabilität, und dies wird durch Gleichheit, Drogen und Propaganda erreicht. Gott und Religion..." }, - { id: 7, brand: "Ankermann", name: "Gaming Work V3", price: 1299.99, categoryId: 0, discount: 0, - rating: 4.7, description: "", imageUrl: "https://f.media-amazon.com/images/I/81gpDyNWhzL._AC_SL1500_.jpg" } - ] - ) - - 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 } - ] - ) + prepopulateDatabase() + // Step 3: Send status back res.status(200).send() }) \ No newline at end of file diff --git a/software/backend/scripts/categories.json b/software/backend/scripts/categories.json new file mode 100644 index 0000000..3918073 --- /dev/null +++ b/software/backend/scripts/categories.json @@ -0,0 +1,5 @@ +{ + "id": 1, + "title": "Hello World", + "completed": false +} \ No newline at end of file diff --git a/software/backend/scripts/databaseHelper.ts b/software/backend/scripts/databaseHelper.ts new file mode 100644 index 0000000..90003b1 --- /dev/null +++ b/software/backend/scripts/databaseHelper.ts @@ -0,0 +1,33 @@ +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' + +import categories from "./../data/categories.json" +import products from "./../data/products.json" +import accounts from "./../data/accounts.json" +import orders from "./../data/orders.json" +import orderedItems from "./../data/orderedItems.json" + +/** + * Delete all datasets in every database table + */ +export function deleteAllTables() { + Category.destroy({ truncate: true }) + OrderedItem.destroy({ truncate: true }) + Order.destroy({ truncate: true }) + Product.destroy({ truncate: true }) + Account.destroy({ truncate: true }) +} + +/** + * Insert default datasets in the database tables + */ +export function prepopulateDatabase() { + Category.bulkCreate(categories.data) + Product.bulkCreate(products.data) + Account.bulkCreate(accounts.data) + Order.bulkCreate(orders.data) + OrderedItem.bulkCreate(orderedItems.data) +} \ No newline at end of file diff --git a/software/tsconfig.json b/software/tsconfig.json index 5a09a36..48f8a79 100644 --- a/software/tsconfig.json +++ b/software/tsconfig.json @@ -9,6 +9,7 @@ "experimentalDecorators": true, "emitDecoratorMetadata": true, "baseUrl": ".", + "resolveJsonModule": true, "paths": { "@/*": ["./src/*"] } diff --git a/software/tsconfig.server.json b/software/tsconfig.server.json index 069a37f..dd1bbcc 100644 --- a/software/tsconfig.server.json +++ b/software/tsconfig.server.json @@ -8,10 +8,12 @@ "esModuleInterop": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, - "baseUrl": "." + "baseUrl": ".", + "resolveJsonModule": true }, "exclude": ["node_modules", "dist"], "include": [ - "backend/**/*.ts" + "backend/**/*.ts", + "backend/**/*.json" ] } \ No newline at end of file