Skip to content

Commit

Permalink
Categories migration
Browse files Browse the repository at this point in the history
  • Loading branch information
paulordyl committed Jun 30, 2021
1 parent e928595 commit f64d1c8
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 45 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules --respawn src/server.ts"
"dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules --respawn src/server.ts",
"typeorm": "ts-node-dev ./node_modules/typeorm/cli"
},
"dependencies": {
"csv-parse": "^4.16.0",
Expand Down
35 changes: 35 additions & 0 deletions src/database/migrations/1625013295458-CreateCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { MigrationInterface, QueryRunner, Table } from "typeorm";

export class CreateCategories1625013295458 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "categories",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
{
name: "description",
type: "varchar",
},
{
name: "created_at",
type: "timestamp",
default: "now()",
},
],
})
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("categories");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Column, CreateDateColumn, Entity, PrimaryColumn } from "typeorm";
import { v4 as uuidV4 } from "uuid";

@Entity("categories")
class Category {
@PrimaryColumn()
id?: string;

@Column()
name: string;

@Column()
description: string;

@CreateDateColumn()
created_at: Date;

constructor() {
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/modules/cars/repositories/ICategoriesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Category } from "../model/Category";
import { Category } from "../entities/Category";

interface ICreateCategoryDTO {
name: string;
description: string;
}

interface ICategoriesRepository {
findByName(name: string): Category;
list(): Category[];
create({ name, description }: ICreateCategoryDTO): void;
findByName(name: string): Promise<Category>;
list(): Promise<Category[]>;
create({ name, description }: ICreateCategoryDTO): Promise<void>;
}

export { ICategoriesRepository, ICreateCategoryDTO };
2 changes: 1 addition & 1 deletion src/modules/cars/repositories/ISpecificationsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Specification } from "../model/Specification";
import { Specification } from "../entities/Specification";

interface ISpecificationDTO {
name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
import { Category } from "../../model/Category";
import { getRepository, Repository } from "typeorm";

import { Category } from "../../entities/Category";
import {
ICategoriesRepository,
ICreateCategoryDTO,
} from "../ICategoriesRepository";

class CategoriesRepository implements ICategoriesRepository {
private categories: Category[];

private static INSTANCE: CategoriesRepository;
private repository: Repository<Category>;

private constructor() {
this.categories = [];
constructor() {
this.repository = getRepository(Category);
}

public static getInstance(): CategoriesRepository {
if (!CategoriesRepository.INSTANCE) {
CategoriesRepository.INSTANCE = new CategoriesRepository();
}
return CategoriesRepository.INSTANCE;
}

create({ name, description }: ICreateCategoryDTO): void {
const category = new Category();

Object.assign(category, {
async create({ name, description }: ICreateCategoryDTO): Promise<void> {
const category = this.repository.create({
name,
description,
created_at: new Date(),
});

this.categories.push(category);
await this.repository.save(category);
}

list(): Category[] {
return this.categories;
async list(): Promise<Category[]> {
const categories = await this.repository.find();
return categories;
}

findByName(name: string): Category {
const category = this.categories.find((category) => category.name === name);
async findByName(name: string): Promise<Category> {
const category = this.repository.findOne({ name });
return category;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Specification } from "../../model/Specification";
import { Specification } from "../../entities/Specification";
import {
ISpecificationsRepository,
ISpecificationDTO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { CreateCategoryUseCase } from "./CreateCategoryUseCase";
class CreateCategoryController {
constructor(private createCategoryUseCase: CreateCategoryUseCase) {}

handle(request: Request, response: Response): Response {
async handle(request: Request, response: Response): Promise<Response> {
const { name, description } = request.body;

this.createCategoryUseCase.execute({ name, description });
await this.createCategoryUseCase.execute({ name, description });

return response.status(201).send();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ interface IRequest {
class CreateCategoryUseCase {
constructor(private categoriesRepository: ICategoriesRepository) {}

execute({ name, description }: IRequest): void {
const categoryAlreadyExists = this.categoriesRepository.findByName(name);
async execute({ name, description }: IRequest): Promise<void> {
const categoryAlreadyExists = await this.categoriesRepository.findByName(
name
);

if (categoryAlreadyExists) {
throw new Error("Category already exists!");
Expand Down
14 changes: 8 additions & 6 deletions src/modules/cars/useCases/createCategory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { CategoriesRepository } from "../../repositories/implementations/Categor
import { CreateCategoryController } from "./CreateCategoryController";
import { CreateCategoryUseCase } from "./CreateCategoryUseCase";

const categoriesRepository = CategoriesRepository.getInstance();
export default (): CreateCategoryController => {
const categoriesRepository = new CategoriesRepository();

const createCategoryUseCase = new CreateCategoryUseCase(categoriesRepository);
const createCategoryUseCase = new CreateCategoryUseCase(categoriesRepository);

const createCategoryController = new CreateCategoryController(
createCategoryUseCase
);
const createCategoryController = new CreateCategoryController(
createCategoryUseCase
);

export { createCategoryController };
return createCategoryController;
};
2 changes: 1 addition & 1 deletion src/modules/cars/useCases/importCategory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CategoriesRepository } from "../../repositories/implementations/Categor
import { ImportCategoryController } from "./ImportCategoryController";
import { ImportCategoryUseCase } from "./ImportCategoryUseCase";

const categoriesRepository = CategoriesRepository.getInstance();
const categoriesRepository = null;

const importCategoryUseCase = new ImportCategoryUseCase(categoriesRepository);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Category } from "../../model/Category";
import { Category } from "../../entities/Category";
import { ICategoriesRepository } from "../../repositories/ICategoriesRepository";

class ListCategoriesUseCase {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/cars/useCases/listCategories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CategoriesRepository } from "../../repositories/implementations/Categor
import { ListCategoriesController } from "./ListCategoriesController";
import { ListCategoriesUseCase } from "./ListCategoriesUseCase";

const categoriesRepository = CategoriesRepository.getInstance();
const categoriesRepository = null;
const listCategoriesUseCase = new ListCategoriesUseCase(categoriesRepository);
const listCategoriesController = new ListCategoriesController(
listCategoriesUseCase
Expand Down
4 changes: 2 additions & 2 deletions src/routes/categories.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express";
import multer from "multer";

import { createCategoryController } from "../modules/cars/useCases/createCategory";
import createCategoryController from "../modules/cars/useCases/createCategory";
import { importCategoryController } from "../modules/cars/useCases/importCategory";
import { listCategoriesController } from "../modules/cars/useCases/listCategories";

Expand All @@ -12,7 +12,7 @@ const upload = multer({
});

categoriesRoutes.post("/", (request, response) => {
return createCategoryController.handle(request, response);
return createCategoryController().handle(request, response);
});

categoriesRoutes.get("/", (request, response) => {
Expand Down

0 comments on commit f64d1c8

Please sign in to comment.