Skip to content

Commit

Permalink
feat: language filtering (#7)
Browse files Browse the repository at this point in the history
SubstantialCattle5 authored Sep 24, 2024
1 parent fb8ce12 commit 1d62634
Showing 5 changed files with 272 additions and 187 deletions.
2 changes: 2 additions & 0 deletions prisma/migrations/20240924073131_clang/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Blog" ADD COLUMN "language" TEXT NOT NULL DEFAULT 'english';
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -60,6 +60,8 @@ model Blog {
userId Int?
region String @default("general")
language String @default("english")
@@index([userId])
@@index([title])
}
144 changes: 88 additions & 56 deletions src/blog/blog.controller.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,100 @@
import { Controller, Get, Post, Put, Delete, Param, Body, Query, Res, UseGuards } from "@nestjs/common";
import { ApiTags, ApiResponse, ApiOperation, ApiBearerAuth } from "@nestjs/swagger";
import { BlogService } from "./blog.service";
import { CreateBlogDto } from "./dto/create-blog.dto";
import { UpdateBlogDto } from "./dto/update-blog.dto";
import { JwtGuard } from "../auth/guards/auth.guard";

import {
Controller,
Get,
Post,
Put,
Delete,
Param,
Body,
Query,
Res,
} from '@nestjs/common';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import { BlogService } from './blog.service';
import { CreateBlogDto } from './dto/create-blog.dto';
import { UpdateBlogDto } from './dto/update-blog.dto';

@ApiTags('blog')
@Controller('blog')
export class BlogController {
constructor(private readonly blogService: BlogService) { }
constructor(private readonly blogService: BlogService) {}

@Post()
@ApiOperation({ summary: 'Create a new blog' })
@ApiResponse({ status: 201, description: 'The blog has been successfully created.' })
@ApiResponse({ status: 404, description: 'Author not found.' })
async create(@Body() createBlogDto: CreateBlogDto, @Res() res) {
const result = await this.blogService.create(createBlogDto);
return res.status(result.status).json(result);
}
@Post()
@ApiOperation({ summary: 'Create a new blog' })
@ApiResponse({
status: 201,
description: 'The blog has been successfully created.',
})
@ApiResponse({ status: 404, description: 'Author not found.' })
async create(@Body() createBlogDto: CreateBlogDto, @Res() res) {
const result = await this.blogService.create(createBlogDto);
return res.status(result.status).json(result);
}

@Get()
@ApiOperation({ summary: 'Find all blogs by region' })
@ApiResponse({ status: 200, description: 'Blogs retrieved successfully.' })
@ApiResponse({
status: 404,
description: 'No blogs found for the specified region.',
})
async findAllByRegion(@Query('region') region: string, @Res() res) {
const result = await this.blogService.findAll(region);
return res.status(result.status).json(result);
}

@Get()
@ApiOperation({ summary: 'Find all blogs by region' })
@ApiResponse({ status: 200, description: 'Blogs retrieved successfully.' })
@ApiResponse({ status: 404, description: 'No blogs found for the specified region.' })
async findAllByRegion(@Query('region') region: string, @Res() res) {
const result = await this.blogService.findAll(region);
return res.status(result.status).json(result);
}
@Get()
@ApiOperation({ summary: 'Find all blogs by region' })
@ApiResponse({ status: 200, description: 'Blogs retrieved successfully.' })
@ApiResponse({
status: 404,
description: 'No blogs found for the specified region.',
})
async findAllByLanguage(@Query('language') lang: string, @Res() res) {
const result = await this.blogService.findAll(lang);
return res.status(result.status).json(result);
}

@Get("/all")
@ApiOperation({ summary: 'Find all blogs' })
@ApiResponse({ status: 200, description: 'Blogs retrieved successfully.' })
@ApiResponse({ status: 404, description: 'No blogs found for the specified region.' })
async findAll(@Res() res) {
const result = await this.blogService.findAll("");
return res.status(result.status).json(result);
}
@Get('/all')
@ApiOperation({ summary: 'Find all blogs' })
@ApiResponse({ status: 200, description: 'Blogs retrieved successfully.' })
@ApiResponse({
status: 404,
description: 'No blogs found for the specified region.',
})
async findAll(@Res() res) {
const result = await this.blogService.findAll('');
return res.status(result.status).json(result);
}

@Get(':id')
@ApiOperation({ summary: 'Find a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog retrieved successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async findById(@Param('id') id: number, @Res() res) {
const result = await this.blogService.findById(+id);
return res.status(result.status).json(result);
}
@Get(':id')
@ApiOperation({ summary: 'Find a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog retrieved successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async findById(@Param('id') id: number, @Res() res) {
const result = await this.blogService.findById(+id);
return res.status(result.status).json(result);
}

@Put(':id')
@ApiOperation({ summary: 'Update a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog updated successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async update(@Param('id') id: number, @Body() updateBlogDto: UpdateBlogDto, @Res() res) {
const result = await this.blogService.update(+id, updateBlogDto);
return res.status(result.status).json(result);
}
@Put(':id')
@ApiOperation({ summary: 'Update a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog updated successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async update(
@Param('id') id: number,
@Body() updateBlogDto: UpdateBlogDto,
@Res() res,
) {
const result = await this.blogService.update(+id, updateBlogDto);
return res.status(result.status).json(result);
}

@Delete(':id')
@ApiOperation({ summary: 'Delete a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog deleted successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async delete(@Param('id') id: number, @Res() res) {
const result = await this.blogService.delete(+id);
return res.status(result.status).json(result);
}
@Delete(':id')
@ApiOperation({ summary: 'Delete a blog by ID' })
@ApiResponse({ status: 200, description: 'Blog deleted successfully.' })
@ApiResponse({ status: 404, description: 'Blog not found.' })
async delete(@Param('id') id: number, @Res() res) {
const result = await this.blogService.delete(+id);
return res.status(result.status).json(result);
}
}
Loading

0 comments on commit 1d62634

Please sign in to comment.