Fixes and adjustments to demo

This commit is contained in:
Toni Koskinen
2026-03-27 10:37:42 +02:00
parent 51a4b84476
commit 4de890ba77
44 changed files with 1076 additions and 104 deletions

View File

@@ -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;

View File

@@ -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;
}
}

View 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);
}
}

View File

@@ -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 {}

View 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 } });
}
}