Skip to content

Commit

Permalink
feat: add create and getAll endpoints for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Frost committed May 6, 2024
1 parent eccdced commit f0c7691
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 48 deletions.
51 changes: 50 additions & 1 deletion src/posts/dto/create-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
export class CreatePostDto {}
import { Transform, Type } from 'class-transformer';
import {
IsDecimal,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';

class StickerDto {
@IsString()
@IsNotEmpty()
name: string;

@IsDecimal()
x: number;

@IsDecimal()
y: number;

@IsInt()
@Transform(({ value }) => parseInt(value))
rotation: number;
}

class LocationDto {
@IsDecimal()
latitude: number;

@IsDecimal()
longitude: number;
}

export class CreatePostDto {
@IsString()
@IsNotEmpty()
caption: string;

@IsOptional()
@IsNotEmpty()
@ValidateNested({ each: true })
@Type(() => StickerDto)
stickers?: StickerDto[];

@IsNotEmpty()
@ValidateNested()
@Type(() => LocationDto)
location: LocationDto;
}
4 changes: 0 additions & 4 deletions src/posts/dto/update-post.dto.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/posts/entities/post-location.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Embeddable, Property } from '@mikro-orm/core';

@Embeddable()
export class PostLocation {
@Property()
@Property({ columnType: 'float' })
latitude: number;

@Property()
@Property({ columnType: 'float' })
longitude: number;
}
6 changes: 3 additions & 3 deletions src/posts/entities/post-sticker.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Embeddable, Property } from '@mikro-orm/core';
@Embeddable()
export class PostSticker {
@Property()
id: number;
name: string;

@Property()
@Property({ columnType: 'float' })
x: number;

@Property()
@Property({ columnType: 'float' })
y: number;

@Property()
Expand Down
59 changes: 39 additions & 20 deletions src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { PostsService } from './posts.service';
import {
Body,
Controller,
FileTypeValidator,
Get,
ParseFilePipe,
Post,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { randomUUID } from 'crypto';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { User, UserPayload } from 'src/auth/decorators/user.decorator';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { PostsService } from './posts.service';

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

@Post()
create(@Body() createPostDto: CreatePostDto) {
return this.postsService.create(createPostDto);
@UseInterceptors(
FileInterceptor('image', {
storage: diskStorage({
destination: './uploads',
filename: (_, file, cb) => {
const filename = randomUUID() + extname(file.originalname);
cb(null, filename);
},
}),
}),
)
create(
@User() user: UserPayload,
@Body() createPostDto: CreatePostDto,
@UploadedFile(
new ParseFilePipe({
validators: [new FileTypeValidator({ fileType: 'image/*' })],
fileIsRequired: false,
}),
)
file: Express.Multer.File,
) {
return this.postsService.create(createPostDto, user, file);
}

@Get()
findAll() {
return this.postsService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.postsService.findOne(+id);
}

@Patch(':id')
update(@Param('id') id: string, @Body() updatePostDto: UpdatePostDto) {
return this.postsService.update(+id, updatePostDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.postsService.remove(+id);
}
}
5 changes: 4 additions & 1 deletion src/posts/posts.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common';
import { PostsService } from './posts.service';
import { Post } from './entities/post.entity';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';

@Module({
imports: [MikroOrmModule.forFeature([Post])],
controllers: [PostsController],
providers: [PostsService],
})
Expand Down
40 changes: 24 additions & 16 deletions src/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { EntityRepository } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { EntityManager } from '@mikro-orm/postgresql';
import { Injectable } from '@nestjs/common';
import { UserPayload } from 'src/auth/decorators/user.decorator';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { Post } from './entities/post.entity';

@Injectable()
export class PostsService {
create(createPostDto: CreatePostDto) {
return 'This action adds a new post';
}

findAll() {
return `This action returns all posts`;
}
constructor(
@InjectRepository(Post)
private readonly postRepository: EntityRepository<Post>,
private readonly em: EntityManager,
) {}

findOne(id: number) {
return `This action returns a #${id} post`;
async create(
createPostDto: CreatePostDto,
user: UserPayload,
file: Express.Multer.File,
) {
const post = this.postRepository.create({
...createPostDto,
imageUrl: file ? `/uploads/${file.filename}` : undefined,
author: user.sub,
});
await this.em.flush();
return post;
}

update(id: number, updatePostDto: UpdatePostDto) {
return `This action updates a #${id} post`;
}

remove(id: number) {
return `This action removes a #${id} post`;
findAll() {
return this.postRepository.findAll({ populate: ['author'] });
}
}
1 change: 0 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class UsersController {
update(
@User() user: UserPayload,
@Param('id') id: string,

@Body() updateUserDto: UpdateUserDto,
@UploadedFile(
new ParseFilePipe({
Expand Down

0 comments on commit f0c7691

Please sign in to comment.