Skip to content

Commit

Permalink
feat: extras
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedied committed Mar 13, 2020
1 parent e5bc949 commit 11a7da1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion funpets-server/main/function.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"bindings": [
{
"authLevel": "anonymous",
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
Expand Down
32 changes: 32 additions & 0 deletions funpets-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion funpets-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
"@azure/functions": "^1.0.3",
"@nestjs/azure-database": "^1.0.0",
"@nestjs/azure-func-http": "^0.4.1",
"@nestjs/azure-storage": "^2.1.0",
"@nestjs/azure-storage": "^2.1.1",
"@nestjs/common": "^6.10.14",
"@nestjs/core": "^6.10.14",
"@nestjs/platform-express": "^6.10.14",
"@nestjs/typeorm": "^6.3.4",
"mongodb": "^3.5.5",
"class-transformer": "^0.2.3",
"class-validator": "^0.11.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4",
Expand Down
5 changes: 3 additions & 2 deletions funpets-server/src/main.azure.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

export async function createApp(): Promise<INestApplication> {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');

app.useGlobalPipes(new ValidationPipe());

await app.init();
return app;
}
3 changes: 3 additions & 0 deletions funpets-server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.useGlobalPipes(new ValidationPipe());

await app.listen(3000);
}
bootstrap();
19 changes: 19 additions & 0 deletions funpets-server/src/stories/stories.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import { Test, TestingModule } from '@nestjs/testing';
import { StoriesController } from './stories.controller';
import { AzureStorageService } from '@nestjs/azure-storage';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Story } from './story.entity';

jest.mock('@nestjs/azure-storage', () => ({
// Use Jest automatic mock generation
...jest.genMockFromModule('@nestjs/azure-storage'),

// Interceptor mock needs to be done manually
AzureStorageFileInterceptor: jest.fn(() => ({
intercept: (context, next) => next.handle(),
})),
}));

const mockRepository = jest.genMockFromModule<any>('typeorm').MongoRepository;

describe('Stories Controller', () => {
let controller: StoriesController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [StoriesController],
providers: [
AzureStorageService,
{ provide: getRepositoryToken(Story), useValue: mockRepository },
],
}).compile();

controller = module.get<StoriesController>(StoriesController);
Expand Down
9 changes: 8 additions & 1 deletion funpets-server/src/stories/stories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Body,
UploadedFile,
UnsupportedMediaTypeException,
BadRequestException,
} from '@nestjs/common';
import {
AzureStorageFileInterceptor,
Expand All @@ -17,7 +18,9 @@ import {
import { InjectRepository } from '@nestjs/typeorm';
import { MongoRepository } from 'typeorm';
import { ObjectID } from 'mongodb';
import { Validator } from 'class-validator';
import { Story } from './story.entity';
import { StoryDto } from './story.dto';

// Some cat facts, courtesy of https://catfact.ninja
const funFacts = [
Expand Down Expand Up @@ -89,7 +92,7 @@ export class StoriesController {
@UseInterceptors(AzureStorageFileInterceptor('file', fileUploadOptions))
async createStory(
@Body()
data: Partial<Story>,
data: StoryDto,
@UploadedFile()
file: UploadedFileMetadata,
): Promise<Story> {
Expand All @@ -100,6 +103,10 @@ export class StoriesController {
if (file) {
story.imageUrl = file.storageUrl || null;
}
const validator = new Validator();
if (validator.isEmpty(story.description) && validator.isEmpty(story.imageUrl)) {
throw new BadRequestException('Either description or image file must be provided');
}
return await this.storiesRepository.save(story);
}
}
14 changes: 14 additions & 0 deletions funpets-server/src/stories/story.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IsNotEmpty, IsOptional, IsDate, MaxLength } from 'class-validator';

export class StoryDto {
@IsNotEmpty()
animal: string;

@IsOptional()
@MaxLength(240)
description: string;

@IsOptional()
@IsDate()
createdAt: Date;
}

0 comments on commit 11a7da1

Please sign in to comment.