Skip to content

Commit

Permalink
ew-1009: proper mapping for all DTOs implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
HKayed committed Oct 18, 2024
1 parent fb0bafa commit 5b88b9d
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class CardResponseDto {
id: string,
title: string,
height: number,
elements: Array<CardResponseElementsInnerDto>,
elements: CardResponseElementsInnerDto[],
visibilitySettings: VisibilitySettingsResponseDto,
timeStamps: TimestampResponseDto
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class CollaborativeTextEditorElementResponseDto {

content: object;

constructor(id: string, type: ContentElementType, timestamps: TimestampResponseDto, content: object) {
constructor(id: string, type: ContentElementType, content: object, timestamps: TimestampResponseDto) {
this.id = id;
this.type = type;
this.timestamps = timestamps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class DrawingElementResponseDto {
constructor(
id: string,
type: ContentElementType,
timestamps: TimestampResponseDto,
content: DrawingElementContentDto
content: DrawingElementContentDto,
timestamps: TimestampResponseDto
) {
this.id = id;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,205 @@
import { CardResponse } from '../cards-api-client';
import {
CardResponse,
CardResponseElementsInner,
VisibilitySettingsResponse,
TimestampsResponse,
DeletedElementResponse,
SubmissionContainerElementResponse,
DrawingElementResponse,
ExternalToolElementResponse,
FileElementResponse,
LinkElementResponse,
RichTextElementResponse,
} from '../cards-api-client';
import { CardResponseElementsInnerDto } from '../dto/card-response-elements-inner.dto';
import { CardResponseDto } from '../dto/card-response.dto';
import { CollaborativeTextEditorElementResponseDto } from '../dto/collaborative-text-editor-element-response.dto';
import { DeletedElementResponseDto } from '../dto/deleted-element-response.dto';
import { DrawingElementContentDto } from '../dto/drawing-element-content.dto';
import { DrawingElementResponseDto } from '../dto/drawing-element-response.dto';
import { ExternalToolElementContentDto } from '../dto/external-tool-element-content.dto';
import { ExternalToolElementResponseDto } from '../dto/external-tool-element-response.dto';
import { FileElementContentDto } from '../dto/file-element-content.dto';
import { FileElementResponseDto } from '../dto/file-element-response.dto';
import { LinkElementContentDto } from '../dto/link-element-content.dto';
import { LinkElementResponseDto } from '../dto/link-element-response.dto';
import { RichTextElementContentDto } from '../dto/rich-text-element-content.dto';
import { RichTextElementResponseDto } from '../dto/rich-text-element-response.dto';
import { SubmissionContainerElementResponseDto } from '../dto/submission-container-element-response.dto';
import { ContentElementType } from '../enums/content-element-type.enum';
import { SubmissionContainerElementContentDto } from '../dto/submission-container-element-content.dto';
import { DeletedElementContentDto } from '../dto/deleted-element-content.dto';
import { VisibilitySettingsResponseDto } from '../dto/visibility-settings-response.dto';
import { TimestampResponseDto } from '../dto/timestamp-response.dto';
import { CardContentElementInner } from '../types/card-content-elements-inner.type';

export class CardResponseMapper {
public static mapToCardResponseDto(cardResponse: CardResponse) {
public static mapToCardResponseDto(cardResponse: CardResponse): CardResponseDto {
return new CardResponseDto(
cardResponse.id,
cardResponse.title!,
cardResponse.height,
cardResponse.elements,
cardResponse.visibilitySettings,
cardResponse.timestamps
this.mapToCardResponseElementsInnerDto(cardResponse.elements),
this.mapToVisibilitySettingsDto(cardResponse.visibilitySettings),
this.mapToTimestampDto(cardResponse.timestamps)
);
}

private static mapTo
private static mapToCardResponseElementsInnerDto(
cardResponseElementsInner: CardResponseElementsInner[]
): CardResponseElementsInnerDto[] {
const elements: CardResponseElementsInnerDto[] = [];

cardResponseElementsInner.forEach((element) => {
switch (element.type) {
case ContentElementType.COLLABORATIVE_TEXT_EDITOR:
elements.push(
new CollaborativeTextEditorElementResponseDto(
element.id,
ContentElementType.COLLABORATIVE_TEXT_EDITOR,
this.mapToElementsContent(element, ContentElementType.COLLABORATIVE_TEXT_EDITOR),
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.DELETED:
elements.push(
new DeletedElementResponseDto(
element.id,
ContentElementType.DELETED,
this.mapToElementsContent(element, ContentElementType.DELETED) as DeletedElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.SUBMISSION_CONTAINER:
elements.push(
new SubmissionContainerElementResponseDto(
element.id,
ContentElementType.SUBMISSION_CONTAINER,
this.mapToElementsContent(
element,
ContentElementType.SUBMISSION_CONTAINER
) as SubmissionContainerElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.DRAWING:
elements.push(
new DrawingElementResponseDto(
element.id,
ContentElementType.DRAWING,
this.mapToElementsContent(element, ContentElementType.DRAWING) as DrawingElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.EXTERNAL_TOOL:
elements.push(
new ExternalToolElementResponseDto(
element.id,
ContentElementType.EXTERNAL_TOOL,
this.mapToElementsContent(element, ContentElementType.EXTERNAL_TOOL) as ExternalToolElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.FILE:
elements.push(
new FileElementResponseDto(
element.id,
ContentElementType.FILE,
this.mapToElementsContent(element, ContentElementType.FILE) as FileElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.LINK:
elements.push(
new LinkElementResponseDto(
element.id,
ContentElementType.LINK,
this.mapToElementsContent(element, ContentElementType.LINK) as LinkElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
case ContentElementType.RICH_TEXT:
elements.push(
new RichTextElementResponseDto(
element.id,
ContentElementType.RICH_TEXT,
this.mapToElementsContent(element, ContentElementType.RICH_TEXT) as RichTextElementContentDto,
this.mapToTimestampDto(element.timestamps)
)
);
break;
default:
break;
}
});
return elements;
}

private static mapToElementsContent(
response: CardResponseElementsInner,
elementType: ContentElementType
): CardContentElementInner {
switch (elementType) {
case ContentElementType.COLLABORATIVE_TEXT_EDITOR:
return {};
case ContentElementType.DELETED: {
const deletedElementContent: DeletedElementResponse = response as DeletedElementResponse;
return new DeletedElementContentDto(
deletedElementContent.content.title,
ContentElementType.DELETED,
deletedElementContent.content.description
);
}
case ContentElementType.SUBMISSION_CONTAINER: {
const submissionContainerResponse: SubmissionContainerElementResponse =
response as SubmissionContainerElementResponse;
return new SubmissionContainerElementContentDto(submissionContainerResponse.content.dueDate);
}
case ContentElementType.DRAWING: {
const drawingResponse: DrawingElementResponse = response as DrawingElementResponse;
return new DrawingElementContentDto(drawingResponse.content.description);
}
case ContentElementType.EXTERNAL_TOOL: {
const externalToolResponse: ExternalToolElementResponse = response as ExternalToolElementResponse;
return new ExternalToolElementContentDto(externalToolResponse.content.contextExternalToolId);
}
case ContentElementType.FILE: {
const fileResponse: FileElementResponse = response as FileElementResponse;
return new FileElementContentDto(fileResponse.content.caption, fileResponse.content.alternativeText);
}
case ContentElementType.LINK: {
const linkElementResponse: LinkElementResponse = response as LinkElementResponse;
return new LinkElementContentDto(
linkElementResponse.content.url,
linkElementResponse.content.title,
linkElementResponse.content.description!,
linkElementResponse.content.imageUrl as string
);
}
case ContentElementType.RICH_TEXT: {
const richTextResponse: RichTextElementResponse = response as RichTextElementResponse;
return new RichTextElementContentDto(richTextResponse.content.text, richTextResponse.content.inputFormat);
}
default:
break;
}
return {};
}

private static mapToVisibilitySettingsDto(
visibilitySettings: VisibilitySettingsResponse
): VisibilitySettingsResponseDto {
return new VisibilitySettingsResponseDto(visibilitySettings.publishedAt as string);
}

private static mapToTimestampDto(timestamp: TimestampsResponse): TimestampResponseDto {
return new TimestampResponseDto(timestamp.lastUpdatedAt, timestamp.createdAt, timestamp.deletedAt!);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DeletedElementContentDto } from '../dto/deleted-element-content.dto';
import { DrawingElementContentDto } from '../dto/drawing-element-content.dto';
import { ExternalToolElementContentDto } from '../dto/external-tool-element-content.dto';
import { FileElementContentDto } from '../dto/file-element-content.dto';
import { LinkElementContentDto } from '../dto/link-element-content.dto';
import { RichTextElementContentDto } from '../dto/rich-text-element-content.dto';
import { SubmissionContainerElementContentDto } from '../dto/submission-container-element-content.dto';

export type CardContentElementInner =
| LinkElementContentDto
| DeletedElementContentDto
| DrawingElementContentDto
| ExternalToolElementContentDto
| FileElementContentDto
| RichTextElementContentDto
| SubmissionContainerElementContentDto
| object;

0 comments on commit 5b88b9d

Please sign in to comment.