Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c93d8d4
update prisma
DevRadhy Aug 21, 2024
36ec732
refactor: change production schema
DevRadhy Aug 21, 2024
c47a534
feat: create prisma production controll mapper
DevRadhy Aug 21, 2024
e4e23bf
feat: create prisma production controll repository
DevRadhy Aug 21, 2024
29c2098
feat: create production control dto
DevRadhy Aug 21, 2024
cec9e1b
feat: create production control abstract repository
DevRadhy Aug 21, 2024
7deee21
feat: export prisma production repository
DevRadhy Aug 23, 2024
edf9b59
feat: create production routes
DevRadhy Aug 23, 2024
cebc77c
feat: create production controller
DevRadhy Aug 23, 2024
785257e
feat: create production entity
DevRadhy Aug 23, 2024
1e0042b
feat: create production view
DevRadhy Aug 23, 2024
e3de0c3
fix: change animalId
DevRadhy Aug 24, 2024
7001abc
feat: create production create
DevRadhy Aug 24, 2024
4c5fe82
feat: create production entity test
DevRadhy Aug 24, 2024
5ebdfd6
refactor: change production entity test
DevRadhy Aug 24, 2024
61aee96
refactor: change production entity name
DevRadhy Aug 24, 2024
c55bc6c
refactor: change production repositories name
DevRadhy Aug 24, 2024
ce1e9cd
refactor: change productionControl to production
DevRadhy Aug 24, 2024
e1d9a04
feat: add types root to tsconfig
DevRadhy Aug 24, 2024
616f7b7
refactor: change production entity import
DevRadhy Aug 29, 2024
7a52bc1
refactor: change production props
DevRadhy Aug 29, 2024
df5e79a
feat: create find many productions service
DevRadhy Aug 29, 2024
93bef02
feat: create find many productions controller
DevRadhy Aug 29, 2024
357f1f8
feat: create find many productions route
DevRadhy Aug 29, 2024
f1d8681
feat: add misc to gitignore
DevRadhy Aug 29, 2024
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
5 changes: 4 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ node_modules/
dist/

# logs
**/*.log
**/*.log

# misc
.vscode/
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"prisma": "^5.2.0",
"prisma": "^5.18.0",
"tsx": "^4.7.0",
"typescript": "*",
"vitest": "^0.28.5",
"zod": "^3.20.6"
},
"dependencies": {
"@prisma/client": "^5.2.0",
"@prisma/client": "^5.10.2",
"@types/cors": "^2.8.13",
"bcrypt": "^5.1.0",
"bcryptjs": "^2.4.3",
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

*/
-- AlterTable
ALTER TABLE "animals" ADD COLUMN "gender" DECIMAL(65,30) NOT NULL;
ALTER TABLE "animals" ADD COLUMN "gender" INTEGER NOT NULL;

-- CreateTable
CREATE TABLE "genders" (
"id" DECIMAL(65,30) NOT NULL,
"type" DECIMAL(65,30) NOT NULL,
"id" SERIAL NOT NULL,
"type" TEXT NOT NULL,

CONSTRAINT "genders_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "genders_type_key" ON "genders"("type");

-- AddForeignKey
ALTER TABLE "animals" ADD CONSTRAINT "animals_gender_fkey" FOREIGN KEY ("gender") REFERENCES "genders"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "animal_productions" (
"id" TEXT NOT NULL,
"animal_id" TEXT NOT NULL,
"date" TIMESTAMP(3) NOT NULL,
"goal" TEXT NOT NULL,
"price" DECIMAL(65,30) NOT NULL,

CONSTRAINT "animal_productions_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "animal_productions" ADD CONSTRAINT "animal_productions_animal_id_fkey" FOREIGN KEY ("animal_id") REFERENCES "animals"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
4 changes: 2 additions & 2 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ model Animal {
owner User @relation(fields: [ownerId], references: [id])
genderId Gender @relation(fields: [gender], references: [id])

AnimalProduction AnimalProduction[]
production Production[]

@@map("animals")
}
Expand All @@ -49,7 +49,7 @@ model Gender {
@@map("genders")
}

model AnimalProduction {
model Production {
id String @id @default(uuid())
animalId String @map("animal_id")
date DateTime
Expand Down
30 changes: 30 additions & 0 deletions server/src/controllers/Production/createProductionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Request, Response } from "express";
import { z } from "zod";
import { CreateProduction } from "../../services/Production/createProduction";
import { ProductionViewModel } from "../../views/ProductionViewModel";

export class CreateProductionController {
constructor (
private createProduction: CreateProduction,
) {}

async handle(request: Request, response: Response) {
const CreateAnimalProps = z.object({
animalId: z.string(),
date: z.string(),
goal: z.string(),
price: z.number(),
});

const { animalId, date, goal, price } = CreateAnimalProps.parse(request.body);

const production = await this.createProduction.execute({
animalId,
date: new Date(date),
goal,
price,
});

return response.status(201).json(ProductionViewModel.toHTTP(production));
}
}
17 changes: 17 additions & 0 deletions server/src/controllers/Production/findManyProductionsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Request, Response } from "express";
import { FindManyProductions } from "../../services/Production/findManyProductions";
import { ProductionViewModel } from "../../views/ProductionViewModel";

export class FindManyProductionsController {
constructor(
private findManyProductions: FindManyProductions,
) {}

async handle(request: Request, response: Response) {
const { ownerId } = request.params;

const productions = await this.findManyProductions.execute(ownerId);

return response.json(productions.map((production) => ProductionViewModel.toHTTP(production)));
}
}
8 changes: 8 additions & 0 deletions server/src/controllers/Production/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createProduction, findManyProductions } from "../../services/Production";
import { CreateProductionController } from "./createProductionController";
import { FindManyProductionsController } from "./findManyProductionsController";

const createProductionController = new CreateProductionController(createProduction);
const findManyProductionsController = new FindManyProductionsController(findManyProductions);

export { createProductionController, findManyProductionsController };
8 changes: 7 additions & 1 deletion server/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { PrismaClient } from "@prisma/client";
import { PrismaAnimalsRepository } from "./prisma/PrismaAnimalsRepository";
import { PrismaUsersRepository } from "./prisma/PrismaUsersRepository";
import { PrismaProductionRepository } from "./prisma/PrismaProductionRepository";

const prisma = new PrismaClient();
const prismaAnimalsRepository = new PrismaAnimalsRepository(prisma);
const prismaUsersRepository = new PrismaUsersRepository(prisma);
const prismaProduction = new PrismaProductionRepository(prisma);

export { prismaAnimalsRepository, prismaUsersRepository };
export {
prismaAnimalsRepository,
prismaUsersRepository,
prismaProduction
};
23 changes: 23 additions & 0 deletions server/src/database/mappers/PrismaProductionMappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ProductionProps } from "../../dtos/AnimalsDTO";

export class PrismaProduction {
static toPrisma(data: ProductionProps) {
return {
id: data.id,
animalId: data.animalId,
date: data.date,
goal: data.goal,
price: data.price,
};
}

static toDomain(raw: any) {
return {
id: raw.id,
animalId: raw.animalId,
date: new Date(raw.date),
goal: raw.goal,
price: Number(raw.price),
};
}
}
67 changes: 67 additions & 0 deletions server/src/database/prisma/PrismaProductionRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { PrismaClient } from "@prisma/client";
import { ProductionRepository } from "../../repositories/ProductionRepository";
import { ProductionProps } from "../../dtos/AnimalsDTO";
import { PrismaProduction } from "../mappers/PrismaProductionMappers";

export class PrismaProductionRepository implements ProductionRepository {
constructor (
private prisma: PrismaClient,
) {}

async create(props: ProductionProps): Promise<void> {
const raw = PrismaProduction.toPrisma(props);

await this.prisma.production.create({
data: raw
});
}

async findByAnimalId(id: string): Promise<ProductionProps | null> {
const animalExists = await this.prisma.production.findFirst({
where: {
animalId: id,
}
});

if(!animalExists) {
return null;
}

return PrismaProduction.toDomain(animalExists);
}

async findMany(ownerId: string): Promise<ProductionProps[]> {
const animals = await this.prisma.production.findMany({
where: {
animal: {
ownerId,
}
}
});

return animals.map(PrismaProduction.toDomain);
}

async save(props: ProductionProps): Promise<void> {
const raw = PrismaProduction.toPrisma(props);

await this.prisma.production.update({
where: {
id: raw.id,
},
data: raw,
});

return;
}

async delete(id: string): Promise<void> {
await this.prisma.production.delete({
where: {
id,
}
});

return;
}
}
38 changes: 38 additions & 0 deletions server/src/entities/Production.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { randomUUID } from 'crypto';
import { describe, expect, it } from 'vitest';
import Production from './Production';

describe("Production", () => {
it("Should be able to create a new production", () => {
const raw = {
animalId: randomUUID(),
date: new Date(),
goal: "Engorda",
price: 1250.00,
};

const animal = new Production(raw);

expect(animal).toBeTruthy();
expect(animal).toHaveProperty("id");
expect(animal).toContain({
id: animal.id,
...raw,
});
});

it("Should be able to create a instance to an existing production", () => {
const id = randomUUID();

const animal = new Production({
animalId: randomUUID(),
date: new Date(),
goal: "Engorda",
price: 1250.00,
}, id);

expect(animal).toBeTruthy();
expect(animal).toHaveProperty("id");
expect(animal.id).toBe(id);
});
});
38 changes: 38 additions & 0 deletions server/src/entities/Production.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { randomUUID } from "crypto";

interface ProductionProps {
animalId: string;
date: Date;
goal: string;
price: number;
}

export default class Production {
private _id: string;
private props: ProductionProps;

constructor(props: ProductionProps, id?: string) {
this.props = props;
this._id = id ?? randomUUID();
}

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

public get animalId() {
return this.props.animalId;
}

public get date() {
return this.props.date;
}

public get goal() {
return this.props.goal;
}

public get price() {
return this.props.price;
}
}
9 changes: 9 additions & 0 deletions server/src/repositories/ProductionRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Production from "../entities/Production";

export abstract class ProductionRepository {
abstract create(props: Production): Promise<void>;
abstract findByAnimalId(id: string): Promise<Production | null>;
abstract findMany(ownerId: string): Promise<Production[]>;
abstract save(props: Production): Promise<void>;
abstract delete(id: string): Promise<void>;
}
Loading