Skip to content
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
12,216 changes: 11,157 additions & 1,059 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "1.0.0",
"scripts": {
"build-ts": "tsc",
"build-ts": "tsc -p tsconfig.prod.json",
"build": "npm run build-ts && npm run lint",
"dev": "cross-env NODE_ENV=dev ts-node-dev --ignore-watch node_modules --inspect=0.0.0.0:9267 ./src/api/server.ts",
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
Expand All @@ -30,16 +30,16 @@
"@types/lusca": "^1.6.1",
"@types/node": "^13.13.4",
"@types/supertest": "^2.0.9",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
"cross-env": "^7.0.2",
"errorhandler": "^1.5.1",
"eslint": "^6.8.0",
"eslint": "^8.10.0",
"jest": "^26.0.0",
"nodemon": "^2.0.3",
"supertest": "^4.0.2",
"ts-jest": "^25.4.0",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.8.3"
"typescript": "^3.9.10"
}
}
4 changes: 4 additions & 0 deletions src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import lusca from "lusca";

// Controllers (route handlers)
import * as healthController from "./controllers/health";
import { registerGenialyRouter } from "./genially/genially.router";

// Create Express server
const app = express();
Expand All @@ -20,4 +21,7 @@ app.use(lusca.xssProtection(true));
// Primary app routes
app.get("/", healthController.check);

//routes of Genially
app.use("/api", registerGenialyRouter());

export default app;
16 changes: 16 additions & 0 deletions src/api/genially/controllers/DeleteGeniallyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Request, Response } from "express";
import DeleteGeniallyService from "../../../contexts/core/genially/application/DeleteGeniallyService";

export class DeleteGeniallyController {
constructor(private deleteService: DeleteGeniallyService) {}

public async execute(req: Request, res: Response) {
try {
const { id } = req.params;
await this.deleteService.execute(id);
res.status(200).send("deleted successful");
} catch (error) {
res.status(500).send({ error: error.message });
}
}
}
24 changes: 24 additions & 0 deletions src/api/genially/controllers/GeniallyPostController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Request, Response } from "express";
import CreateGeniallyService from "../../../contexts/core/genially/application/CreateGeniallyService";

export class GeniallyPostController {
constructor(private createGeniallyService: CreateGeniallyService) {}

public async execute(req: Request, res: Response) {
try {
const { id } = req.params;
const { name, description } = req.body;
const genially = await this.createGeniallyService.execute({
id,
name,
description,
});

res
.status(201)
.json({ id, name, description, createAt: genially.createdAt });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}
17 changes: 17 additions & 0 deletions src/api/genially/controllers/RenameGeniallyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Request, Response } from "express";
import RenameGeniallyService from "../../../contexts/core/genially/application/RenameGeniallyService";

export class RenameGeniallyController {
constructor(private renameService: RenameGeniallyService) {}

public async execute(req: Request, res: Response) {
try {
const { id } = req.params;
const { name } = req.body;
await this.renameService.execute(id, name);
res.status(200).send("rename successful");
} catch (error) {
res.status(500).send({ error: error.message });
}
}
}
37 changes: 37 additions & 0 deletions src/api/genially/genially.router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Request, Response, Router } from "express";
import CreateGeniallyService from "../../contexts/core/genially/application/CreateGeniallyService";
import DeleteGeniallyService from "../../contexts/core/genially/application/DeleteGeniallyService";
import RenameGeniallyService from "../../contexts/core/genially/application/RenameGeniallyService";
import InMemoryGeniallyRepository from "../../contexts/core/genially/infrastructure/InMemoryGeniallyRepository";
import { DeleteGeniallyController } from "./controllers/DeleteGeniallyController";
import { GeniallyPostController } from "./controllers/GeniallyPostController";
import { RenameGeniallyController } from "./controllers/RenameGeniallyController";

export function registerGenialyRouter(): Router {
const geniallyRouter = Router();

const geniallyRepository = new InMemoryGeniallyRepository();

//Create Genially Router
const createGeniallyService = new CreateGeniallyService(geniallyRepository);
const genialyPostControler = new GeniallyPostController(
createGeniallyService
);
geniallyRouter.post("/genially/:id", (req: Request, res: Response) =>
genialyPostControler.execute(req, res)
);

const deleteGeniallyService = new DeleteGeniallyService(geniallyRepository);
const deleteControler = new DeleteGeniallyController(deleteGeniallyService);
geniallyRouter.delete("/genially/:id", (req: Request, res: Response) =>
deleteControler.execute(req, res)
);

const renameGeniallyService = new RenameGeniallyService(geniallyRepository);
const renameControler = new RenameGeniallyController(renameGeniallyService);
geniallyRouter.put("/genially/rename/:id", (req: Request, res: Response) =>
renameControler.execute(req, res)
);

return geniallyRouter;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Genially from "../domain/Genially";
import { GeniallyDescription } from "../domain/GeniallyDescription";
import { GeniallyName } from "../domain/GeniallyName";
import GeniallyRepository from "../domain/GeniallyRepository";

type CreateGeniallyServiceRequest = {
Expand All @@ -13,7 +15,11 @@ export default class CreateGeniallyService {
public async execute(req: CreateGeniallyServiceRequest): Promise<Genially> {
const { id, name, description } = req;

const genially = new Genially(id, name, description);
const genially = new Genially(
id,
new GeniallyName(name),
new GeniallyDescription(description)
);

await this.repository.save(genially);

Expand Down
14 changes: 12 additions & 2 deletions src/contexts/core/genially/application/DeleteGeniallyService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import Genially from "../domain/Genially";
import GeniallyNotExist from "../domain/GeniallyNotExist";
import GeniallyRepository from "../domain/GeniallyRepository";

export default class DeleteGeniallyService {
public async execute(): Promise<Genially> {
return undefined;
constructor(private repository: GeniallyRepository) {}

public async execute(id: string): Promise<Genially> {
const genially = await this.repository.find(id);
if (!genially) {
throw new GeniallyNotExist(id);
}
genially.deleteFromDate(new Date());
await this.repository.save(genially);
return genially;
}
}
15 changes: 13 additions & 2 deletions src/contexts/core/genially/application/RenameGeniallyService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import Genially from "../domain/Genially";
import { GeniallyName } from "../domain/GeniallyName";
import GeniallyNotExist from "../domain/GeniallyNotExist";
import GeniallyRepository from "../domain/GeniallyRepository";

export default class RenameGeniallyService {
public async execute(): Promise<Genially> {
return undefined;
constructor(private repository: GeniallyRepository) {}

public async execute(id: string, newName: string): Promise<Genially> {
const genially = await this.repository.find(id);
if (!genially) {
throw new GeniallyNotExist(id);
}
genially.rename(new GeniallyName(newName));
await this.repository.save(genially);
return genially;
}
}
13 changes: 13 additions & 0 deletions src/contexts/core/genially/domain/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class GeniallyNameInvalid extends Error {
constructor(name: string) {
super(`name invalid :"${name}" su longitud debe ser de 3 a 20 caracteres`);
}
}

export class GeniallyDescriptionInvalid extends Error {
constructor(description: string) {
super(
`Description invalid :"${description}" su longitud debe ser de maximo 125 caracteres`
);
}
}
24 changes: 19 additions & 5 deletions src/contexts/core/genially/domain/Genially.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import { GeniallyDescription } from "./GeniallyDescription";
import { GeniallyName } from "./GeniallyName";

export default class Genially {
private _id: string;
private _name: string;
private _description: string;
private _name: GeniallyName;
private _description: GeniallyDescription;
private _createdAt: Date;
private _modifiedAt: Date;
private _deletedAt: Date;

constructor(id: string, name: string, description?: string) {
constructor(
id: string,
name: GeniallyName,
description?: GeniallyDescription
) {
this._id = id;
this._name = name;
this._description = description;
this._createdAt = new Date();
}
public deleteFromDate(date: Date): void {
this._deletedAt = date;
}
public rename(newName: GeniallyName): void {
this._name = newName;
this._modifiedAt = new Date();
}

get id(): string {
return this._id;
}

get name(): string {
return this._name;
return this._name.value;
}

get description(): string {
return this._description;
return this._description.value;
}

get createdAt(): Date {
Expand Down
17 changes: 17 additions & 0 deletions src/contexts/core/genially/domain/GeniallyDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*La descripción de un genially está limitada a 125 caracteres.*/

import { GeniallyDescriptionInvalid } from "./Errors";

export class GeniallyDescription {
readonly value: string;
constructor(value: string) {
this.descriptionIsValid(value);
this.value = value;
}

private descriptionIsValid(value: string) {
if (!(value.length <= 125)) {
throw new GeniallyDescriptionInvalid(value);
}
}
}
17 changes: 17 additions & 0 deletions src/contexts/core/genially/domain/GeniallyName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*El nombre de un genially no puede estar vacío y su longitud debe ser de 3 a 20 caracteres. */

import { GeniallyNameInvalid } from "./Errors";

export class GeniallyName {
readonly value: string;
constructor(value: string) {
this.isValid(value);
this.value = value;
}

private isValid(value: string) {
if (!(value.length >= 3 && value.length <= 20)) {
throw new GeniallyNameInvalid(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Genially from "../domain/Genially";
import GeniallyRepository from "../domain/GeniallyRepository";

export default class InMemoryGeniallyRepository implements GeniallyRepository {
private geniallys: Genially[];
private geniallys: Genially[] = [];

async save(genially: Genially): Promise<void> {
await this.delete(genially.id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import CreateGeniallyService from "../../../../../src/contexts/core/genially/application/CreateGeniallyService";
import Genially from "../../../../../src/contexts/core/genially/domain/Genially";
import GeniallyRepository from "../../../../../src/contexts/core/genially/domain/GeniallyRepository";
import InMemoryGeniallyRepository from "../../../../../src/contexts/core/genially/infrastructure/InMemoryGeniallyRepository";

let repository: GeniallyRepository;
let createGeniallyService: CreateGeniallyService;

beforeEach(() => {
repository = new InMemoryGeniallyRepository();
createGeniallyService = new CreateGeniallyService(repository);
});

describe("test of CreateGeniallyService", () => {
it("should create a valid genially", async () => {
await createGeniallyService.execute({
id: "id-test",
name: "name-test",
description: "descriptions-test",
});

const genially = await repository.find("id-test");
expect(genially).toBeInstanceOf(Genially);
expect(genially.id).toEqual("id-test");
});
});
9 changes: 3 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
}
"include": ["src/**/*","test/**/*.ts"],
"exclude": ["node_modules"]
}
19 changes: 19 additions & 0 deletions tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"],
"exclude": ["node modules", "test/**/*"]
}