Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Use session ID as param
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 23, 2020
1 parent 8233d52 commit d30ce2f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/modules/sessions/sessions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class SessionController {
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<sessions>> {
return this.sessionsService.getSession(userId, { id: Number(id) });
return this.sessionsService.getSession(userId, Number(id));
}

@Delete(':id')
Expand All @@ -60,6 +60,6 @@ export class SessionController {
@Param('userId', ParseIntPipe) userId: number,
@Param('id', ParseIntPipe) id: number,
): Promise<Expose<sessions>> {
return this.sessionsService.deleteSession(userId, { id: Number(id) });
return this.sessionsService.deleteSession(userId, Number(id));
}
}
14 changes: 5 additions & 9 deletions src/modules/sessions/sessions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '@nestjs/common';
import {
sessions,
sessionsCreateInput,
sessionsOrderByInput,
sessionsWhereInput,
sessionsWhereUniqueInput,
Expand Down Expand Up @@ -40,27 +39,24 @@ export class SessionsService {

async getSession(
userId: number,
sessionWhereUniqueInput: sessionsWhereUniqueInput,
id: number,
): Promise<Expose<sessions> | null> {
const session = await this.prisma.sessions.findOne({
where: sessionWhereUniqueInput,
where: { id },
});
if (session.userId !== userId) throw new UnauthorizedException();
if (!session)
throw new HttpException('Session not found', HttpStatus.NOT_FOUND);
return this.prisma.expose<sessions>(session);
}

async deleteSession(
userId: number,
where: sessionsWhereUniqueInput,
): Promise<Expose<sessions>> {
async deleteSession(userId: number, id: number): Promise<Expose<sessions>> {
const testSession = await this.prisma.sessions.findOne({
where,
where: { id },
});
if (testSession.userId !== userId) throw new UnauthorizedException();
const session = await this.prisma.sessions.delete({
where,
where: { id },
});
return this.prisma.expose<sessions>(session);
}
Expand Down

0 comments on commit d30ce2f

Please sign in to comment.