Skip to content

Commit

Permalink
Merge pull request #4 from fga-eps-mds/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
IanFPFerreira authored May 24, 2023
2 parents 477ff35 + 3aa1631 commit f4bd41a
Show file tree
Hide file tree
Showing 25 changed files with 932 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.ignore/
4 changes: 3 additions & 1 deletion .github/workflows/tutorial-cd-develop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: tutorial-cd-develop

# Just run on develop branch

on:
push:
branches: [ develop ]
Expand All @@ -17,4 +19,4 @@ jobs:
heroku_api_key: ${{secrets.HEROKU_API_KEY_HML}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME_HML}}
heroku_email: ${{secrets.HEROKU_EMAIL_HML}}
remote_branch: develop
branch: develop
2 changes: 1 addition & 1 deletion .github/workflows/tutorial-cd-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
heroku_api_key: ${{secrets.HEROKU_API_KEY_PRD}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME_PRD}}
heroku_email: ${{secrets.HEROKU_EMAIL_PRD}}
remote_branch: main
branch: main
16 changes: 16 additions & 0 deletions front-receive/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Front Receive</title>
<script src="script.js"></script>
</head>
<body>

<input type="text" name="id" id="id" placeholder="Name">
<button onclick="receiveFile()">Receive</button>

</body>
</html>
31 changes: 31 additions & 0 deletions front-receive/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const receiveFile = () => {
const id = document.getElementById('id').value;

const formData = new FormData();
formData.append('id', id);

// Get file id using XMLHttpRequest
const xhr = new XMLHttpRequest();

xhr.open('GET', 'http://localhost:3004/tutorials/'+id, true);
xhr.send(formData);

xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
const response = JSON.parse(this.responseText);

let blob1 = new Blob([new Uint8Array(response.data.data)],{type:'application/pdf'})

let blobUrl = URL.createObjectURL(blob1);

let link = document.createElement('a');

if('download' in link){
link.type = 'download'
link.href = blobUrl
link.download = response.filename
link.click()
}
}
}
}
18 changes: 18 additions & 0 deletions front-send/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Front send</title>
<script src="script.js"></script>
</head>
<body>
<form action="http://localhost:3004/tutorials" method="post" enctype="multipart/form-data">
<input type="text" name="name" id="name" placeholder="Name">
<input type="text" name="category_id" id="category_id" placeholder="Id da Categoria">
<input type="file" name="file" id="file">
<button type="submit">Send</button>
</form>
</body>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@types/multer": "^1.4.7",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^9.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import configuration from './configs/configuration';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CategoriesModule } from './categories/categories.module';
import { TutorialsModule } from './tutorials/tutorials.module';

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { CategoriesModule } from './categories/categories.module';
}),
CacheModule.register({ isGlobal: true }),
CategoriesModule,
TutorialsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
91 changes: 85 additions & 6 deletions src/categories/categories.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,104 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CategoriesController } from './categories.controller';
import { CategoriesService } from './categories.service';
import { v4 as uuidv4 } from 'uuid';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { CacheModule } from '@nestjs/common';


describe('CategoriesController', () => {
let controller: CategoriesController;
let categoriesController: CategoriesController;

const mockUuid = uuidv4();

const mockCreateCategoryDto: CreateCategoryDto = {
name: 'mock_category',
};

const mockUpdteCategoryDto: UpdateCategoryDto = {
name: 'new_mock_category',
};

const mockCategoriesService = {
createCategory: jest.fn((dto) => {
return {
...dto,
};
}),
findCategoryById: jest.fn((id) => {
return {
...mockCreateCategoryDto,
id,
};
}),
updateCategory: jest.fn((dto, id) => {
return {
...mockCreateCategoryDto,
...dto,
id,
};
}),
deleteCategory: jest.fn((id) => {
return;
}),
findCategories: jest.fn(() => {
return [{ ...mockCreateCategoryDto }];
}),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CategoriesController],
providers: [CategoriesService],
imports: [CacheModule.register()],
}).overrideProvider(CategoriesService)
.useValue({})
})
.overrideProvider(CategoriesService)
.useValue(mockCategoriesService)
.compile();

controller = module.get<CategoriesController>(CategoriesController);
categoriesController =
module.get<CategoriesController>(CategoriesController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
expect(categoriesController).toBeDefined();
});

it('should create a new category with success', async () => {
const dto = mockCreateCategoryDto;
const response = await categoriesController.createCategory(dto);

expect(response).toMatchObject({ ...dto });
});

it('should return an category with success', async () => {
const categoryId = mockUuid;
const response = await categoriesController.findCategoryById(categoryId);

expect(response).toMatchObject({ id: categoryId });
});

it('should return all categories with success', async () => {
const response = await categoriesController.findCategories();

expect(response.length).toBeGreaterThan(0);
expect(response).toEqual([{ ...mockCreateCategoryDto }]);
});

it('should update an category with success', async () => {
const categoryId = mockUuid;
const dto = mockUpdteCategoryDto;
const response = await categoriesController.updateCategory(dto, categoryId);

expect(response).toMatchObject({ ...dto, id: categoryId });
});

it('should delete an category with success', async () => {
const categoryId = mockUuid;
const successMessage = 'Categoria removida com sucesso';
const response = await categoriesController.deleteCategory(categoryId);

expect(response).toBe(successMessage);
});

});
21 changes: 15 additions & 6 deletions src/categories/categories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import {
Post,
Put,
UseInterceptors,
InternalServerErrorException,
} from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { Category } from './entities/category.entity';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { Category } from './entities/category.entity';

@Controller('categories')
@UseInterceptors(CacheInterceptor)
export class CategoriesController {
constructor(private readonly categoriesService: CategoriesService) {}
constructor(private categoriesService: CategoriesService) {}

@Get()
async findCategories(): Promise<Category[]> {
Expand All @@ -44,9 +45,17 @@ export class CategoriesController {

@Delete(':id')
async deleteCategory(@Param('id') id: string) {
await this.categoriesService.deleteCategory(id);
return {
message: 'Categoria removida com sucesso',
};
try {
await this.categoriesService.deleteCategory(id);
return 'Categoria removida com sucesso';
} catch (error) {
if (error.code === '23503') {
throw new InternalServerErrorException(
'Não é possível remover categorias associadas a tutoriais',
);
} else {
throw new InternalServerErrorException(error.message);
}
}
}
}
4 changes: 2 additions & 2 deletions src/categories/categories.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { CategoriesService } from './categories.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CategoriesController } from './categories.controller';
import { CategoriesService } from './categories.service';
import { Category } from './entities/category.entity';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [TypeOrmModule.forFeature([Category])],
Expand Down
Loading

0 comments on commit f4bd41a

Please sign in to comment.