Skip to content

Commit

Permalink
#220: implemented remove of post and reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
basmasking committed May 12, 2024
1 parent 9397d19 commit 973e263
Show file tree
Hide file tree
Showing 32 changed files with 103 additions and 107 deletions.
4 changes: 2 additions & 2 deletions segments/comic.segment.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"./domain/comic/create": { "default": { "access": "protected" } },
"./domain/comic/get": { "default": { "access": "protected" } },
"./domain/comic/remove": { "default": { "access": "protected" } },
"./domain/comic/erase": { "default": { "access": "protected" } },

"./domain/comic/data/create": { "default": { "access": "private" } },
"./domain/comic/data/remove": { "default": { "access": "private" } },
"./domain/comic/data/erase": { "default": { "access": "private" } },
"./domain/comic/data/retrieve": { "default": { "access": "private" } }
}
4 changes: 2 additions & 2 deletions segments/image.segment.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"./domain/image/create": { "default": { "access": "protected" } },
"./domain/image/download": { "default": { "access": "protected" } },
"./domain/image/get": { "default": { "access": "protected" } },
"./domain/image/remove": { "default": { "access": "protected" } },
"./domain/image/erase": { "default": { "access": "protected" } },

"./domain/image/save": { "default": { "access": "private" } },
"./domain/image/data/create": { "default": { "access": "private" } },
"./domain/image/data/remove": { "default": { "access": "private" } },
"./domain/image/data/erase": { "default": { "access": "private" } },
"./domain/image/data/retrieve": { "default": { "access": "private" } }
}
4 changes: 2 additions & 2 deletions src/domain/comic/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import createImage from '../image/create';
import removeImage from '../image/remove';
import eraseImage from '../image/erase';
import type ComicData from './data/ComicData';
import createData from './data/create';
import { IMAGE_TYPE } from './definitions/constants.js';
Expand All @@ -20,7 +20,7 @@ export default async function create(imageDataUrl: string, structure?: string):
{
if (image !== undefined)
{
await removeImage(image.id);
await eraseImage(image.id);
}

throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';

export default async function remove(id: string): Promise<void>
export default async function erase(id: string): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
}
12 changes: 12 additions & 0 deletions src/domain/comic/erase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

import eraseImage from '../image/erase';
import eraseData from './data/erase';
import retrieveData from './data/retrieve';

export default async function erase(id: string): Promise<void>
{
const data = await retrieveData(id);

await eraseData(data.id);
await eraseImage(data.imageId);
}
12 changes: 0 additions & 12 deletions src/domain/comic/remove.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';

export default async function remove(id: string): Promise<void>
export default async function erase(id: string): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
}
7 changes: 7 additions & 0 deletions src/domain/comment/erase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import eraseData from './data/erase';

export default async function erase(id: string): Promise<void>
{
return eraseData(id);
}
7 changes: 0 additions & 7 deletions src/domain/comment/remove.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';

export default async function remove(id: string): Promise<void>
export default async function erase(id: string): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
}
10 changes: 10 additions & 0 deletions src/domain/image/erase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import eraseData from './data/erase';
import retrieveData from './data/retrieve';

export default async function erase(id: string): Promise<void>
{
const data = await retrieveData(id);

return eraseData(data.id);
}
10 changes: 0 additions & 10 deletions src/domain/image/remove.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/domain/post/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import type Requester from '../authentication/Requester';
import createComic from '../comic/create';
import removeComic from '../comic/remove';
import eraseComic from '../comic/erase';
import increaseCreatorPostCount from '../creator/increasePostCount';
import createPost from './data/create';
import removePost from './data/remove';
import erasePost from './data/erase';

export default async function create(requester: Requester, comicImageDataUrl: string): Promise<void>
{
Expand All @@ -19,8 +19,8 @@ export default async function create(requester: Requester, comicImageDataUrl: st
}
catch (error: unknown)
{
const undoComic = comic !== undefined ? removeComic(comic.id) : Promise.resolve();
const undoPost = post !== undefined ? removePost(post.id) : Promise.resolve();
const undoComic = comic !== undefined ? eraseComic(comic.id) : Promise.resolve();
const undoPost = post !== undefined ? erasePost(post.id) : Promise.resolve();

await Promise.all([undoComic, undoPost]);

Expand Down
16 changes: 2 additions & 14 deletions src/domain/post/data/PostData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export default class PostData
#ratingCount: number;
#reactionCount: number;

#deleted: boolean;

constructor(id: string, creatorId: string, comicId: string, createdAt = new Date(), ratingCount = 0, reactionCount = 0, deleted = false)
constructor(id: string, creatorId: string, comicId: string, createdAt = new Date(), ratingCount = 0, reactionCount = 0)
{
this.#id = id;
this.#creatorId = creatorId;
Expand All @@ -22,8 +20,6 @@ export default class PostData

this.#ratingCount = ratingCount;
this.#reactionCount = reactionCount;

this.#deleted = deleted;
}

get id() { return this.#id; }
Expand All @@ -38,8 +34,6 @@ export default class PostData

get reactionCount() { return this.#reactionCount; }

get deleted() { return this.#deleted; }

increaseRatingCount(): PostData
{
return this.#mutate({ ratingCount: this.#ratingCount + 1 });
Expand All @@ -60,11 +54,6 @@ export default class PostData
return this.#mutate({ reactionCount: this.#reactionCount - 1 });
}

delete(): PostData
{
return this.#mutate({ deleted: true });
}

#mutate(values: Partial<PostData>)
{
return new PostData(
Expand All @@ -73,8 +62,7 @@ export default class PostData
values.comicId ?? this.#comicId,
values.createdAt ?? this.#createdAt,
values.ratingCount ?? this.#ratingCount,
values.reactionCount ?? this.#reactionCount,
values.deleted ?? this.#deleted
values.reactionCount ?? this.#reactionCount
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';

export default async function remove(id: string): Promise<void>
export default async function erase(id: string): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
}
3 changes: 1 addition & 2 deletions src/domain/post/data/mapRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default function mapRecord(record: RecordData): PostData
record.comicId as string,
new Date(record.createdAt as string),
record.ratingCount as number,
record.reactionCount as number,
record.deleted as boolean
record.reactionCount as number
);
}
7 changes: 5 additions & 2 deletions src/domain/post/data/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';
import type PostData from './PostData';

export default async function remove(id: string): Promise<void>
export default async function remove(post: PostData): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
const updatedData = { ...post, deleted: true };

return database.updateRecord(RECORD_TYPE, post.id, updatedData);
}
3 changes: 1 addition & 2 deletions src/domain/post/data/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export default async function update(data: PostData): Promise<void>
comicId: data.comicId,
createdAt: data.createdAt,
ratingCount: data.ratingCount,
reactionCount: data.reactionCount,
deleted: data.deleted
reactionCount: data.reactionCount
};

await database.updateRecord(RECORD_TYPE, data.id, record);
Expand Down
11 changes: 5 additions & 6 deletions src/domain/post/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
import type Requester from '../authentication/Requester';
import decreasePostCount from '../creator/decreasePostCount';
import increasePostCount from '../creator/increasePostCount';
import removeData from './data/remove';
import retrieveOwn from './data/retrieveOwn';
import updateData from './data/update';

export default async function remove(requester: Requester, id: string): Promise<void>
{
// We only delete the post itself and do not cascade it towards the reactions as it doesn't add
// any value, and it would make the code more complex.

const currentData = await retrieveOwn(id, requester.id);
const updatedData = currentData.delete();
const post = await retrieveOwn(id, requester.id);

let creatorCount;

try
{
creatorCount = await decreasePostCount(currentData.creatorId);
creatorCount = await decreasePostCount(post.creatorId);

await updateData(updatedData);
await removeData(post);
}
catch (error: unknown)
{
if (creatorCount !== undefined)
{
await increasePostCount(currentData.creatorId);
await increasePostCount(post.creatorId);
}

throw error;
Expand Down
9 changes: 9 additions & 0 deletions src/domain/rating/data/erase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import database from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';

export default async function erase(id: string): Promise<void>
{
return database.deleteRecord(RECORD_TYPE, id);
}
4 changes: 2 additions & 2 deletions src/domain/rating/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import type Requester from '../authentication/Requester';
import type RatingData from './data/RatingData';
import create from './data/create';
import erase from './data/erase';
import find from './data/find';
import remove from './data/remove';

export default async function update(requester: Requester, postId: string | undefined, reactionId: string | undefined): Promise<RatingData | void>
{
const data = await find(requester.id, postId, reactionId);

return data === undefined
? create(requester.id, postId, reactionId)
: remove(data.id);
: erase(data.id);
}
8 changes: 4 additions & 4 deletions src/domain/reaction/createComic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import type Requester from '../authentication/Requester';
import createActualComic from '../comic/create';
import removeActualComic from '../comic/remove';
import eraseActualComic from '../comic/erase';
import increaseReactionCount from '../post/increaseReactionCount';
import createReaction from './data/create';
import removeReaction from './data/remove';
import eraseReaction from './data/erase';
import type ReactionView from './view/ReactionView';
import createView from './view/createView';

Expand All @@ -22,8 +22,8 @@ export default async function createComic(requester: Requester, postId: string,
}
catch (error: unknown)
{
const undoComment = comic !== undefined ? removeActualComic(comic.id) : Promise.resolve();
const undoReaction = reaction !== undefined ? removeReaction(reaction.id) : Promise.resolve();
const undoComment = comic !== undefined ? eraseActualComic(comic.id) : Promise.resolve();
const undoReaction = reaction !== undefined ? eraseReaction(reaction.id) : Promise.resolve();

await Promise.all([undoComment, undoReaction]);

Expand Down
8 changes: 4 additions & 4 deletions src/domain/reaction/createComment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import type Requester from '../authentication/Requester';
import createActualComment from '../comment/create';
import removeActualComment from '../comment/remove';
import eraseActualComment from '../comment/erase';
import increaseReactionCount from '../post/increaseReactionCount';
import createReaction from './data/create';
import removeReaction from './data/remove';
import eraseReaction from './data/erase';
import type ReactionView from './view/ReactionView';
import createView from './view/createView';

Expand All @@ -22,8 +22,8 @@ export default async function createComment(requester: Requester, postId: string
}
catch (error: unknown)
{
const undoComment = comment !== undefined ? removeActualComment(comment.id) : Promise.resolve();
const undoReaction = reaction !== undefined ? removeReaction(reaction.id) : Promise.resolve();
const undoComment = comment !== undefined ? eraseActualComment(comment.id) : Promise.resolve();
const undoReaction = reaction !== undefined ? eraseReaction(reaction.id) : Promise.resolve();

await Promise.all([undoComment, undoReaction]);

Expand Down
Loading

0 comments on commit 973e263

Please sign in to comment.