-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add create and getAll endpoints for posts
- Loading branch information
1 parent
eccdced
commit f0c7691
Showing
8 changed files
with
122 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters