Skip to content

Commit

Permalink
[#140] refactor: 반복되는 로직을 Block 메소드로 모듈화
Browse files Browse the repository at this point in the history
  • Loading branch information
skid901 committed Dec 19, 2020
1 parent 1c2bbc1 commit 1a6d0d2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
14 changes: 11 additions & 3 deletions backend/src/models/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface BlockDoc extends Document {
) => Promise<void>;
deleteChild?: (this: BlockDoc, childId: string) => Promise<void>;
deleteCascade?: (this: BlockDoc) => Promise<void>;
findIndexFromChildIdList?: (this: BlockDoc, childId: string) => number;
}

BlockSchema.statics.createOne = async function (
Expand Down Expand Up @@ -167,9 +168,7 @@ BlockSchema.methods.deleteChild = async function (
this: BlockDoc,
childId: string,
): Promise<void> {
const index = this.childIdList.findIndex(
(_childId) => _childId.toHexString() === childId,
);
const index = this.findIndexFromChildIdList(childId);
this.childIdList.splice(index, 1);
await this.save();
};
Expand All @@ -185,4 +184,13 @@ BlockSchema.methods.deleteCascade = async function (
await Block.findByIdAndDelete(this.id).exec();
};

BlockSchema.methods.findIndexFromChildIdList = function (
this: BlockDoc,
childId: string,
): number {
return this.childIdList.findIndex(
(_childId) => _childId.toHexString() === childId,
);
};

export const Block = model<BlockDoc>(MODEL_NAME, BlockSchema) as BlockModel;
8 changes: 2 additions & 6 deletions backend/src/services/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export const move = async (
}

toIndex ??= to.childIdList.length;
const fromIndex = from.childIdList.findIndex(
(_childId) => _childId.toHexString() === blockId,
);
const fromIndex = from.findIndexFromChildIdList(blockId);
toIndex = fromIndex < toIndex ? toIndex - 1 : toIndex;
await to.deleteChild(blockId);
await to.setChild(block, toIndex);
Expand All @@ -78,9 +76,7 @@ export const deleteOnly = async (blockId: string): Promise<BlockDoc> => {
}

const parent = await Block.readOne(block.parentId.toHexString());
const index = parent.childIdList.findIndex(
(childId) => childId.toHexString() === blockId,
);
const index = parent.findIndexFromChildIdList(blockId);

await parent.setChildren(block.childIdList, index);
await parent.deleteChild(blockId);
Expand Down
6 changes: 1 addition & 5 deletions backend/test/services/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ describe('@services/block', () => {

expect(received.value).toEqual(expected.value);
expect(received.type).toEqual(expected.type);
expect(
parent.childIdList.findIndex(
(childId) => childId.toHexString() === received.id,
),
).toEqual(index);
expect(parent.findIndexFromChildIdList(received.id)).toEqual(index);
});

it('create: Duplicated error', async () => {
Expand Down

0 comments on commit 1a6d0d2

Please sign in to comment.