Skip to content

Commit

Permalink
Branch the DB and the usecase on the server route handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jbuget committed Nov 22, 2021
1 parent aa2145c commit 0d3bb2b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/domain/entities/TaskRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TaskList } from './TaskList';

export interface TaskRepository {
findAll(): TaskList
findAll(): Promise<TaskList>
}
4 changes: 2 additions & 2 deletions src/domain/usecases/queries/list_tasks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TaskRepository } from '../../entities/TaskRepository';
import { TaskList } from '../../entities/TaskList';

function listTasks(taskRepository: TaskRepository): TaskList {
return taskRepository.findAll();
async function listTasks(taskRepository: TaskRepository): Promise<TaskList> {
return await taskRepository.findAll();
}

export {
Expand Down
13 changes: 5 additions & 8 deletions src/infrastructure/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fastify, { FastifyInstance } from 'fastify';
import { P } from 'pino';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import { listTasks } from '../domain/usecases/queries/list_tasks';
import { TaskRepositorySql } from './repositories/TaskRepositorySql';

function build(logger?: P.Logger): FastifyInstance {
const server = fastify({
Expand All @@ -15,11 +14,9 @@ function build(logger?: P.Logger): FastifyInstance {

// Get all tasks
server.get('/tasks', async () => {
try {
return await prisma.task.findMany();
} finally {
await prisma.$disconnect();
}
const taskRepository = new TaskRepositorySql();
const taskList = await listTasks(taskRepository);
return taskList.tasks;
});

// Crate a new task
Expand Down
25 changes: 25 additions & 0 deletions src/infrastructure/repositories/TaskRepositorySql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PrismaClient } from '@prisma/client';
import { TaskRepository } from '../../domain/entities/TaskRepository';
import { TaskList } from '../../domain/entities/TaskList';
import { Status, Task } from '../../domain/entities/Task';

export class TaskRepositorySql implements TaskRepository {
async findAll(): Promise<TaskList> {
const prisma = new PrismaClient();

let domainTasks: Task[];
try {
const prismaTasks = await prisma.task.findMany();
domainTasks = prismaTasks.map(task => new Task({
id: task.id,
content: task.content,
createdAt: task.createdAt,
updatedAt: task.updatedAt,
status: task.status as Status
}));
} finally {
await prisma.$disconnect();
}
return new TaskList(domainTasks);
}
}
6 changes: 3 additions & 3 deletions test/domain/usecases/queries/list_tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import { TaskRepository } from '../../../../src/domain/entities/TaskRepository';
import { TaskList } from '../../../../src/domain/entities/TaskList';

describe('domain.usecases.queries.list_tasks', function () {
it('should have a method #listTasks', () => {
it('should have a method #listTasks', async () => {
// given
const now = new Date('2021-11-15T20:01:45.264Z');
const task1: Task = new Task({ id: 1, createdAt: now, content: 'Task_content_1' });
const task2: Task = new Task({ id: 2, createdAt: now, content: 'Task_content_2' });
const taskList: TaskList = new TaskList([task1, task2]);

const taskRepository: TaskRepository = {
findAll(): TaskList {
async findAll(): Promise<TaskList> {
return taskList;
}
};

// when
const tasks: TaskList = listTasks(taskRepository);
const tasks: TaskList = await listTasks(taskRepository);

// then
expect(tasks).toStrictEqual(taskList);
Expand Down
41 changes: 41 additions & 0 deletions test/infrastructure/repositories/TaskRepositorySql.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TaskList } from '../../../src/domain/entities/TaskList';
import { TaskRepositorySql } from '../../../src/infrastructure/repositories/TaskRepositorySql';
import { Status, Task } from '../../../src/domain/entities/Task';

describe('infrastructure.repositories.TaskRepositorySql', function () {
describe('#findAll', () => {
it('should return an instance of TaskList with tasks data', async () => {
// given
const taskRepository = new TaskRepositorySql();

// when
const tasks: TaskList = await taskRepository.findAll();

// then
const task1 = new Task({
id: 1,
content: 'Task 1 content',
createdAt: new Date('2021-11-09T17:08:02.865Z'),
updatedAt: new Date('2021-11-09T17:08:02.866Z'),
status: Status.DONE
});
const task2 = new Task({
id: 2,
content: 'Task 2 content',
createdAt: new Date('2021-11-09T17:08:02.927Z'),
updatedAt: new Date('2021-11-09T17:08:02.928Z'),
status: Status.TO_DO
});
const task3 = new Task({
id: 3,
content: 'Task 3 content',
createdAt: new Date('2021-11-09T17:08:02.969Z'),
updatedAt: new Date('2021-11-09T17:08:02.970Z'),
status: Status.TO_DO
});

const expectedTasks = new TaskList([task1, task2, task3]);
expect(tasks).toStrictEqual(expectedTasks);
});
});
});

0 comments on commit 0d3bb2b

Please sign in to comment.