Skip to content

Commit

Permalink
Add enh (#3)
Browse files Browse the repository at this point in the history
* chore: remove unnused classes

* chore: added types
  • Loading branch information
pana1990 authored Nov 16, 2020
1 parent 5680369 commit bdaede5
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@nuxtjs/pwa": "^3.0.2",
"core-js": "^3.6.5",
"nuxt": "^2.14.6",
"nuxt-property-decorator": "^2.8.8"
"nuxt-property-decorator": "^2.8.8",
"uuid": "^8.3.1"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
Expand Down
4 changes: 2 additions & 2 deletions pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div class="container flex items-center justify-center m-auto mt-12">
<div>
<header class="">
<Logo class="" />
<header>
<Logo />
<h1 class="text-2xl text-gray-900 font-extrabold mt-2">
nuxt-hexagonal-architecture-playground
</h1>
Expand Down
1 change: 1 addition & 0 deletions pages/todo/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<button
class="flex-shrink-0 bg-teal-500 hover:bg-teal-700 border-teal-500 hover:border-teal-700 text-sm border-4 text-white py-1 px-2 rounded"
type="button"
tabindex="-1"
@click="addTask"
>
Crear
Expand Down
1 change: 1 addition & 0 deletions src/shared/domain/error/DomainError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class DomainError extends Error {}
25 changes: 25 additions & 0 deletions src/shared/domain/uuid/Uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import DomainError from '@/src/shared/domain/error/DomainError';
// @ts-ignore
import { v4 } from 'uuid';

class UuidIsInvalid extends DomainError {
constructor(value: string) {
super(`Uuid ${value} is invalid`);
}
}

export default abstract class Uuid {
readonly value: string;

protected constructor(private value: string) {
Uuid.ensureIsValidUuid(value);

this.value = value;
}

private static ensureIsValidUuid(value: string) {
if (!v4().validate(value)) {
throw new UuidIsInvalid(value);
}
}
}
3 changes: 3 additions & 0 deletions src/shared/domain/uuid/UuidGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface UuidGenerator {
generate(): string;
}
13 changes: 10 additions & 3 deletions src/shared/infrastructure/container/Container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'reflect-metadata';
import UuidGenerator from '@/src/shared/domain/uuid/UuidGenerator';
import { SYMBOLS } from '@/src/shared/infrastructure/container/Types';
import V4UuidGenerator from '@/src/shared/infrastructure/uuid/V4UuidGenerator';
import { Create } from '@/src/todo/application/create/Create';
import { List } from '@/src/todo/application/list/List';
import TodoRepository from '@/src/todo/domain/TodoRepository';
Expand All @@ -10,11 +12,16 @@ import getDecorators from 'inversify-inject-decorators';
export const container = new Container();

container
.bind<TodoRepository>(SYMBOLS.TodoRepository)
.bind<TodoRepository>(SYMBOLS.TODO_REPOSITORY)
.to(InMemoryTodoRepository)
.inSingletonScope();

container.bind<Create>(SYMBOLS.TodoCreate).to(Create).inSingletonScope();
container.bind<List>(SYMBOLS.TodoList).to(List).inSingletonScope();
container.bind<Create>(SYMBOLS.TODO_CREATE).to(Create).inSingletonScope();
container.bind<List>(SYMBOLS.TODO_LIST).to(List).inSingletonScope();

container
.bind<UuidGenerator>(SYMBOLS.UUID_GENERATOR)
.to(V4UuidGenerator)
.inSingletonScope();

export const { lazyInject } = getDecorators(container);
7 changes: 4 additions & 3 deletions src/shared/infrastructure/container/Types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const SYMBOLS = {
TodoRepository: Symbol('TodoRepository'),
TodoCreate: Symbol('TodoCreate'),
TodoList: Symbol('TodoList'),
TODO_REPOSITORY: Symbol('TODO_REPOSITORY'),
TODO_CREATE: Symbol('TODO_CREATE'),
TODO_LIST: Symbol('TODO_LIST'),
STORE: Symbol('Store'),
UUID_GENERATOR: Symbol('UUID_GENERATOR'),
};
10 changes: 10 additions & 0 deletions src/shared/infrastructure/uuid/V4UuidGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import UuidGenerator from '@/src/shared/domain/uuid/UuidGenerator';
import { injectable } from 'inversify';
import { v4 } from 'uuid';

@injectable()
export default class V4UuidGenerator implements UuidGenerator {
generate(): string {
return v4();
}
}
16 changes: 13 additions & 3 deletions src/todo/application/create/Create.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import CommandHandler from '@/src/shared/domain/use-case/command/CommandHandler';
import UuidGenerator from '@/src/shared/domain/uuid/UuidGenerator';
import { SYMBOLS } from '@/src/shared/infrastructure/container/Types';
import CreateCommand from '@/src/todo/application/create/CreateCommand';
import Todo from '@/src/todo/domain/Todo';
import TodoBody from '@/src/todo/domain/TodoBody';
import TodoId from '@/src/todo/domain/TodoId';
import TodoRepository from '@/src/todo/domain/TodoRepository';
import { inject, injectable } from 'inversify';

@injectable()
export class Create implements CommandHandler {
constructor(
@inject(SYMBOLS.TodoRepository)
private readonly todoRepository: TodoRepository
@inject(SYMBOLS.TODO_REPOSITORY)
private readonly todoRepository: TodoRepository,
@inject(SYMBOLS.UUID_GENERATOR)
private readonly uuidGenerator: UuidGenerator
) {}

dispatch(command: CreateCommand): Promise<void> {
return this.todoRepository.add(command.todo);
const todoId = new TodoId(this.uuidGenerator.generate());
const todoBody = new TodoBody(command.todoBody);
const todo = Todo.create(todoId, todoBody);

return this.todoRepository.add(todo);
}
}
2 changes: 1 addition & 1 deletion src/todo/application/create/CreateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Command from '@/src/shared/domain/use-case/command/Command';

export default class CreateCommand implements Command {
constructor(readonly todo: string) {}
constructor(readonly todoBody: string) {}
}
12 changes: 10 additions & 2 deletions src/todo/application/list/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import Query from '@/src/shared/domain/use-case/query/Query';
import QueryHandler from '@/src/shared/domain/use-case/query/QueryHandler';
import { SYMBOLS } from '@/src/shared/infrastructure/container/Types';
import ListResponse from '@/src/todo/application/list/ListResponse';
import Todo from '@/src/todo/domain/Todo';
import TodoRepository from '@/src/todo/domain/TodoRepository';
import { inject, injectable } from 'inversify';

@injectable()
export class List implements QueryHandler {
constructor(
@inject(SYMBOLS.TodoRepository)
@inject(SYMBOLS.TODO_REPOSITORY)
private readonly todoRepository: TodoRepository
) {}

ask(query?: Query): ListResponse {
const todos = this.todoRepository.getAll();
return new ListResponse(todos);

return new ListResponse(todos.map(List.mappingTodosToArray()));
}

private static mappingTodosToArray() {
return (todo: Todo): string => {
return todo.body.value;
};
}
}
23 changes: 23 additions & 0 deletions src/todo/domain/Todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import TodoId from '@/src/todo/domain/TodoId';
import TodoBody from '@/src/todo/domain/TodoBody';
import TodoCreatedAt from '@/src/todo/domain/TodoCreatedAt';

export default class Todo {
constructor(
private readonly _id: TodoId,
private readonly _body: TodoBody,
private readonly _createdAt: TodoCreatedAt
) {}

get body(): TodoBody {
return this._body;
}

get createdAt(): TodoCreatedAt {
return this._createdAt;
}

static create(todoId: TodoId, todoBody: TodoBody) {
return new Todo(todoId, todoBody, Date.now());
}
}
7 changes: 7 additions & 0 deletions src/todo/domain/TodoBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class TodoBody {
constructor(private readonly _value: string) {}

get value(): string {
return this._value;
}
}
3 changes: 3 additions & 0 deletions src/todo/domain/TodoCreatedAt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class TodoCreatedAt {
constructor(value: Date) {}
}
7 changes: 7 additions & 0 deletions src/todo/domain/TodoId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class TodoId {
constructor(private readonly _value: string) {}

get value(): string {
return this._value;
}
}
6 changes: 4 additions & 2 deletions src/todo/domain/TodoRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Todo from '@/src/todo/domain/Todo';

export default interface TodoRepository {
getAll(): string[];
add(task: string): Promise<void>;
getAll(): Todo[];
add(todo: Todo): Promise<void>;
}
9 changes: 5 additions & 4 deletions src/todo/infrastructure/InMemoryTodoRepository.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Todo from '@/src/todo/domain/Todo';
import { injectable } from 'inversify';
import TodoRepository from '@/src/todo/domain/TodoRepository';

@injectable()
export class InMemoryTodoRepository implements TodoRepository {
private static _todos: string[] = [];
private static _todos: Todo[] = [];

add(task: string): Promise<void> {
InMemoryTodoRepository._todos.push(task);
add(todo: Todo): Promise<void> {
InMemoryTodoRepository._todos.push(todo);

return Promise.resolve();
}

getAll(): string[] {
getAll(): Todo[] {
return InMemoryTodoRepository._todos;
}
}
4 changes: 2 additions & 2 deletions store/TodoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export default class TodoStore extends VuexModule {

@Mutation
updateTodos() {
const listResponse = container.get<List>(SYMBOLS.TodoList).ask();
const listResponse = container.get<List>(SYMBOLS.TODO_LIST).ask();
this._todos = listResponse.todoList;
}

@Action
async addTodo(todo: string) {
const createCommand = new CreateCommand(todo);
await container.get<Create>(SYMBOLS.TodoCreate).dispatch(createCommand);
await container.get<Create>(SYMBOLS.TODO_CREATE).dispatch(createCommand);
this.updateTodos();
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12140,7 +12140,7 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.0:
uuid@^8.3.0, uuid@^8.3.1:
version "8.3.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.1.tgz#2ba2e6ca000da60fce5a196954ab241131e05a31"
integrity sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==
Expand Down

0 comments on commit bdaede5

Please sign in to comment.