Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Post" ADD COLUMN "estimated_time" INTEGER NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Post" ADD COLUMN "views" INTEGER NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- You are about to drop the column `views` on the `Post` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "Post" DROP COLUMN "views";
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ model Post {
coverImageLink String?
slug String @unique
previewImageLink String?
estimated_time Int @default(0)
authorId Int
categoryId Int
createdAt DateTime @default(now())
Expand Down
49 changes: 27 additions & 22 deletions src/modules/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import { Controller, Get, Param } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
} from '@nestjs/common';
import { PostsService } from './posts.service';
import { Post as PostModel } from '@prisma/client';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';

@Controller('posts')
export class PostsController {
constructor(private readonly postService: PostsService) {}

@Get('published')
async findPublishedPosts(): Promise<PostModel[]> {
return this.postService.posts({
where: { isPublished: true },
});
@Post()
async create(@Body() dto: CreatePostDto) {
return this.postService.createPost(dto);
}

@Get('filtered-posts/:searchString')
async findFilteredPosts(
@Param('searchString') searchString: string,
): Promise<PostModel[]> {
return this.postService.posts({
where: {
OR: [
{
title: { contains: searchString },
},
{
content: { contains: searchString },
},
],
},
});
@Get('published')
async findPublishedPosts(): Promise<PostModel[]> {
return this.postService.findPublishedPosts();
}

@Get(':slug')
async findPostBySlug(@Param('slug') slug: string): Promise<PostModel> {
return this.postService.findPostBySlug(slug);
}

@Patch(':slug')
update(@Param('slug') slug: string, @Body() dto: UpdatePostDto) {
return this.postService.updatePost(slug, dto);
}

@Delete(':slug')
remove(@Param('slug') slug: string) {
return this.postService.removePost(slug);
}
}
100 changes: 66 additions & 34 deletions src/modules/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../prisma.service';
import { Post, Prisma } from '@prisma/client';
import { Post } from '@prisma/client';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';

@Injectable()
export class PostsService {
constructor(private prisma: PrismaService) {}

private calculateEstimatedTime(content: string): number {
if (!content) {
new NotFoundException('Conteúdo não encontrado!');
return 0;
}

const countWords = content.trim().split(/\s+/).length;

return Math.ceil(countWords / 200);
}

async createPost(data: CreatePostDto): Promise<Post> {
const estimated_time = this.calculateEstimatedTime(data.content as string);

return this.prisma.post.create({
data: {
title: data.title,
previewContent: data.previewContent,
content: data.content,
slug: data.slug,
isPublished: data.isPublished,
coverImageLink: data.coverImageLink,
previewImageLink: data.previewImageLink,
estimated_time,
author: { connect: { id: data.authorId } },
category: { connect: { id: data.categoryId } },
},
});
}

async findPostBySlug(slug: string): Promise<Post | null> {
const post = await this.prisma.post.findUnique({
where: { slug },
Expand All @@ -24,26 +56,17 @@ export class PostsService {
});

if (!post) {
throw new NotFoundException('Post not found!');
throw new NotFoundException('Artigo não encontrado!');
}

return post;
}

async posts(params: {
skip?: number;
take?: number;
cursor?: Prisma.PostWhereUniqueInput;
where?: Prisma.PostWhereInput;
orderBy?: Prisma.PostOrderByWithRelationInput;
}): Promise<Post[]> {
const { skip, take, cursor, where, orderBy } = params;
return this.prisma.post.findMany({
skip,
take,
cursor,
where,
orderBy,
async findPublishedPosts(): Promise<Post[]> {
const posts = await this.prisma.post.findMany({
where: {
isPublished: true,
},
include: {
category: {
select: {
Expand All @@ -57,28 +80,37 @@ export class PostsService {
},
},
});
}

async createPost(data: Prisma.PostCreateInput): Promise<Post> {
return this.prisma.post.create({
data,
});
if (!posts) {
throw new NotFoundException('Artigos não encontrados!');
}

return posts;
}

async updatePost(params: {
where: Prisma.PostWhereUniqueInput;
data: Prisma.PostUpdateInput;
}): Promise<Post> {
const { data, where } = params;
return this.prisma.post.update({
data,
where,
});
async updatePost(slug: string, data: UpdatePostDto) {
const findPost = await this.findPostBySlug(slug);

if (!findPost) {
throw new NotFoundException(
`Não foi encontrado o artigo com o slug "${slug}"`,
);
}

return this.prisma.post.update({ where: { slug }, data });
}

async deletePost(where: Prisma.PostWhereUniqueInput): Promise<Post> {
return this.prisma.post.delete({
where,
});
async removePost(slug: string) {
const findPost = await this.findPostBySlug(slug);

if (!findPost) {
throw new NotFoundException(
`Não foi encontrado o artigo com o slug "${slug}"`,
);
}

await this.prisma.post.delete({ where: { slug } });

return { message: 'Artigo removido com sucesso!' };
}
}
Loading