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

Commit

Permalink
Add search tags route and fix updating tags in ruleset (#88)
Browse files Browse the repository at this point in the history
* Add search tags route and fix updating tags in ruleset

* Add take query

* Search with startsWith instead of include

* Add usageCount to tag
  • Loading branch information
maxidragon authored Dec 29, 2023
1 parent 63670c9 commit 73ba813
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
56 changes: 56 additions & 0 deletions src/ruleset/ruleset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class RulesetService {
if (dto.updateRulesList) {
await this.updateRulesList(rulesetId, userId);
}
await this.updateTags(rulesetId, dto.tags, userId);
return {
message: 'Ruleset updated successfully',
status: 200,
Expand Down Expand Up @@ -368,6 +369,61 @@ export class RulesetService {
}
}

async updateTags(rulesetId: number, tags: string[], userId: number) {
const ruleset = await this.getRulesetById(rulesetId);
if (!ruleset) {
throw new HttpException(
{
statusCode: 404,
message: 'Ruleset not found',
error: 'Not Found',
},
404,
);
}
if (ruleset.user.id !== userId) {
throw new HttpException(
{
statusCode: 403,
message: 'You are not allowed to update this ruleset',
error: 'Forbidden',
},
403,
);
}
await this.prisma.rulesetTag.deleteMany({
where: {
rulesetId,
},
});
for (const tag of tags) {
const newTag = await this.prisma.tag.upsert({
where: {
name: tag,
},
update: {},
create: {
name: tag,
},
});

await this.prisma.rulesetTag.create({
data: {
ruleset: {
connect: {
id: rulesetId,
},
},
tag: {
connect: {
id: newTag.id,
},
},
},
});
}
}

async deleteRuleset(rulesetId: number, userId: number) {
const ruleset = await this.getRulesetById(rulesetId);
if (!ruleset) {
Expand Down
6 changes: 3 additions & 3 deletions src/tag/tag.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { TagService } from './tag.service';

@Controller('tag')
export class TagController {
constructor(private readonly tagService: TagService) {}

@Get()
async getAllTags() {
return await this.tagService.getAllTags();
async searchTags(@Query('search') search: string, @Query('take') take = 10) {
return await this.tagService.searchTags(search, take);
}
}
34 changes: 32 additions & 2 deletions src/tag/tag.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@ import { DbService } from '../db/db.service';
export class TagService {
constructor(private readonly prisma: DbService) {}

async getAllTags() {
return await this.prisma.tag.findMany();
async searchTags(search: string, take = 10) {
const exactMatch: any = await this.prisma.tag.findFirst({
where: { name: search },
});
const partialMatchCount = await this.prisma.rulesetTag.groupBy({
by: ['tagId'],
_count: true,
orderBy: {
_count: {
tagId: 'desc',
},
},
where: { tag: { name: { startsWith: search } } },
take: take - (exactMatch ? 1 : 0),
});
const partialMatch: any = await this.prisma.tag.findMany({
where: { id: { in: partialMatchCount.map((t) => t.tagId) } },
});
if (exactMatch) {
exactMatch.usageCount = partialMatchCount.find(
(t) => t.tagId === exactMatch.id,
)._count;
}
partialMatch.map((tag) => {
tag.usageCount = partialMatchCount.find((t) => t.tagId === tag.id)._count;
});
return {
tags: [...(exactMatch ? [exactMatch] : []), ...partialMatch].filter(
(tag, index, self) =>
self.findIndex((t) => t.name === tag.name) === index,
),
};
}
}

0 comments on commit 73ba813

Please sign in to comment.