Skip to content

Commit

Permalink
feat: define List model
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Jul 18, 2024
1 parent 7858287 commit b6152b5
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
72 changes: 72 additions & 0 deletions pkg/timeline/model/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it } from 'vitest';

import type { AccountID } from '../../accounts/model/account.js';
import { type CreateListArgs, List, type ListID } from './list.js';

describe('List', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;

it('should create a new list', () => {
const list = List.new(args);

expect(list.getId()).toBe(args.id);
expect(list.getTitle()).toBe(args.title);
expect(list.isPublic()).toBe(true);
expect(list.getOwnerId()).toBe(args.ownerId);
expect(list.getMemberIds()).toEqual(args.memberIds);
expect(list.getCreatedAt()).toBe(args.createdAt);
});

it('should add a member to the list', () => {
const list = List.new(args);
const memberId = '4' as AccountID;

list.addMember(memberId);

expect(list.getMemberIds()).toStrictEqual([
'3' as AccountID,
'4' as AccountID,
]);
});

it('should not add a member if already in the list', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;
const list3 = List.new(args);
const memberId = '3' as AccountID;

list3.addMember(memberId);

expect(list3.getMemberIds()).toStrictEqual(['3' as AccountID]);
});

it('should remove member from list', () => {
const args: CreateListArgs = {
id: '1' as ListID,
title: 'My List',
publicity: 'PUBLIC',
ownerId: '2' as AccountID,
memberIds: ['3' as AccountID],
createdAt: new Date(),
} as const;
const list3 = List.new(args);
const memberId = '3' as AccountID;

list3.removeMember(memberId);

expect(list3.getMemberIds()).toStrictEqual([]);
});
});
67 changes: 67 additions & 0 deletions pkg/timeline/model/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { AccountID } from '../../accounts/model/account.js';
import type { ID } from '../../id/type.js';

export type ListID = ID<List>;
export interface CreateListArgs {
id: ListID;
title: string;
publicity: 'PUBLIC' | 'PRIVATE';
ownerId: AccountID;
memberIds: AccountID[];
createdAt: Date;
}
export class List {
private readonly id: ListID;
private readonly title: string;
private readonly publicity: 'PUBLIC' | 'PRIVATE';
private readonly ownerId: AccountID;
private memberIds: AccountID[];
private readonly createdAt: Date;

private constructor(args: CreateListArgs) {
this.id = args.id;
this.title = args.title;
this.publicity = args.publicity;
this.ownerId = args.ownerId;
this.memberIds = args.memberIds;
this.createdAt = args.createdAt;
}

static new(args: CreateListArgs) {
return new List(args);
}

getId(): ListID {
return this.id;
}

getTitle(): string {
return this.title;
}

isPublic(): boolean {
return this.publicity === 'PUBLIC';
}

getOwnerId(): AccountID {
return this.ownerId;
}

getMemberIds(): AccountID[] {
return this.memberIds;
}

getCreatedAt(): Date {
return this.createdAt;
}

addMember(memberId: AccountID) {
// ToDo: member limit
if (this.memberIds.includes(memberId)) return;
this.memberIds.push(memberId);
}

removeMember(memberId: AccountID) {
this.memberIds = this.memberIds.filter((id) => id !== memberId);
}
}

0 comments on commit b6152b5

Please sign in to comment.