Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Add isLiked property to rules list (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxidragon authored Jan 8, 2024
1 parent 4e3ba60 commit 7fb68d8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ForbiddenException, Injectable } from '@nestjs/common';
import { ForbiddenException, HttpException, Injectable } from '@nestjs/common';
import { JwtAuthDto, LoginDto, RegisterDto } from './dto';
import { DbService } from '../db/db.service';
import { sha512 } from 'js-sha512';
Expand Down Expand Up @@ -26,18 +26,19 @@ export class AuthService {
}

async login(dto: LoginDto): Promise<object> {
const user = await this.prisma.user.findUniqueOrThrow({
const user = await this.prisma.user.findUnique({
where: { email: dto.email },
});

if (!user || (user && !(sha512(dto.password) === user.password))) {
throw new HttpException('Wrong credentials!', 403);
}

const roles = await this.prisma.roles.findMany({
where: { userId: user.id },
select: { role: true },
});

if (!user || !(sha512(dto.password) === user.password))
throw new ForbiddenException('Wrong credentials!');

const jwt = await this.generateAuthJwt({
userId: user.id,
roles: roles.map((role) => role.role),
Expand Down
7 changes: 7 additions & 0 deletions src/auth/guards/optionalJwtAuth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AuthGuard } from '@nestjs/passport';

export class OptionalJwtAuthGuard extends AuthGuard('jwt') {
handleRequest(err, user) {
return user;
}
}
21 changes: 15 additions & 6 deletions src/rule/rule.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@ import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { RuleService } from './rule.service';
import { JwtAuthDto } from 'src/auth/dto';
import { AuthGuard } from '@nestjs/passport';
import { GetUser } from 'src/auth/decorator/getUser.decorator';
import { GetUser } from '../auth/decorator/getUser.decorator';
import { OptionalJwtAuthGuard } from '../auth/guards/optionalJwtAuth.guard';

@Controller('rule')
export class RuleController {
constructor(private readonly ruleService: RuleService) {}

@UseGuards(OptionalJwtAuthGuard)
@Get('search')
async searchRules(@Query('search') search: string) {
return await this.ruleService.searchRules(search);
async searchRules(
@Query('search') search: string,
@Query('skip') skip = 0,
@Query('take') take = 10,
@GetUser() user: JwtAuthDto,
) {
return await this.ruleService.searchRules(search, skip, take, user.userId);
}

@UseGuards(OptionalJwtAuthGuard)
@Get('user/:userId')
async getUserRules(
@Param('userId') userId: number,
@Query('skip') skip: number,
@Query('take') take: number,
@Query('skip') skip = 0,
@Query('take') take = 10,
@GetUser() user: JwtAuthDto,
) {
return await this.ruleService.getUserRules(userId, skip, take);
return await this.ruleService.getUserRules(userId, skip, take, user.userId);
}
@UseGuards(AuthGuard('jwt'))
@Get('like/:id')
Expand Down
26 changes: 23 additions & 3 deletions src/rule/rule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Injectable } from '@nestjs/common';
export class RuleService {
constructor(private readonly prisma: DbService) {}

async getUserRules(userId: number, skip = 0, take = 10) {
async getUserRules(userId: number, skip = 0, take = 10, requestedBy: number) {
const rules: any = await this.prisma.rule.findMany({
where: { ruleset: { userId } },
skip,
Expand All @@ -16,6 +16,12 @@ export class RuleService {
where: { ruleId: rule.id },
});
rule.likes = likes;
if (requestedBy) {
const liked = await this.prisma.likedRules.findFirst({
where: { ruleId: rule.id, userId: requestedBy },
});
rule.isLiked = !!liked;
}
}
return rules;
}
Expand All @@ -42,11 +48,25 @@ export class RuleService {
}
}

async searchRules(search: string, skip = 0, take = 10) {
return await this.prisma.rule.findMany({
async searchRules(search: string, skip = 0, take = 10, requestedBy: number) {
const rules: any = await this.prisma.rule.findMany({
where: { name: { contains: search } },
skip,
take,
});

for (const rule of rules) {
const likes = await this.prisma.likedRules.count({
where: { ruleId: rule.id },
});
rule.likes = likes;
if (requestedBy) {
const liked = await this.prisma.likedRules.findFirst({
where: { ruleId: rule.id, userId: requestedBy },
});
rule.isLiked = !!liked;
}
}
return rules;
}
}

0 comments on commit 7fb68d8

Please sign in to comment.