-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Branch the DB and the usecase on the server route handler
- Loading branch information
Showing
6 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/infrastructure/repositories/TaskRepositorySql.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |