Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions desafio-fullstack-backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
39 changes: 39 additions & 0 deletions desafio-fullstack-backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# compiled output
/dist
/node_modules

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

.env

package-lock.json
yarn.lock

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions desafio-fullstack-backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
Binary file added desafio-fullstack-backend/Diagrama ER.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions desafio-fullstack-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# DesafioFullstackBackend

## Instalação

```bash
$ npm install
```

## Inicialização

```bash
# development
$ npm run start
```

## Conexão com o banco de dados
- Crie um arquivo `.env`
- Adicione as seguintes configurações:

```bash
DATABASE_HOST=
DATABASE_PORT=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_NAME=
```

## Criar banco de dados
- Automático
```bash
Ao iniciar o projeto, as tabelas vão ser automaticamente criadas.
```
- Manual
```bash
Importe o arquivo create-database.sql em seu pgadmin (ou algum outro que esteja utilizando).
```
[Arquivo para criar banco de dado](./create-database.sql)
84 changes: 84 additions & 0 deletions desafio-fullstack-backend/create-database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
BEGIN;


CREATE TABLE public.classroom
(
id integer NOT NULL,
name character varying NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO public.classroom(
id, name)
VALUES (1, 'Sala 402');

CREATE TABLE public.course
(
id integer NOT NULL,
name character varying NOT NULL,
"startTime" character varying NOT NULL,
"endTime" character varying NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE public.course_classroom
(
"courseId" integer NOT NULL,
"classroomId" integer NOT NULL,
PRIMARY KEY ("courseId", "classroomId")
);

CREATE TABLE public.course_teacher
(
"courseId" integer NOT NULL,
"teacherId" integer NOT NULL,
PRIMARY KEY ("courseId", "teacherId")
);

CREATE TABLE public.teacher
(
id integer NOT NULL,
name character varying NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO public.teacher(
id, name)
VALUES (1, 'Pedro Henrique');

CREATE TABLE public."user"
(
id integer NOT NULL,
email character varying NOT NULL,
password character varying NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO public."user"(
id, email, password)
VALUES (1, 'user@test.com', 'testpass');

ALTER TABLE public.course_classroom
ADD FOREIGN KEY ("classroomId")
REFERENCES public.classroom (id)
NOT VALID;


ALTER TABLE public.course_classroom
ADD FOREIGN KEY ("courseId")
REFERENCES public.course (id)
NOT VALID;


ALTER TABLE public.course_teacher
ADD FOREIGN KEY ("teacherId")
REFERENCES public.teacher (id)
NOT VALID;


ALTER TABLE public.course_teacher
ADD FOREIGN KEY ("courseId")
REFERENCES public.course (id)
NOT VALID;

END;
4 changes: 4 additions & 0 deletions desafio-fullstack-backend/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
77 changes: 77 additions & 0 deletions desafio-fullstack-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"name": "desafio-fullstack-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^7.6.15",
"@nestjs/config": "^0.6.3",
"@nestjs/core": "^7.6.15",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^7.6.15",
"@nestjs/typeorm": "^7.1.5",
"@types/passport": "^1.0.6",
"dotenv": "^10.0.0",
"pg": "^8.6.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.6",
"typeorm": "^0.2.32"
},
"devDependencies": {
"@nestjs/cli": "^7.6.0",
"@nestjs/schematics": "^7.3.0",
"@nestjs/testing": "^7.6.15",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.36",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.1.3",
"ts-jest": "^26.5.4",
"ts-loader": "^8.0.18",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
22 changes: 22 additions & 0 deletions desafio-fullstack-backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});
12 changes: 12 additions & 0 deletions desafio-fullstack-backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}
38 changes: 38 additions & 0 deletions desafio-fullstack-backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { UserEntity } from './user/entity/user.entity';
import { CourseModule } from './course/course.module';
import { TeacherModule } from './teacher/teacher.module';
import { TeacherEntity } from './teacher/entity/teacher.entity';
import { ClassroomModule } from './classroom/classroom.module';
import { ClassroomEntity } from './classroom/entity/classroom.entity';
import { CourseEntity } from './course/entity/course.entity';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
entities: [UserEntity, TeacherEntity, ClassroomEntity, CourseEntity]
}),
AuthModule,
UserModule,
CourseModule,
TeacherModule,
ClassroomModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
8 changes: 8 additions & 0 deletions desafio-fullstack-backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
26 changes: 26 additions & 0 deletions desafio-fullstack-backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Body, Controller, HttpStatus, Post, Res } from '@nestjs/common';

import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';

@Controller('auth')
export class AuthController {

constructor(private authService: AuthService) { }

@Post('/login')
async login(@Body() loginDto: LoginDto, @Res() response) {

try {
const result = await this.authService.login(loginDto)
if (!result) {
return response.status(HttpStatus.FORBIDDEN).send('invalid credentials')
}

return response.status(HttpStatus.OK).send(result);
} catch (error) {
return response.status(HttpStatus.INTERNAL_SERVER_ERROR).send(error);
}
}

}
Loading