Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Adiciona áreas de conhecimento (fga-eps-mds/2024.2-ARANDU-DOC#71) #17

Merged
merged 1 commit into from
Feb 1, 2025
Merged
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.PHONY: build
build:
docker-compose build --no-cache
docker-compose build

.PHONY: start
start:
docker-compose up

.PHONY: run
run:
docker-compose build --no-cache && docker-compose up
docker-compose build && docker-compose up

.PHONY: stop
stop:
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 { MongooseModule } from '@nestjs/mongoose';
import * as Joi from 'joi';
import { ContentModule } from './content/content.module';
import { JourneyModule } from './journey/journey.module';
import { KnowledgeModule } from './knowledge/knowledge.module';
import { SubjectModule } from './subject/subject.module';
import { TrailModule } from './trail/trail.module';

Expand All @@ -25,6 +26,7 @@ import { TrailModule } from './trail/trail.module';
inject: [ConfigService],
}),
HttpModule,
KnowledgeModule,
SubjectModule,
ContentModule,
JourneyModule,
Expand Down
43 changes: 43 additions & 0 deletions src/knowledge/dtos/createKnowledge.dto.ts
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';
}
38 changes: 38 additions & 0 deletions src/knowledge/dtos/updateKnowledge.dto.ts
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';
}
15 changes: 15 additions & 0 deletions src/knowledge/dtos/updateKnowledgeOrder.dto.ts
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[];
}
91 changes: 91 additions & 0 deletions src/knowledge/knowledge.controller.ts
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);
}
}
22 changes: 22 additions & 0 deletions src/knowledge/knowledge.module.ts
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 { }
24 changes: 24 additions & 0 deletions src/knowledge/knowledge.schema.ts
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;
}
Loading
Loading