From f8bdb54c3352ebc5c14efd6ebfb10b53108277b1 Mon Sep 17 00:00:00 2001 From: Tobias Zoghaib Date: Sat, 12 Oct 2024 16:22:14 +0200 Subject: [PATCH] Simplified json import --- misc/database.drawio | 11 +- software/backend/data/accounts.json | 8 - software/backend/data/cities-locations.json | 2 +- software/backend/data/exercises.json | 2 +- software/backend/data/orders.json | 23 +- .../backend/models/ordering/order.model.ts | 4 - software/backend/scripts/databaseHelper.ts | 197 +++++++++++------- 7 files changed, 136 insertions(+), 111 deletions(-) diff --git a/misc/database.drawio b/misc/database.drawio index d607986..e5696f3 100644 --- a/misc/database.drawio +++ b/misc/database.drawio @@ -1,6 +1,6 @@ - + @@ -270,9 +270,6 @@ - - - @@ -451,10 +448,10 @@ - + - + @@ -532,7 +529,7 @@ - + diff --git a/software/backend/data/accounts.json b/software/backend/data/accounts.json index 20b93d8..4a3bfce 100644 --- a/software/backend/data/accounts.json +++ b/software/backend/data/accounts.json @@ -8,8 +8,6 @@ "lastName": "Hagemeister", "addresses": [ { - "id": 0, - "accountId": 0, "street": "Laportestraße", "houseNumber": 22, "postalCode": 30449, @@ -18,8 +16,6 @@ ], "payments": [ { - "id": 0, - "accountId": 0, "bankName": "Deutsche Bank", "iban": "DE92500105175721645777" } @@ -34,8 +30,6 @@ "lastName": "Stoiber", "addresses": [ { - "id": 1, - "accountId": 1, "street": "Gustav-Adolf-Straße", "houseNumber": 30, "postalCode": 30167, @@ -44,8 +38,6 @@ ], "payments": [ { - "id": 1, - "accountId": 1, "bankName": "DZ Bank", "iban": "DE12500105179557939114" } diff --git a/software/backend/data/cities-locations.json b/software/backend/data/cities-locations.json index 77d3008..97a2818 100644 --- a/software/backend/data/cities-locations.json +++ b/software/backend/data/cities-locations.json @@ -1,5 +1,5 @@ { - "data": [ + "cities": [ { "name": "Hannover", "country": "Germany", diff --git a/software/backend/data/exercises.json b/software/backend/data/exercises.json index b59659d..9acd493 100644 --- a/software/backend/data/exercises.json +++ b/software/backend/data/exercises.json @@ -1,5 +1,5 @@ { - "data": [ + "groups": [ { "nameDe": "Den Shop kennenlernen", "nameEn": "Getting to know the shop", diff --git a/software/backend/data/orders.json b/software/backend/data/orders.json index bbc9510..8a1b6fb 100644 --- a/software/backend/data/orders.json +++ b/software/backend/data/orders.json @@ -1,13 +1,11 @@ { - "data": [ + "orders": [ { "username": "hagemeister93", - "shippingProgress": 4, - "addressId": 0, - "paymentId": 0, "tickets": [ { - "concertId": 0, + "date": "2024-10-18", + "concertGroupName": "Unlimited Love", "orderPrice": 184, "seatGroup": "A", "seatRow": 0, @@ -17,19 +15,18 @@ }, { "username": "duranduran", - "shippingProgress": 5, - "addressId": 4, - "paymentId": 3, "tickets": [ { - "concertId": 0, + "date": "2024-10-18", + "concertGroupName": "Unlimited Love", "orderPrice": 184, "seatGroup": "A", "seatRow": 0, "seat": 2 }, { - "concertId": 0, + "date": "2024-10-18", + "concertGroupName": "Unlimited Love", "orderPrice": 184, "seatGroup": "A", "seatRow": 0, @@ -39,12 +36,10 @@ }, { "username": "duranduran", - "shippingProgress": 2, - "addressId": 5, - "paymentId": 3, "tickets": [ { - "concertId": 0, + "date": "2024-10-18", + "concertGroupName": "Unlimited Love", "orderPrice": 184, "seatGroup": "A", "seatRow": 0, diff --git a/software/backend/models/ordering/order.model.ts b/software/backend/models/ordering/order.model.ts index 47b8c95..a536a72 100644 --- a/software/backend/models/ordering/order.model.ts +++ b/software/backend/models/ordering/order.model.ts @@ -16,10 +16,6 @@ export class Order extends Model { @Column orderedAt: Date - @Default(1) - @Column - shippingProgress: number - @ForeignKey(() => Address) @Column addressId: number diff --git a/software/backend/scripts/databaseHelper.ts b/software/backend/scripts/databaseHelper.ts index 483dd42..0650dea 100644 --- a/software/backend/scripts/databaseHelper.ts +++ b/software/backend/scripts/databaseHelper.ts @@ -23,7 +23,8 @@ import orders from "./../data/orders.json" import accountRoles from "./../data/accountRoles.json" import citiesLocations from "./../data/cities-locations.json" import exercises from "./../data/exercises.json" -import bands from "./../data/bands-concerts.json" +import bandsConcerts from "./../data/bands-concerts.json" +import { Op } from 'sequelize' /** @@ -57,7 +58,7 @@ export function deleteExerciseProgressTables() { } export async function prepopulateExerciseDatabase() { - for (let exerciseGroup of exercises.data) { + for (let exerciseGroup of exercises.groups) { ExerciseGroup.create(exerciseGroup) .then(async dataset => { for (let exercise of exerciseGroup.exercises) { @@ -75,10 +76,11 @@ export async function prepopulateExerciseDatabase() { export async function prepopulateDatabase() { deleteAllTables() - AccountRole.bulkCreate(accountRoles.data) - //Genre.bulkCreate(genres.data) - for (let city of citiesLocations.data) + + ////////// Locations and Seat Tables ////////// + + for (let city of citiesLocations.cities) { await City.create(city) .then(async cityDataset => { @@ -93,42 +95,41 @@ export async function prepopulateDatabase() { { seatGroup["locationId"] = locationDataset.id - // todo activate - // await SeatGroup.create(seatGroup) - // .then(async seatGroupRes => { - // if (seatGroup.standingArea) { - // // In an area without seats, create one row with all "seats" - // await SeatRow.create({ - // row: 0, - // seatGroupId: seatGroupRes.id - // }) - // .then(async seatRowRes => { - // for (let i = 0; i < seatGroup.capacity; i++) { - // await Seat.create({ - // seatNr: i + 1, - // seatRowId: seatRowRes.id - // }) - // } - // }) - // } - // else - // { - // for (let row = 0; row < seatGroup.rows; row++) { - // await SeatRow.create({ - // row: row + 1, - // seatGroupId: seatGroupRes.id - // }) - // .then(async seatRowRes => { - // for (let col = 0; col < seatGroup.capacity / seatGroup.rows; col++) { - // await Seat.create({ - // seatNr: col, - // seatRowId: seatRowRes.id - // }) - // } - // }) - // } - // } - // }) + await SeatGroup.create(seatGroup) + .then(async seatGroupRes => { + if (seatGroup.standingArea) { + // In an area without seats, create one row with all "seats" + await SeatRow.create({ + row: 0, + seatGroupId: seatGroupRes.id + }) + .then(async seatRowRes => { + for (let i = 0; i < seatGroup.capacity; i++) { + await Seat.create({ + seatNr: i + 1, + seatRowId: seatRowRes.id + }) + } + }) + } + else + { + for (let row = 0; row < seatGroup.rows; row++) { + await SeatRow.create({ + row: row + 1, + seatGroupId: seatGroupRes.id + }) + .then(async seatRowRes => { + for (let col = 0; col < seatGroup.capacity / seatGroup.rows; col++) { + await Seat.create({ + seatNr: col, + seatRowId: seatRowRes.id + }) + } + }) + } + } + }) } }) } @@ -136,7 +137,11 @@ export async function prepopulateDatabase() { } - // Account & Sub tables + + ////////// Account Tables ////////// + + AccountRole.bulkCreate(accountRoles.data) + for (let account of accounts.data) { await Account.create(account) .then(async dataset => { @@ -161,7 +166,10 @@ export async function prepopulateDatabase() { } - for(let band of bands.bands) { + + ////////// Band and Concert Tables ////////// + + for(let band of bandsConcerts.bands) { // Create a band dataset await Band.create(band) .then(async dataset => { @@ -230,38 +238,75 @@ export async function prepopulateDatabase() { }) } - // for (let order of orders.data) { - // await Order.create(order) - // .then(async dataset => { - // for (let ticket of order.tickets) { - // ticket["orderId"] = dataset.id - // SeatGroup.findOne({ - // where: { - // name: ticket.seatGroup - // } - // }) - // .then(seatGroup => { - // SeatRow.findOne({ - // where: { - // seatGroupId: seatGroup.id, - // row: ticket.seatRow - // } - // }) - // .then(seatRow => { - // Seat.findOne({ - // where: { - // seatRowId: seatRow.id, - // seatNr: ticket.seat - // } - // }) - // .then(async seat => { - // ticket["seatId"] = seat.id - // await Ticket.create(ticket) - // }) - // }) - // }) - // } - // }) - // } + + ////////// Order and Ticket Tables ////////// + + for (let order of orders.orders) { + let account = await Account.findOne({ + where: { + username: order.username + } + }) + + let payment = await Payment.findOne({ + where: { + accountId: account.dataValues.id + } + }) + + let address = await Address.findOne({ + where: { + accountId: account.dataValues.id + } + }) + + await Order.create({ + accountId: account.dataValues.id, + paymentId: payment.dataValues.id, + addressId: address.dataValues.id + }) + .then(async dataset => { + for (let ticket of order.tickets) { + let concert = await Concert.findOne({ + where: { + [Op.and] : [ + { name: ticket.concertGroupName }, + { date: ticket.date } + ] + } + }) + + SeatGroup.findOne({ + where: { + name: ticket.seatGroup + } + }) + .then(seatGroup => { + SeatRow.findOne({ + where: { + seatGroupId: seatGroup.id, + row: ticket.seatRow + } + }) + .then(seatRow => { + Seat.findOne({ + where: { + seatRowId: seatRow.id, + seatNr: ticket.seat + } + }) + .then(async seat => { + await Ticket.create({ + orderId: dataset.dataValues.id, + concertId: concert.dataValues.id, + orderPrice: ticket.orderPrice, + seatId: seat.dataValues.id + }) + }) + }) + }) + } + }) + } } \ No newline at end of file