Atomize model classes

This commit is contained in:
2024-10-11 17:42:21 +02:00
parent 8e7c9a949d
commit 8f0de99634
59 changed files with 432 additions and 356 deletions

View File

@@ -1,7 +1,6 @@
import { RatingModel } from "@/data/models/acts/ratingModel"
import { dateToHumanReadableString } from "./dateTimeScripts"
import { TourModel } from "@/data/models/acts/tourModel"
import { EventModel } from "@/data/models/acts/eventModel"
import { ConcertModel } from "@/data/models/acts/concertModel"
/**
* Calculate a price based on parameters
@@ -16,6 +15,7 @@ export function calcPrice(price: number, quantity: number = 1): number {
return Math.round(quantity * price * 100) / 100
}
/**
* Calculate the average of an Array of ratings
*
@@ -33,6 +33,14 @@ export function calcRating(ratings: Array<RatingModel>) {
return sum / ratings.length
}
/**
* Classifies a bunch of ratings to groups from 1 to 5 stars
*
* @param ratings Array of RatingModels
*
* @returns Array of Objects: { value: number[1-5], count: number }
*/
export function calcRatingValues(ratings: Array<RatingModel>) {
let ratingValues = [
{ value: 1, count: 0 },
@@ -57,10 +65,10 @@ export function calcRatingValues(ratings: Array<RatingModel>) {
*
* @returns A date string. If one concert: dd.MM.YYYY, if two or more: dd.MM.YYYY - dd.MM.YYYY
*/
export function createDateRangeString(event: EventModel) {
export function createDateRangeString(concerts: Array<ConcertModel>) {
const dateArray: Array<Date> = []
for (let concert of event.concerts) {
for (let concert of concerts) {
dateArray.push(new Date(concert.date))
}
@@ -85,10 +93,10 @@ export function createDateRangeString(event: EventModel) {
*
* @returns Lowest ticket price, rounded to two floating point digits
*/
export function lowestTicketPrice(event: EventModel): string {
export function lowestTicketPrice(concerts: Array<ConcertModel>): string {
const priceArray : Array<number> = []
for (let concert of event.concerts) {
for (let concert of concerts) {
priceArray.push(concert.price)
}

View File

@@ -1,9 +1,23 @@
/**
* Concert a date object to german time string
*
* @param date Date object
*
* @returns German date string, e.g. 31.12.2024
*/
export function dateToHumanReadableString(date: Date) {
return String(date.getDate()).padStart(2, '0') + '.' +
String(date.getMonth() + 1).padStart(2, '0') + '.' +
date.getFullYear()
}
/**
* Convert ISO time string to german time string
*
* @param string ISO time string, e.g. 2024-12-31
*
* @returns German date string, e.g. 31.12.2024
*/
export function dateStringToHumanReadableString(string: string) {
return dateToHumanReadableString(new Date(string))
}