diff --git a/banner/types/Banner.ts b/banner/types/Banner.ts new file mode 100644 index 0000000..e1f1698 --- /dev/null +++ b/banner/types/Banner.ts @@ -0,0 +1,8 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { Entity } from "../../entities/types/Entity"; +import { BannerDTO } from "./BannerDTO"; + +export interface Banner extends Entity { + +} diff --git a/banner/types/BannerModel.ts b/banner/types/BannerDTO.ts similarity index 90% rename from banner/types/BannerModel.ts rename to banner/types/BannerDTO.ts index 54ba0a0..d681380 100644 --- a/banner/types/BannerModel.ts +++ b/banner/types/BannerDTO.ts @@ -1,11 +1,12 @@ // Copyright (c) 2021-2024. Sendanor . All rights reserved. +import { DTO } from "../../entities/types/DTO"; import { BannerLanguage } from "./BannerLanguage"; import { BannerLocation } from "./BannerLocation"; import { BannerState } from "./BannerState"; import { BannerType } from "./BannerType"; -export interface BannerModel { +export interface BannerDTO extends DTO { /** * The state of the banner @@ -41,8 +42,8 @@ export interface BannerModel { readonly imageUrl : string; readonly imageAlt : string; - readonly languages ?: BannerLanguage[]; - readonly locations ?: BannerLocation[]; + readonly languages ?: readonly BannerLanguage[]; + readonly locations ?: readonly BannerLocation[]; readonly draftTime ?: string; readonly startTime ?: string; diff --git a/banner/types/BannerEntity.ts b/banner/types/BannerEntity.ts new file mode 100644 index 0000000..6df49ee --- /dev/null +++ b/banner/types/BannerEntity.ts @@ -0,0 +1,104 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { EntityFactoryImpl } from "../../entities/types/EntityFactoryImpl"; +import { VariableType } from "../../entities/types/VariableType"; +import { Banner } from "./Banner"; +import { BannerDTO } from "./BannerDTO"; +import { BannerLanguage } from "./BannerLanguage"; +import { BannerLocation } from "./BannerLocation"; +import { BannerState } from "./BannerState"; +import { BannerType } from "./BannerType"; + +export const BannerEntityFactory = ( + EntityFactoryImpl.create('Banner') + .add( EntityFactoryImpl.createProperty("state").setTypes(BannerState) ) + .add( EntityFactoryImpl.createProperty("type").setTypes(BannerType) ) + .add( EntityFactoryImpl.createProperty("addTitleText").setTypes(VariableType.BOOLEAN) ) + .add( EntityFactoryImpl.createProperty("addAltText").setTypes(VariableType.BOOLEAN) ) + .add( EntityFactoryImpl.createProperty("title").setTypes(VariableType.STRING) ) + .add( EntityFactoryImpl.createProperty("url").setTypes(VariableType.STRING) ) + .add( EntityFactoryImpl.createProperty("imageUrl").setTypes(VariableType.STRING) ) + .add( EntityFactoryImpl.createProperty("imageAlt").setTypes(VariableType.STRING) ) + .add( EntityFactoryImpl.createOptionalArrayProperty("languages").setTypes(BannerLanguage) ) + .add( EntityFactoryImpl.createOptionalArrayProperty("locations").setTypes(BannerLocation) ) + .add( EntityFactoryImpl.createProperty("draftTime").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("startTime").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("publishTime").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("endTime").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("started").setTypes(VariableType.BOOLEAN, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("published").setTypes(VariableType.BOOLEAN, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("ended").setTypes(VariableType.BOOLEAN, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("owner").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) +); + +export const isBannerDTO = BannerEntityFactory.createTestFunctionOfDTO(); + +export const isBanner = BannerEntityFactory.createTestFunctionOfInterface(); + +export const explainBannerDTO = BannerEntityFactory.createExplainFunctionOfDTO(); + +export const isBannerDTOOrUndefined = BannerEntityFactory.createTestFunctionOfDTOorOneOf(VariableType.UNDEFINED); + +export const explainBannerDTOOrUndefined = BannerEntityFactory.createExplainFunctionOfDTOorOneOf(VariableType.UNDEFINED); + +export const BaseBannerEntity = BannerEntityFactory.createEntityType(); + +/** + * Banner entity. + */ +export class BannerEntity + extends BaseBannerEntity + implements Banner +{ + + /** + * Creates a Banner entity. + * + * @param value The optional DTO of Banner + */ + public static create ( + value ?: BannerDTO, + ) : BannerEntity { + return new BannerEntity(value); + } + + /** + * Creates a Banner entity from DTO. + * + * @param dto The optional DTO of Banner + */ + public static createFromDTO ( + dto : BannerDTO, + ) : BannerEntity { + return new BannerEntity(dto); + } + + /** + * Normalizes the value as a DTO. + */ + public static toDTO ( + value: BannerDTO | Banner | BannerEntity, + ) : BannerDTO { + if (isBannerEntity(value)) { + return value.getDTO(); + } else if (isBanner(value)) { + return value.getDTO(); + } else { + return value; + } + } + + /** + * Construct an entity of BannerEntity. + */ + public constructor ( + dto ?: BannerDTO | undefined, + ) { + super(dto); + } + +} + +export function isBannerEntity (value: unknown): value is BannerEntity { + return value instanceof BannerEntity; +}