Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement 10 and 11 issues #15

Merged
merged 7 commits into from
Jul 3, 2024
Merged

implement 10 and 11 issues #15

merged 7 commits into from
Jul 3, 2024

Conversation

murtagh27
Copy link
Contributor

No description provided.

@murtagh27 murtagh27 requested review from a team, Tschonti, LeventeFarkashazi, pem00 and VarMattew and removed request for a team May 22, 2024 22:36
package.json Outdated Show resolved Hide resolved
@@ -0,0 +1,4 @@
import { OmitType } from '@nestjs/swagger';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can fix this by running yarn lint:fix. If you have the ESLint extension in VSCode, it should show the error and may even fix it when saving the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
i dont know whats the problem, i have eslint...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why it doesn't find the apps directory. Maybe try running it manually: npx eslint apps/* --ext .ts,.tsx --fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try running from a git bash terminal? It's possible that powershell doesn't like forward slashes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried both and it still says the same error message:l

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command was invalid on windows, I'll push to master to fix this. You can do the linting with yarn eslint apps --ext .ts,.tsx --fix until then.

Comment on lines 12 to 14
async create(createDrinkActionDto: CreateDrinkActionDto) {
return this.prisma.drinkAction.create({ data: createDrinkActionDto });
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the drinkId or the eventId is invalid, we get an internal server error. There should be some error handling for these cases.
e.g. :

Suggested change
async create(createDrinkActionDto: CreateDrinkActionDto) {
return this.prisma.drinkAction.create({ data: createDrinkActionDto });
}
async create(createDrinkActionDto: CreateDrinkActionDto) {
const { drinkId, eventId } = createDrinkActionDto;
const [drinkExists, eventExists] = await Promise.all([
this.prisma.drink.findUnique({ where: { id: drinkId } }),
this.prisma.event.findUnique({ where: { id: eventId } }),
]);
if (!drinkExists) {
throw new BadRequestException(`Drink with ID ${drinkId} does not exist.`);
}
if (!eventExists) {
throw new BadRequestException(`Event with ID ${eventId} does not exist.`);
}
return this.prisma.drinkAction.create({ data: createDrinkActionDto });
}
}

but there are other (maybe more efficient ) ways to do it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably more efficient to just attempt the update and catch any exceptions. We might not be able to tell which ID was invalid, but since this API won't be public, it's not essential to give a very detailed error message

Copy link
Contributor

@LeventeFarkashazi LeventeFarkashazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix these, and update the branch. I think there should be no merge conflicts so you can simply do it on the PR-s github page (there is a button under the checks)

Comment on lines 20 to 27
async findAll(): Promise<DrinkAction[]> {
return this.drinkActionsService.findAll();
}

@Get(':id')
async findOne(@Param('id', new ParseUUIDPipe()) id: string): Promise<DrinkAction> {
return this.drinkActionsService.findOne(id);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should return a different DTO, that also includes the event and drink fields

Comment on lines 16 to 31
async findAll(): Promise<DrinkAction[]> {
return this.prisma.drinkAction.findMany({
include: { drink: true, event: true },
});
}

async findOne(id: string): Promise<DrinkAction> {
const drinkAction = await this.prisma.drinkAction.findUnique({
where: { id },
include: { drink: true, event: true },
});
if (!drinkAction) {
throw new NotFoundException('DrinkAction not found');
}
return drinkAction;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should also return with that new DTO


import { CreateDrinkActionDto } from './create-drink-action.dto';

export class UpdateDrinkActionDto extends PartialType(CreateDrinkActionDto) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drinkId and eventId should not be updateable, so remove those from this class with OmitType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please delete this file. This is generated when you use a command like npm install .... We should be using yarn instead (e.g. yarn add ...)

This was linked to issues Jun 24, 2024
Copy link
Member

@Tschonti Tschonti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting there, just some minor issues

Comment on lines +18 to +22
data: {
...rest,
drink: { connect: { id: drinkId } },
event: { connect: { id: eventId } },
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Connecting drink and event like this is probably not needed, since both the dto and the schema have drinkId and eventId fields, so the connection is established just by passing the dto to the data property. But it also doesn't hurt, at least this way the connection is more apparent just from looking at the code . So this can stay

drink: { connect: { id: drinkId } },
event: { connect: { id: eventId } },
},
include: { drink: true, event: true },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary here. We don't usually do anything with the return object of a POST request, so there's no need to add extra joins. Returning just the standard DrinkAction is fine

Comment on lines 25 to 32
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2003') {
throw new BadRequestException('drinkId or eventId does not exist');
}
}
throw error;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested this with invalid event or drink Ids, I got a different Prisma error code, so the response was 500 instead of 400.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont know how to fix this, can you tell me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you console.log the error inside the catch block and send a request with an invalid event or drink id, you'll see the error that prisma throws. You can use the code from that instead of P2003

export class UpdateDrinkActionDto extends OmitType(PartialType(CreateDrinkActionDto), [
'drinkId',
'eventId',
] as const) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as const is not needed here

@Tschonti Tschonti merged commit 4c052c7 into main Jul 3, 2024
3 checks passed
@Tschonti Tschonti deleted the lilla branch July 3, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create DrinkAction service Create DrinkAction controller and DTO
3 participants