Compare commits

..

No commits in common. "82e1a6258fc7704395233c23c79050204dbd1992" and "f90ef12490cf5ba5cdd622108772a2df350ebe6c" have entirely different histories.

3 changed files with 25 additions and 152 deletions

View file

@ -1,9 +1 @@
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 }],
});
export class ShowSchema {}

View file

@ -3,12 +3,8 @@ import {
Get,
Post,
Body,
Patch,
Param,
BadRequestException,
InternalServerErrorException,
NotFoundException,
Query,
Put,
Delete,
} from '@nestjs/common';
import { ShowsService } from './shows.service';
@ -19,131 +15,27 @@ export class ShowsController {
constructor(private readonly showsService: ShowsService) {}
@Post()
async create(@Body() dto: ShowDto) {
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,
});
}
create(@Body() dto: ShowDto) {
return this.showsService.create(dto);
}
@Get()
async 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,
});
}
findAll() {
return this.showsService.findAll();
}
@Get('/id/:id')
async findOne(@Param('id') id: string) {
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,
});
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.showsService.findOne(+id);
}
@Get('search')
async search(@Query('query') name: string) {
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,
});
}
@Patch(':id')
update(@Param('id') id: string, @Body() dto: ShowDto) {
return this.showsService.update(+id, dto);
}
@Put('id/:id')
async update(@Param('id') id: string, @Body() dto: ShowDto) {
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,
});
}
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.showsService.remove(+id);
}
}

View file

@ -1,36 +1,25 @@
import { Injectable } from '@nestjs/common';
import { ShowDto } from './dto/show.dto';
import { InjectModel } from '@nestjs/mongoose';
import { Show } from './entities/show.entity';
import { Model } from 'mongoose';
@Injectable()
export class ShowsService {
constructor(@InjectModel('Show') private showModel: Model<Show>) {}
async create(dto: ShowDto): Promise<any> {
const show = new this.showModel(dto);
return show.save();
create(dto: ShowDto) {
return 'This action adds a new show';
}
async findAll(): Promise<Show[]> {
return this.showModel.find();
findAll() {
return `This action returns all shows`;
}
async findId(id: string): Promise<any> {
return this.showModel.findById(id);
findOne(id: number) {
return `This action returns a #${id} show`;
}
async search(name: string) {
const regex = new RegExp(name, 'i');
return this.showModel.find({ title: { $regex: regex } });
update(id: number, dto: ShowDto) {
return `This action updates a #${id} show`;
}
async update(id: string, dto: ShowDto): Promise<any> {
return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true });
}
async remove(id: string): Promise<any> {
return this.showModel.findByIdAndDelete(id);
remove(id: number) {
return `This action removes a #${id} show`;
}
}