Compare commits
4 commits
f90ef12490
...
82e1a6258f
| Author | SHA1 | Date | |
|---|---|---|---|
| 82e1a6258f | |||
| 66778f9944 | |||
| 00d57b4feb | |||
| f0cf48fc87 |
3 changed files with 152 additions and 25 deletions
|
|
@ -1 +1,9 @@
|
||||||
export class ShowSchema {}
|
import { Schema } from 'mongoose';
|
||||||
|
|
||||||
|
export const ShowSchema = new Schema({
|
||||||
|
title: { type: String, required: true },
|
||||||
|
year: { type: Number, required: true },
|
||||||
|
seasons: { type: Number, required: true },
|
||||||
|
description: { type: String, required: true },
|
||||||
|
genres: [{ type: String, required: true }],
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,12 @@ import {
|
||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
Body,
|
Body,
|
||||||
Patch,
|
|
||||||
Param,
|
Param,
|
||||||
|
BadRequestException,
|
||||||
|
InternalServerErrorException,
|
||||||
|
NotFoundException,
|
||||||
|
Query,
|
||||||
|
Put,
|
||||||
Delete,
|
Delete,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ShowsService } from './shows.service';
|
import { ShowsService } from './shows.service';
|
||||||
|
|
@ -15,27 +19,131 @@ export class ShowsController {
|
||||||
constructor(private readonly showsService: ShowsService) {}
|
constructor(private readonly showsService: ShowsService) {}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() dto: ShowDto) {
|
async create(@Body() dto: ShowDto) {
|
||||||
return this.showsService.create(dto);
|
try {
|
||||||
|
await this.showsService.create(dto);
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Show was created successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new BadRequestException({
|
||||||
|
status: 'Bad request',
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
findAll() {
|
async findAll() {
|
||||||
return this.showsService.findAll();
|
try {
|
||||||
|
const shows = await this.showsService.findAll();
|
||||||
|
return { status: 'Ok', shows, totalShows: shows.length };
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get('/id/:id')
|
||||||
findOne(@Param('id') id: string) {
|
async findOne(@Param('id') id: string) {
|
||||||
return this.showsService.findOne(+id);
|
try {
|
||||||
|
const show = await this.showsService.findId(id);
|
||||||
|
if (show) {
|
||||||
|
return { status: 'Ok', show };
|
||||||
|
} else {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Get('search')
|
||||||
update(@Param('id') id: string, @Body() dto: ShowDto) {
|
async search(@Query('query') name: string) {
|
||||||
return this.showsService.update(+id, dto);
|
try {
|
||||||
|
const shows = await this.showsService.search(name);
|
||||||
|
if (shows.length > 0) {
|
||||||
|
return { status: 'Ok', show: shows, test: shows.length };
|
||||||
|
} else {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show matching ${name}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Put('id/:id')
|
||||||
remove(@Param('id') id: string) {
|
async update(@Param('id') id: string, @Body() dto: ShowDto) {
|
||||||
return this.showsService.remove(+id);
|
try {
|
||||||
|
const newShow = await this.showsService.update(id, dto);
|
||||||
|
if (!newShow) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Movie was updated successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('id/:id')
|
||||||
|
async remove(@Param('id') id: string) {
|
||||||
|
try {
|
||||||
|
const deletedMovie = await this.showsService.remove(id);
|
||||||
|
if (!deletedMovie) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Movie removed successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,36 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { ShowDto } from './dto/show.dto';
|
import { ShowDto } from './dto/show.dto';
|
||||||
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
|
import { Show } from './entities/show.entity';
|
||||||
|
import { Model } from 'mongoose';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ShowsService {
|
export class ShowsService {
|
||||||
create(dto: ShowDto) {
|
constructor(@InjectModel('Show') private showModel: Model<Show>) {}
|
||||||
return 'This action adds a new show';
|
|
||||||
|
async create(dto: ShowDto): Promise<any> {
|
||||||
|
const show = new this.showModel(dto);
|
||||||
|
return show.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
findAll() {
|
async findAll(): Promise<Show[]> {
|
||||||
return `This action returns all shows`;
|
return this.showModel.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
findOne(id: number) {
|
async findId(id: string): Promise<any> {
|
||||||
return `This action returns a #${id} show`;
|
return this.showModel.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(id: number, dto: ShowDto) {
|
async search(name: string) {
|
||||||
return `This action updates a #${id} show`;
|
const regex = new RegExp(name, 'i');
|
||||||
|
return this.showModel.find({ title: { $regex: regex } });
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(id: number) {
|
async update(id: string, dto: ShowDto): Promise<any> {
|
||||||
return `This action removes a #${id} show`;
|
return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(id: string): Promise<any> {
|
||||||
|
return this.showModel.findByIdAndDelete(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue