Skip to content

Commit

Permalink
feat: add likes for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 6, 2022
1 parent b7119d0 commit b511117
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
21 changes: 21 additions & 0 deletions backend/src/posts/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,25 @@ describe('Controller', () => {
});
});
});
describe('update', () => {
beforeEach(() => {
jest.spyOn(s, 'get').mockResolvedValue({
message: 'test',
date: 100000,
userId: '1234',
likes: ['1234'],
});
jest.spyOn(s, 'exists').mockResolvedValue(true);
s.toggleLike = jest.fn();
});
it('should add a like in the doc', async () => {
expect(await c.update({ user: { user_id: '1234' } }, '1')).toEqual({
message: 'test',
date: 100000,
userId: '1234',
userName: 'Jane Doe',
likes: ['1234'],
});
});
});
});
28 changes: 28 additions & 0 deletions backend/src/posts/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
Body,
Controller as BaseController,
Get,
Param,
ParseIntPipe,
Post,
Put,
Query,
Req,
UseGuards,
Expand Down Expand Up @@ -95,4 +97,30 @@ export class Controller {

return this.service.create(post.message, userId, userName);
}

@Put('/:id')
@UseGuards(FirebaseAuthGuard)
public async update(
@Req() request: any,
@Param('id') id: string,
): Promise<ResolvedPostDocument> {
const { user } = request;
const { user_id: userId } = user;

const exists = await this.service.exists(id);
if (!exists) {
throw new BadRequestException('Post not found');
}

await this.service.toggleLike(id, userId);

const post = await this.service.get(id);

const userName = await getDisplayNameByUserId(post.userId);

return {
...post,
userName,
};
}
}
1 change: 1 addition & 0 deletions backend/src/posts/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class PostDocument extends NewPostDocument {
date: number;
userId: string;
id?: string;
likes?: string[];
}

export class PaginatedResults {
Expand Down
39 changes: 39 additions & 0 deletions backend/src/posts/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ export class Service {
private postsCollection: CollectionReference<PostDocument>,
) {}

async exists(id: string): Promise<boolean> {
return this.postsCollection
.doc(id)
.get()
.then((postDoc) => {
return postDoc.exists;
});
}

async toggleLike(id: string, userId: string) {
const docRef = this.postsCollection.doc(id);
return docRef.get().then((postDoc) => {
const post = postDoc.data();
const likes = post.likes || [];
const likeIndex = likes.indexOf(userId);
if (likeIndex === -1) {
likes.push(userId);
} else {
likes.splice(likeIndex, 1);
}
return docRef.update({ likes });
});
}

async get(id: string): Promise<PostDocument> {
return this.postsCollection
.doc(id)
.get()
.then((postDoc) => {
const docId = postDoc.id;
const post = postDoc.data();

return {
...post,
id: docId,
};
});
}

async getMultiple(
limit: number,
startAfter?: string | undefined,
Expand Down

0 comments on commit b511117

Please sign in to comment.