controller.ts 2.18 KB
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe } from '@nestjs/common'
import { CourseService } from './service'
import { serialize } from 'src/utils/serializer'
import { Authenticated } from 'src/decorators/authenticated'
import { Roles } from 'src/decorators/roles'
import { UserRole } from 'src/models/enum/UsersEnum'
import { CourseCreateDto, CourseItemDto, UpdateCourseDto } from './dto/CourseDto'
import { ACCOUNTID } from 'src/decorators/accountId'
import { ApiResponse, ApiTags } from '@nestjs/swagger'

@Controller('onboarding/courses')
@ApiTags('courses')
export class CourseController {
	constructor(private readonly coursesService: CourseService) {}

	@Post('/')
	@Authenticated()
	@Roles(UserRole.ADMIN, UserRole.COLLABORATOR)
	@ApiResponse({ type: CourseItemDto })
	async create(@ACCOUNTID() accountId: number, @Body() payload: CourseCreateDto) {
		const createdCourse = await this.coursesService.create(accountId, payload)
		return serialize(CourseItemDto, createdCourse)
	}

	@Get('/')
	@Authenticated()
	@Roles(UserRole.ADMIN, UserRole.COLLABORATOR)
	@ApiResponse({ type: CourseItemDto })
	async getAll(@ACCOUNTID() accountId: number) {
		const courses = await this.coursesService.findAll(accountId)
		return serialize(CourseItemDto, courses)
	}

	@Get('/:courseId')
	@Authenticated()
	@Roles(UserRole.ADMIN, UserRole.COLLABORATOR)
	@ApiResponse({ type: CourseItemDto })
	async getById(@ACCOUNTID() accountId: number, @Param('courseId', ParseIntPipe) courseId: number) {
		const course = await this.coursesService.findOne(accountId, courseId)
		return serialize(CourseItemDto, course)
	}

	@Patch('/:courseId')
	@Authenticated()
	@Roles(UserRole.ADMIN, UserRole.COLLABORATOR)
	@ApiResponse({ type: CourseItemDto })
	async update(
		@ACCOUNTID() accountId: number,
		@Param('courseId', ParseIntPipe) courseId: number,
		@Body() updateCourseDto: UpdateCourseDto
	) {
		return this.coursesService.update(accountId, courseId, updateCourseDto)
	}

	@Delete('/:courseId')
	@Authenticated()
	@Roles(UserRole.ADMIN, UserRole.COLLABORATOR)
	async remove(@ACCOUNTID() accountId: number, @Param('courseId', ParseIntPipe) courseId: number) {
		return this.coursesService.remove(accountId, courseId)
	}
}