-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from jaiminRaiyani/feat/hashtag-management
Feat/hashtag management
- Loading branch information
Showing
12 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
11 changes: 11 additions & 0 deletions
11
backend/prisma/migrations/20250210093710_hashtags/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-- CreateTable | ||
CREATE TABLE "hashtags" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "hashtags_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "hashtags_name_key" ON "hashtags"("name"); |
2 changes: 2 additions & 0 deletions
2
backend/prisma/migrations/20250210132054_added_hashtag_updated_at/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "hashtags" ADD COLUMN "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IsString, IsNotEmpty, Matches } from 'class-validator'; | ||
|
||
export class CreateHashtagDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
@Matches(/^[a-zA-Z0-9_]+$/, { | ||
message: 'Hashtag name can only contain letters, numbers, and underscores', | ||
}) | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Controller, Post, Get, Body, UseGuards } from '@nestjs/common'; | ||
import { HashtagsService } from './hashtags.service'; | ||
import { RolesGuard } from '../common/guards/roles.guard'; // Use existing RolesGuard | ||
import { Roles } from '../common/decorators/roles.decorator'; // Apply role-based access | ||
import { Role } from '../common/enums/roles.enum'; // Ensure correct role names | ||
import { CreateHashtagDto } from './dto/create-hashtag.dto'; | ||
|
||
@Controller('hashtags') | ||
@UseGuards(RolesGuard) // Apply role guard globally in this controller | ||
export class HashtagsController { | ||
constructor(private readonly hashtagsService: HashtagsService) {} | ||
|
||
@Post() | ||
@Roles(Role.Admin) | ||
async create(@Body() createHashtagDto: CreateHashtagDto) { | ||
const hashtag = await this.hashtagsService.create(createHashtagDto); | ||
return { message: 'Hashtag created successfully', data: hashtag }; | ||
} | ||
|
||
@Get() | ||
async findAll() { | ||
return this.hashtagsService.findAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HashtagsService } from './hashtags.service'; | ||
import { HashtagsController } from './hashtags.controller'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Module({ | ||
controllers: [HashtagsController], | ||
providers: [HashtagsService, PrismaService], | ||
}) | ||
export class HashtagsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { HashtagsService } from './hashtags.service'; | ||
import { PrismaService } from 'nestjs-prisma'; | ||
|
||
describe('HashtagsService', () => { | ||
let service: HashtagsService; | ||
let prisma: PrismaService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [HashtagsService, PrismaService], | ||
}).compile(); | ||
|
||
service = module.get<HashtagsService>(HashtagsService); | ||
prisma = module.get<PrismaService>(PrismaService); | ||
}); | ||
|
||
it('should create a hashtag', async () => { | ||
jest | ||
.spyOn(prisma.hashtag, 'create') | ||
.mockResolvedValue({ id: '1', name: 'NestJS', createdAt: new Date() }); | ||
|
||
const result = await service.create({ name: 'NestJS' }); | ||
expect(result.name).toBe('NestJS'); | ||
}); | ||
|
||
it('should find all hashtags', async () => { | ||
jest | ||
.spyOn(prisma.hashtag, 'findMany') | ||
.mockResolvedValue([{ id: '1', name: 'NestJS', createdAt: new Date() }]); | ||
|
||
const result = await service.findAll(); | ||
expect(result.length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { CreateHashtagDto } from './dto/create-hashtag.dto'; | ||
|
||
@Injectable() | ||
export class HashtagsService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async create(createHashtagDto: CreateHashtagDto) { | ||
const hashtag = await this.prisma.hashtag.create({ | ||
data: createHashtagDto, | ||
}); | ||
return hashtag; | ||
} | ||
async findAll() { | ||
return this.prisma.hashtag.findMany(); | ||
} | ||
} |