Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactor create #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Repository for studying architeture model and the [Deno](https://deno.land/) runtime.

## Requisitos
## Requirements
- [Docker](https://docs.docker.com/install/)
- [Docker Compose](https://docs.docker.com/compose/install/)

Expand Down
8 changes: 8 additions & 0 deletions app/Interactors/Tasks/Create/Create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Input from './Input.ts'

interface Create
{
createTask(input: Input): Promise<boolean>
}

export default Create
7 changes: 7 additions & 0 deletions app/Interactors/Tasks/Create/Input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Input
{
title: string
description: string
}

export default Input
39 changes: 39 additions & 0 deletions app/Interactors/Tasks/Create/UseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Input from './Input.ts'
import Create from './Create.ts'
import Task from './../../../Entities/Task.ts'
import Repository from './../Repository.ts'
import DataAccess from './../DataAccess.ts'

export default class implements Create
{
private repository: Repository

public constructor(repository: Repository)
{
this.repository = repository
}

public createTask(input: Input): Promise<boolean>
{
const task = this.buildTask(input)
const dataAccess = this.convertTaskToDataAccess(task)

return this.repository.save(dataAccess)
}

private buildTask(input: Input): Task
{
const task = new Task(input.title)
task.setDescription(input.description)
return task
}

private convertTaskToDataAccess(task: Task): DataAccess
{
return {
'title': task.getTitle(),
'description': task.getDescription(),
'createdAt': task.getCreatedAt(),
}
}
}
8 changes: 8 additions & 0 deletions app/Interactors/Tasks/DataAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface DataAccess
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O que te motivou a dar esse nome para essa interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foi a tradução que usei para "Dados de Acesso", mas agora que você questionou, se formos tentar usar a arquitetura limpa talvez eu tenha que nomear a classe que chamei de Repository para DataAccess e dá outro nome para essa ou extinguir e trabalhar diretamente com entidades, o que acha?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eu gosto da nomenclatura Repository, mas creio que o nome não fará diferença desde que seja semântico e sigamos um padrão

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Também acho bom, mas não considera que DataAccess é ainda mais abstrato?

{
title: String
description: String
createdAt: Date
}

export default DataAccess
8 changes: 8 additions & 0 deletions app/Interactors/Tasks/Repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import DataAccess from './DataAccess.ts'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eu colocaria essa classe fora do diretório Interactors, em: app/Repositories/Tasks/Task.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Você fala do DataAccess ou do Repository? Optei por deixar ambas nesse local para que o diretório simule a camada de Casos de Uso. Nesse caso apenas Repository, que é uma interface, ficaria nessa pasta e as classes que a implementam ficariam no diretório que sugeriu. Já a DataAccess deixei também aqui pois o Caso de Uso depende diretamente dela, assim nada que o caso de uso dependa estará fora da pasta.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Estava falando do Repository. Você tem razão, acho que eu não tinha me atentado para o fato de que ela é uma interface


interface Repository
{
save(data: DataAccess): Promise<boolean>
}

export default Repository
11 changes: 11 additions & 0 deletions app/Views/error404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error 404</title>
</head>
<body>
<p>Error 404</p>
</body>
</html>
16 changes: 16 additions & 0 deletions app/Views/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tasks</title>
</head>
<body>
<h1>Tasks</h1>
<h2>Create</h2>
<form action="." method="post">
<input type="text" name="title"/>
<button type="submit">Create</button>
</form>
</body>
</html>
28 changes: 23 additions & 5 deletions app/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { serve } from 'https://deno.land/std/http/server.ts'
import { serve, ServerRequest } from 'https://deno.land/std/http/server.ts'
import { readFileStr } from "https://deno.land/std/fs/mod.ts";

const server = serve({port: 80})
console.log('Server start')

for await (const request of server)
{
readFileStr('./Views/index.html').then((html: string) => {
request.respond({
body: html
})
let fileRead: Promise<string>

switch(request.url){
case '/':
fileRead = readFileStr('./Views/index.html')
break
case '/create/':
fileRead = readFileStr('./Views/form.html')
break
default:
fileRead = readFileStr('./Views/error404.html')
}

fileRead.then((fileContent: string) => {
response(request, fileContent)
})
}

function response(request: ServerRequest, body: string): void
{
request.respond({
body: body
})
}