Compare commits
No commits in common. "02c2f373795b69db919f15871920e7b0e8094ae5" and "fbaed2c1d6e8e3026912210b06a698bf877fe89e" have entirely different histories.
02c2f37379
...
fbaed2c1d6
6 changed files with 15 additions and 84 deletions
|
|
@ -14,7 +14,7 @@
|
|||
};
|
||||
packages = rec {
|
||||
default = shows-api;
|
||||
shows-api = pkgs.callPackage ./package.nix { };
|
||||
shows-api = pkgs.callPackage ./package.nix {};
|
||||
};
|
||||
}
|
||||
);
|
||||
|
|
|
|||
10
package.nix
10
package.nix
|
|
@ -1,7 +1,7 @@
|
|||
{ lib
|
||||
, buildNpmPackage
|
||||
, nodejs
|
||||
,
|
||||
{
|
||||
lib,
|
||||
buildNpmPackage,
|
||||
nodejs,
|
||||
}:
|
||||
buildNpmPackage {
|
||||
name = "shows-api";
|
||||
|
|
@ -28,7 +28,7 @@ buildNpmPackage {
|
|||
meta = {
|
||||
description = "NestJS API to store info about shows on a MongoDB database";
|
||||
homepage = "https://git.everest.tailscale/Toast/shows-api";
|
||||
sourceProvenance = [ lib.sourceTypes.fromSource ];
|
||||
sourceProvenance = [lib.sourceTypes.fromSource];
|
||||
mainProgram = "shows-api";
|
||||
};
|
||||
}
|
||||
|
|
|
|||
28
src/main.ts
28
src/main.ts
|
|
@ -1,7 +1,6 @@
|
|||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { BadRequestException, Logger, ValidationPipe } from '@nestjs/common';
|
||||
import { ValidationError } from 'class-validator';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
|
@ -18,31 +17,6 @@ async function bootstrap() {
|
|||
credentials: true,
|
||||
origin,
|
||||
});
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
// https://stackoverflow.com/questions/75581669/customize-error-message-in-nest-js-using-class-validator
|
||||
exceptionFactory: (validationErrors: ValidationError[] = []) => {
|
||||
const errors = validationErrors.map((error) => ({
|
||||
error: Object.values(error.constraints).join(', '),
|
||||
}));
|
||||
|
||||
let errorMessage: string;
|
||||
errors.forEach((value, index) => {
|
||||
if (index == 0) {
|
||||
errorMessage = value.error;
|
||||
} else {
|
||||
errorMessage = errorMessage.concat(', ', value.error);
|
||||
}
|
||||
});
|
||||
|
||||
return new BadRequestException({
|
||||
status: 'Validation Error',
|
||||
message: errorMessage,
|
||||
});
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await app.listen(process.env.PORT);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
import { Type } from "class-transformer";
|
||||
import { IsNumber, IsOptional, Min } from "class-validator";
|
||||
|
||||
export class PaginationDto {
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@Min(1)
|
||||
limit?: number = 1;
|
||||
}
|
||||
|
|
@ -13,14 +13,6 @@ import {
|
|||
} from '@nestjs/common';
|
||||
import { ShowsService } from './shows.service';
|
||||
import { ShowDto } from './dto/show.dto';
|
||||
import { PaginationDto } from 'src/pagination.dto';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class SearchDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
query: string;
|
||||
}
|
||||
|
||||
@Controller('shows')
|
||||
export class ShowsController {
|
||||
|
|
@ -44,15 +36,10 @@ export class ShowsController {
|
|||
}
|
||||
|
||||
@Get()
|
||||
async findAll(@Query() pagination: PaginationDto) {
|
||||
async findAll() {
|
||||
try {
|
||||
const { page, limit } = pagination;
|
||||
const serviceResponse = await this.showsService.findAll(page, limit);
|
||||
return {
|
||||
status: 'Ok',
|
||||
shows: serviceResponse.shows,
|
||||
showCount: serviceResponse.showCount,
|
||||
};
|
||||
const shows = await this.showsService.findAll();
|
||||
return { status: 'Ok', shows, totalShows: shows.length };
|
||||
} catch (error) {
|
||||
throw new InternalServerErrorException({
|
||||
status: error.name,
|
||||
|
|
@ -85,16 +72,15 @@ export class ShowsController {
|
|||
}
|
||||
|
||||
@Get('search')
|
||||
async search(@Query() search: SearchDto) {
|
||||
async search(@Query('query') name: string) {
|
||||
try {
|
||||
const { query } = search;
|
||||
const shows = await this.showsService.search(query);
|
||||
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 ${query}`,
|
||||
message: `Can't find show matching ${name}`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -13,22 +13,8 @@ export class ShowsService {
|
|||
return show.save();
|
||||
}
|
||||
|
||||
async findAll(page: number, showsPerPage: number): Promise<any> {
|
||||
// Default value is 1 and I can't think any reason why you would want
|
||||
// a single item per page, so if showsPerPage is 1 then just don't do
|
||||
// any pagination at all
|
||||
let shows;
|
||||
if (showsPerPage != 1) {
|
||||
const skip = (page - 1) * showsPerPage;
|
||||
shows = await this.showModel.find().skip(skip).limit(showsPerPage);
|
||||
} else {
|
||||
shows = await this.showModel.find();
|
||||
}
|
||||
const total = await this.showModel.countDocuments();
|
||||
return {
|
||||
shows: shows,
|
||||
showCount: total,
|
||||
};
|
||||
async findAll(): Promise<Show[]> {
|
||||
return this.showModel.find();
|
||||
}
|
||||
|
||||
async findId(id: string): Promise<any> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue