Skip to content

Commit

Permalink
feat: store author in documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed May 27, 2022
1 parent 309ca4e commit 2b8c14e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
18 changes: 14 additions & 4 deletions backend/src/posts/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ describe('Controller', () => {
{
message: 'test',
date: Timestamp.fromMillis(100000),
author: '1234',
},
];
jest
.spyOn(s, 'findAll')
.mockImplementation(() => Promise.resolve(result));

expect(await c.findAll()).toBe(result);
expect(await c.findAll({ user: { user_id: '1234' } })).toBe(result);
});
});
describe('create', () => {
it('should return a new post', async () => {
const post = {
message: 'test',
date: Timestamp.fromMillis(100000),
};
jest.spyOn(s, 'create').mockImplementation(() => Promise.resolve(post));
jest.spyOn(s, 'create').mockImplementation(() =>
Promise.resolve({
...post,
date: Timestamp.fromMillis(100000),
author: '1234',
}),
);

expect(await c.create(post)).toBe(post);
expect(await c.create({ user: { user_id: '1234' } }, post)).toEqual({
...post,
date: Timestamp.fromMillis(100000),
author: '1234',
});
});
});
});
16 changes: 11 additions & 5 deletions backend/src/posts/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
Controller as BaseController,
Get,
Post,
Req,
UseGuards,
} from '@nestjs/common';
import { PostDocument } from './document';
import { PostDocument, NewPostDocument } from './document';
import { Service } from './service';
import { FirebaseAuthGuard } from '../firebase/firebase-auth.guard';

Expand All @@ -15,12 +16,17 @@ export class Controller {

@Get()
@UseGuards(FirebaseAuthGuard)
findAll(): Promise<PostDocument[]> {
return this.service.findAll();
findAll(@Req() request: any): Promise<PostDocument[]> {
const userId = request.user.user_id;
return this.service.findAll(userId);
}
@Post()
@UseGuards(FirebaseAuthGuard)
public create(@Body() post: PostDocument): Promise<PostDocument> {
return this.service.create(post);
public create(
@Req() request: any,
@Body() post: NewPostDocument,
): Promise<PostDocument> {
const userId = request.user.user_id;
return this.service.create(post, userId);
}
}
5 changes: 5 additions & 0 deletions backend/src/posts/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ export class PostDocument {

message: string;
date: Timestamp;
author: string;
}

export class NewPostDocument {
message: string;
}
11 changes: 8 additions & 3 deletions backend/src/posts/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,28 @@ export class Service {
private postsCollection: CollectionReference<PostDocument>,
) {}

async create({ message }): Promise<PostDocument> {
async create({ message }, userId): Promise<PostDocument> {
const t = dayjs(new Date()).valueOf();
const date = Timestamp.fromMillis(t);
const docRef = this.postsCollection.doc(t.toString());
await docRef.set({
message,
date,
author: userId,
});
const postDoc = await docRef.get();
const post = postDoc.data();
return post;
}

async findAll(): Promise<PostDocument[]> {
async findAll(userId): Promise<PostDocument[]> {
const snapshot = await this.postsCollection.get();
const posts: PostDocument[] = [];
snapshot.forEach((doc) => posts.push(doc.data()));
snapshot.forEach((doc) => {
if (doc.data().author === userId) {
posts.push(doc.data());
}
});
return posts;
}
}

0 comments on commit 2b8c14e

Please sign in to comment.