forked from VitorCarvalho67/Boot
-
Notifications
You must be signed in to change notification settings - Fork 0
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 VitorCarvalho67#39 from Daniel-Alvarenga/main
Add activity routes on server
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
server/prisma/migrations/20240731003618_add_aluno_atividade_relation_table/migration.sql
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,14 @@ | ||
-- CreateTable | ||
CREATE TABLE `alunos_atividades` ( | ||
`alunoId` VARCHAR(191) NOT NULL, | ||
`atividadeId` VARCHAR(191) NOT NULL, | ||
`mencao` ENUM('I', 'R', 'B', 'MB') NOT NULL, | ||
|
||
PRIMARY KEY (`alunoId`, `atividadeId`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `alunos_atividades` ADD CONSTRAINT `alunos_atividades_alunoId_fkey` FOREIGN KEY (`alunoId`) REFERENCES `alunos`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `alunos_atividades` ADD CONSTRAINT `alunos_atividades_atividadeId_fkey` FOREIGN KEY (`atividadeId`) REFERENCES `atividades`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; |
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
45 changes: 45 additions & 0 deletions
45
server/src/modules/services/professor/CreateActivityUseCase.ts
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,45 @@ | ||
import { Professor } from "@prisma/client"; | ||
import { prisma } from "../../../prisma/client"; | ||
import { AppError } from "../../../errors/error"; | ||
import { CreateActivityDTO } from "../../interfaces/professorDTOs"; | ||
import { clearUploads } from "../shared/helpers/helpers"; | ||
import { uploadToMinio } from "../../../minioService"; | ||
|
||
|
||
export class CreateActivityUseCase { | ||
async execute({ title, descricao, professorId, imagem }: CreateActivityDTO) { | ||
if (!title || !descricao || !professorId || !imagem) { | ||
throw new AppError("Parâmetros insuficientes ou inválidos."); | ||
} | ||
|
||
const professor = await prisma.professor.findUnique({ | ||
where: { id: professorId } | ||
}); | ||
|
||
if (!professor) { | ||
throw new Error('Professor não encontrado'); | ||
} | ||
|
||
const bucketName = 'boot'; | ||
const objectName = `atividades/${professor.email}/title`; | ||
|
||
try { | ||
const filePath = imagem.path; | ||
await uploadToMinio(bucketName, objectName, filePath); | ||
|
||
clearUploads(); | ||
await prisma.atividade.create({ | ||
data: { | ||
title, | ||
descricao, | ||
professorId, | ||
imagem: objectName | ||
} | ||
}); | ||
|
||
return "Atividade criada com sucesso1"; | ||
} catch (error) { | ||
throw new AppError(`Error uploading image: ${error}`); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/modules/services/professor/LinkAlunoActivityUseCase.ts
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,53 @@ | ||
import { prisma } from "../../../prisma/client"; | ||
import { AppError } from "../../../errors/error"; | ||
import { RelateAlunoAtividadeDTO } from "../../interfaces/professorDTOs"; | ||
|
||
export class RelateAlunoAtividadeUseCase { | ||
async execute({ alunoId, atividadeId, professorId, mencao }: RelateAlunoAtividadeDTO) { | ||
if (!alunoId || !atividadeId || !professorId || !mencao) { | ||
throw new AppError("Parâmetros insuficientes ou inválidos."); | ||
} | ||
|
||
const atividade = await prisma.atividade.findUnique({ | ||
where: { id: atividadeId } | ||
}); | ||
|
||
if (!atividade) { | ||
throw new AppError("Atividade não encontrada."); | ||
} | ||
|
||
const professor = await prisma.professor.findUnique({ | ||
where: { id: professorId } | ||
}); | ||
|
||
if (!professor) { | ||
throw new AppError("Professor não encontrado."); | ||
} | ||
|
||
if(atividade.professorId != professorId){ | ||
throw new AppError("Professor inválido."); | ||
} | ||
|
||
const aluno = await prisma.aluno.findUnique({ | ||
where: { id: alunoId } | ||
}); | ||
|
||
if (!aluno) { | ||
throw new AppError("Aluno não encontrado."); | ||
} | ||
|
||
try { | ||
await prisma.alunoAtividade.create({ | ||
data: { | ||
alunoId, | ||
atividadeId, | ||
mencao | ||
} | ||
}); | ||
|
||
return "Aluno relacionado à atividade com sucesso."; | ||
} catch (error) { | ||
throw new AppError(`Erro ao relacionar aluno à atividade: ${error}`); | ||
} | ||
} | ||
} |
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