Skip to content

Commit

Permalink
Update controller and repo project
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoHernandez92 committed May 9, 2024
1 parent 223e4a2 commit 4856f53
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/controllers/projects.controller/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { type Response, type Request, type NextFunction } from 'express';

import {
type Project,
type ProjectDto,
type ProjectCreateDto,
} from '../../entities/projects.entitty/projects.entity.js';
import { type ProjectRepository } from '../../repositories/projects.repository/project.repository.js';
import { HttpError } from '../../middleware/errors.middleware/errors.middleware.js';
import { type Category } from '@prisma/client';

const debug = createDebug('FP*:controller');

export class ProjectController extends BaseController<Project, ProjectDto> {
export class ProjectController extends BaseController<
Project,
ProjectCreateDto
> {
constructor(protected readonly repo: ProjectRepository) {
debug('instantiated project controller');
super(repo);
Expand Down
10 changes: 9 additions & 1 deletion src/entities/projects.entitty/projects.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ export type Project = {
author: Partial<User>;
};

export type ProjectDto = {
export type ProjectCreateDto = {
title: string;
content: string;
archive: string;
category: Category;
authorId: string;
};

export type ProjectUpdateDto = {
title?: string;
content?: string;
archive?: string;
category?: Category;
authorId?: string;
};

type Category =
| 'geography'
| 'anatomy'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type PrismaClient } from '@prisma/client';
import { type ProjectDto } from '../../entities/projects.entitty/projects.entity';
import { type ProjectCreateDto } from '../../entities/projects.entitty/projects.entity';
import { HttpError } from '../../middleware/errors.middleware/errors.middleware';
import { ProjectRepository } from './project.repository';
import { type Payload } from '../../services/auth.services/auth.services';
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Given a instance of the class UserRepository', () => {

describe('When we use the method create', () => {
test('Then it should call prisma.create', async () => {
const data = {} as unknown as ProjectDto & Payload;
const data = {} as unknown as ProjectCreateDto & Payload;
const result = await repo.create(data);
expect(mockPrisma.project.create).toHaveBeenCalled();
expect(result).toEqual({});
Expand Down
9 changes: 5 additions & 4 deletions src/repositories/projects.repository/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { type Category, type PrismaClient } from '@prisma/client';
import { type Repo } from '../type.repo';
import createDebug from 'debug';
import {
type ProjectUpdateDto,
type Project,
type ProjectDto,
type ProjectCreateDto,
} from '../../entities/projects.entitty/projects.entity';
import { HttpError } from '../../middleware/errors.middleware/errors.middleware.js';
import { type Payload } from '../../services/auth.services/auth.services';
Expand All @@ -25,7 +26,7 @@ const select = {
},
},
};
export class ProjectRepository implements Repo<Project, ProjectDto> {
export class ProjectRepository implements Repo<Project, ProjectCreateDto> {
constructor(private readonly prisma: PrismaClient) {
debug('instantiated project repository');
}
Expand All @@ -48,7 +49,7 @@ export class ProjectRepository implements Repo<Project, ProjectDto> {
return project;
}

async create(data: ProjectDto & Payload) {
async create(data: ProjectCreateDto & Payload) {
const { payload, ...dto } = data;
const newProject = await this.prisma.project.create({
data: dto,
Expand All @@ -57,7 +58,7 @@ export class ProjectRepository implements Repo<Project, ProjectDto> {
return newProject;
}

async update(id: string, data: Partial<ProjectDto & Payload>) {
async update(id: string, data: Partial<ProjectCreateDto & Payload>) {
const { payload, ...projectDto } = data;
const project = await this.prisma.project.findUnique({
where: { id },
Expand Down
Binary file added uploads/1715255309112_descarga.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4856f53

Please sign in to comment.