forked from fga-eps-mds/2024.1-CALCULUS-StudioMaker
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from fga-eps-mds/feat#71/add-knowledge
[FEAT] Adiciona áreas de conhecimento (fga-eps-mds/2024.2-ARANDU-DOC#71)
- Loading branch information
Showing
11 changed files
with
416 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { IsHexColor, IsMongoId, IsNotEmpty, IsOptional, IsString, Matches } from "class-validator"; | ||
|
||
|
||
export class CreateKnowledgeDTO { | ||
@ApiProperty({ | ||
example: 'Nome da Área de Conhecimento', | ||
required: true | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
name: string; | ||
|
||
@ApiProperty({ | ||
example: 'Descrição da Área de Conhecimento', | ||
required: true | ||
}) | ||
@IsString() | ||
@IsNotEmpty() | ||
description: string; | ||
|
||
@IsOptional() | ||
@IsMongoId() | ||
user?: string; | ||
|
||
@ApiProperty({ | ||
example: 0, | ||
default: 0, | ||
required: false | ||
}) | ||
@IsOptional() | ||
order?: Number; | ||
|
||
@ApiProperty({ | ||
example: '#E0E0E0', | ||
default: '#E0E0E0' | ||
}) | ||
@IsString() | ||
@IsHexColor() | ||
@Matches("^#([0-9a-fA-F]{3}){1,2}$") | ||
@IsOptional() | ||
color?: string = '#E0E0E0'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { IsHexColor, IsOptional, IsString, Matches } from "class-validator"; | ||
|
||
|
||
export class UpdateKnowledgeDTO { | ||
@ApiProperty({ | ||
example: 'Nome da Área de Conhecimento', | ||
required: false | ||
}) | ||
@IsString() | ||
@IsOptional() | ||
name: string; | ||
|
||
@ApiProperty({ | ||
example: 'Descrição da Área de Conhecimento', | ||
required: false | ||
}) | ||
@IsString() | ||
@IsOptional() | ||
description: string; | ||
|
||
@ApiProperty({ | ||
example: 1, | ||
default: 0, | ||
required: false | ||
}) | ||
order?: Number; | ||
|
||
@ApiProperty({ | ||
example: '#E0E0E0', | ||
default: '#E0E0E0' | ||
}) | ||
@IsString() | ||
@IsHexColor() | ||
@Matches("^#([0-9a-fA-F]{3}){1,2}$") | ||
@IsOptional() | ||
color?: string = '#E0E0E0'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface UpdateKnowledgeOrderInterface { | ||
_id: string; | ||
name: string; | ||
description?: string; | ||
user?: string; | ||
subjects?: string[]; | ||
order: Number; | ||
createdAt: string; | ||
__v: number; | ||
updatedAt: string; | ||
} | ||
|
||
export class UpdateKnowledgeOrderDTO { | ||
knowledge: UpdateKnowledgeOrderInterface[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Req, UnauthorizedException } from "@nestjs/common"; | ||
import { ApiBody } from "@nestjs/swagger"; | ||
import { Request } from "express"; | ||
import { CreateKnowledgeDTO } from "./dtos/createKnowledge.dto"; | ||
import { UpdateKnowledgeDTO } from "./dtos/updateKnowledge.dto"; | ||
import { UpdateKnowledgeOrderDTO } from "./dtos/updateKnowledgeOrder.dto"; | ||
import { KnowledgeService } from "./knowledge.service"; | ||
|
||
|
||
@Controller('knowledges') | ||
export class KnowledgeController { | ||
constructor(private readonly knowledgeService: KnowledgeService) { } | ||
|
||
@ApiBody({ | ||
type: CreateKnowledgeDTO, | ||
description: 'Endpoint para a criação de uma nova Área de Conhecimento' | ||
}) | ||
@Post() | ||
async create( | ||
@Body() createKnowledgeDTO: CreateKnowledgeDTO, | ||
@Req() req: Request | ||
) { | ||
const authHeader = req.headers.authorization as string; | ||
const token = authHeader?.split(' ')[1]; | ||
|
||
if (!token) throw new UnauthorizedException(`Token not found!`); | ||
|
||
return this.knowledgeService.create(createKnowledgeDTO, token); | ||
} | ||
|
||
@Get() | ||
async getAllKnowledge() { | ||
return this.knowledgeService.getAllKnowledge(); | ||
} | ||
|
||
@Get('users/:id') | ||
async getByUser(@Param('id') userId: string) { | ||
return this.knowledgeService.getByUserId(userId); | ||
} | ||
|
||
@Get(':id') | ||
async getById(@Param('id') id: string) { | ||
return this.knowledgeService.getById(id); | ||
} | ||
|
||
@ApiBody({ | ||
type: UpdateKnowledgeDTO, | ||
description: 'Endpoint para a atualização total ou parcial de uma Área de Conhecimento' | ||
}) | ||
@Patch(':id') | ||
async update( | ||
@Param('id') id: string, | ||
@Body() updateKnowledgeDTO: UpdateKnowledgeDTO, | ||
) { | ||
return this.knowledgeService.update(id, updateKnowledgeDTO); | ||
} | ||
|
||
@Delete(':id') | ||
async delete(@Param('id') id: string) { | ||
return this.knowledgeService.delete(id); | ||
} | ||
|
||
@Put(':id/subjects/:subjectId') | ||
async addSubjectToKnowledge( | ||
@Param('id') id: string, | ||
@Param('subjectId') subjectId: string | ||
) { | ||
return this.knowledgeService.addSubjectToKnowledge(id, subjectId); | ||
} | ||
|
||
@Get(':id/subjects') | ||
async getSubjectsByKnowledgeId(@Param('id') knowledgeId: string) { | ||
try { | ||
return await this.knowledgeService.getSubjectsByKnowledgeId(knowledgeId); | ||
} catch (error) { | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
@ApiBody({ | ||
type: UpdateKnowledgeOrderDTO, | ||
}) | ||
@Patch('/order') | ||
async updateKnowledgeOrder( | ||
@Body() | ||
body: UpdateKnowledgeOrderDTO, | ||
) { | ||
return await this.knowledgeService.updateOrder(body.knowledge); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HttpModule } from "@nestjs/axios"; | ||
import { forwardRef, Module } from "@nestjs/common"; | ||
import { MongooseModule } from "@nestjs/mongoose"; | ||
import { SubjectModule } from "../subject/subject.module"; | ||
import { SubjectSchema } from "../subject/subject.schema"; | ||
import { KnowledgeController } from "./knowledge.controller"; | ||
import { KnowledgeSchema } from "./knowledge.schema"; | ||
import { KnowledgeService } from "./knowledge.service"; | ||
|
||
|
||
@Module({ | ||
imports: [ | ||
HttpModule, | ||
MongooseModule.forFeature([{ name: 'Knowledge', schema: KnowledgeSchema }]), | ||
MongooseModule.forFeature([{ name: 'Subject', schema: SubjectSchema }]), | ||
forwardRef(() => SubjectModule), | ||
], | ||
providers: [KnowledgeService], | ||
controllers: [KnowledgeController], | ||
exports: [KnowledgeService] | ||
}) | ||
export class KnowledgeModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const colorValidator = (v: string) => new RegExp("^#([0-9a-fA-F]{3}){1,2}$").test(v); | ||
|
||
export const KnowledgeSchema = new mongoose.Schema( | ||
{ | ||
name: { type: String, required: true }, | ||
description: { type: String, required: true }, | ||
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, | ||
subjects: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Subject' }], | ||
order: { type: Number, default: 0 }, | ||
color: { type: String, validator: [colorValidator, 'HexCode Invalido!'], default: '#e0e0e0' } | ||
}, | ||
{ timestamps: true, collection: 'knowledges' } | ||
); | ||
|
||
export interface Knowledge extends mongoose.Document { | ||
name: string; | ||
description: string; | ||
user: mongoose.Schema.Types.ObjectId; | ||
subjects?: mongoose.Types.ObjectId[]; | ||
order?: Number; | ||
color?: string; | ||
} |
Oops, something went wrong.