-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
@@ -0,0 +1,4 @@ | |||
import { OmitType } from '@nestjs/swagger'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Co-authored-by: Farkasházi Levente <[email protected]>
async create(createDrinkActionDto: CreateDrinkActionDto) { | ||
return this.prisma.drinkAction.create({ data: createDrinkActionDto }); | ||
} |
There was a problem hiding this comment.
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. :
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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)
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); | ||
} |
There was a problem hiding this comment.
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
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; | ||
} |
There was a problem hiding this comment.
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) {} |
There was a problem hiding this comment.
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
package-lock.json
Outdated
There was a problem hiding this comment.
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 ...
)
There was a problem hiding this 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
data: { | ||
...rest, | ||
drink: { connect: { id: drinkId } }, | ||
event: { connect: { id: eventId } }, | ||
}, |
There was a problem hiding this comment.
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 }, |
There was a problem hiding this comment.
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
} catch (error) { | ||
if (error instanceof PrismaClientKnownRequestError) { | ||
if (error.code === 'P2003') { | ||
throw new BadRequestException('drinkId or eventId does not exist'); | ||
} | ||
} | ||
throw error; | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) {} |
There was a problem hiding this comment.
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
No description provided.