Skip to content

Commit

Permalink
feat: list all posts from blog
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrdsilva committed May 20, 2024
1 parent 0474d61 commit e10a70f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/entities/blog.entity.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import { Field, ID, Int, ObjectType } from '@nestjs/graphql';
import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';

@Entity()
@ObjectType()
export class Blog {
@PrimaryGeneratedColumn('uuid')
@Field(() => ID)
id: string;

@Column({ nullable: false })
@Field(() => String)
title: string;

@Column({ nullable: false })
@Field(() => String)
description: string;

@Column()
@Field(() => String)
filename: string;

@Column({ type: 'json', nullable: false })
@Field(() => String)
body: string;

@Column({ default: 0 })
@Field(() => Int)
views: number;

@Column({ type: 'json' })
@Field(() => [String])
tags: string[];

@CreateDateColumn({ name: 'created_at' })
@Field(() => Date)
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
@Field(() => Date)
updatedAt: Date;
}
3 changes: 2 additions & 1 deletion src/modules/blog.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { BlogController } from 'src/controllers/blog.controller';
import { Blog } from 'src/entities/blog.entity';
import { BlogService } from 'src/providers/blog.service';
import { StorageClientService } from 'src/providers/storage-client.service';
import { BlogResolver } from 'src/resolvers/blog.resolver';

@Module({
imports: [TypeOrmModule.forFeature([Blog])],
controllers: [BlogController],
providers: [BlogService, StorageClientService],
providers: [BlogService, StorageClientService, BlogResolver],
})
export class BlogModule {}
8 changes: 7 additions & 1 deletion src/providers/blog.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Blog } from 'src/entities/blog.entity';
import { CreateBlogInput } from 'src/models/dtos/create-blog.input';
import { Repository } from 'typeorm';
import { StorageClientService } from './storage-client.service';

@Injectable()
Expand All @@ -23,4 +23,10 @@ export class BlogService {

return await this.blogRepository.save(blog);
}

async find() {
const posts = await this.blogRepository.find();
console.log(posts)
return posts;
}
}
17 changes: 17 additions & 0 deletions src/resolvers/blog.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { UseGuards } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';
import { JwtAuthGuard } from 'src/auth/auth.guard';
import { Blog } from 'src/entities/blog.entity';
import { BlogService } from 'src/providers/blog.service';
import { File } from '../entities/file.entity';

@Resolver(() => File)
export class BlogResolver {
constructor(private readonly blogService: BlogService) {}

// @UseGuards(JwtAuthGuard)
@Query(() => [Blog])
findBlogPosts() {
return this.blogService.find();
}
}

0 comments on commit e10a70f

Please sign in to comment.