Fixes and adjustments to demo
This commit is contained in:
@@ -9,6 +9,9 @@ import { AppModule } from './app/app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.enableCors({
|
||||
origin: ['http://localhost:4200'],
|
||||
});
|
||||
const globalPrefix = 'api';
|
||||
app.setGlobalPrefix(globalPrefix);
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
|
||||
import { Post } from '@shared/prisma-generated/src/types';
|
||||
import { prisma } from '@shared/prisma-generated/src/prisma';
|
||||
|
||||
@Controller('posts')
|
||||
export class PostsController {
|
||||
@Get()
|
||||
async findAll(): Promise<Post[]> {
|
||||
const posts = await prisma.post.findMany();
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findById(@Param('id') id: string): Promise<Post> {
|
||||
const post = await prisma.post.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!post) {
|
||||
throw new NotFoundException(`Post with id ${id} not found`);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
}
|
||||
41
apps/api/src/modules/users/posts.controller.ts
Normal file
41
apps/api/src/modules/users/posts.controller.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Controller, Get, Param, Post, Body, Put, Delete, NotFoundException } from '@nestjs/common';
|
||||
import { PostsService } from './posts.service';
|
||||
import { Post as PostEntity } from '@shared/prisma-generated/src/types';
|
||||
import { PostDto } from '@shared/shared-dto';
|
||||
import { UpdatePostDto } from '@shared/shared-dto';
|
||||
|
||||
@Controller('posts')
|
||||
export class PostsController {
|
||||
constructor(private readonly postsService: PostsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll(): Promise<PostEntity[]> {
|
||||
const posts = this.postsService.findAll();
|
||||
|
||||
if (!posts) {
|
||||
throw new NotFoundException("No posts found")
|
||||
}
|
||||
|
||||
return posts
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findById(@Param('id') id: string): Promise<PostEntity> {
|
||||
return this.postsService.findById(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: PostDto): Promise<PostEntity> {
|
||||
return this.postsService.create(data);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdatePostDto): Promise<PostEntity> {
|
||||
return this.postsService.update(id, data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string): Promise<PostEntity> {
|
||||
return this.postsService.delete(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PostsController } from './controllers/posts/posts.controller';
|
||||
import { PostsController } from './posts.controller';
|
||||
import { PostsService } from './posts.service';
|
||||
|
||||
@Module({
|
||||
controllers: [PostsController],
|
||||
providers: [PostsService],
|
||||
exports: [PostsService],
|
||||
})
|
||||
export class PostsModule {}
|
||||
export class PostsModule {}
|
||||
40
apps/api/src/modules/users/posts.service.ts
Normal file
40
apps/api/src/modules/users/posts.service.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Post } from '@shared/prisma-generated/src/types';
|
||||
import { prisma } from '@shared/prisma-generated/src/prisma';
|
||||
import { PostDto } from '@shared/shared-dto';
|
||||
import { UpdatePostDto } from '@shared/shared-dto';
|
||||
|
||||
@Injectable()
|
||||
export class PostsService {
|
||||
async findAll(): Promise<Post[]> {
|
||||
const posts = await prisma.post.findMany();
|
||||
return posts;
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Post> {
|
||||
const post = await prisma.post.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!post) {
|
||||
throw new NotFoundException(`Post with id ${id} not found`);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
async create(data: PostDto): Promise<Post> {
|
||||
return prisma.post.create({ data });
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdatePostDto): Promise<Post> {
|
||||
return prisma.post.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<Post> {
|
||||
return prisma.post.delete({ where: { id } });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user