-
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.
- Loading branch information
1 parent
8a74ea8
commit 501c963
Showing
8 changed files
with
107 additions
and
15 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
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,85 @@ | ||
import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' | ||
import { makeInstructorMapper } from '@/infra/database/prisma/mappers/factories/make-instructor-mapper' | ||
import { makeStudentMapper } from '@/infra/database/prisma/mappers/factories/make-student-mapper' | ||
import { makeEditInstructorDetailsUseCase } from '@/infra/use-cases/factories/make-edit-instructor-details-use-case' | ||
import { makeEditStudentDetailsUseCase } from '@/infra/use-cases/factories/make-edit-student-details-use-case' | ||
import { type FastifyReply, type FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
import { UserPresenter } from '../presenters/user-presenter' | ||
|
||
const editUserDetailsBodySchema = z.object({ | ||
email: z.string().email().optional(), | ||
age: z.number().optional(), | ||
summary: z.string().optional(), | ||
profileImageKey: z.string().optional(), | ||
bannerImageKey: z.string().optional() | ||
}) | ||
|
||
export async function editUserDetailsController(request: FastifyRequest, reply: FastifyReply) { | ||
const { email, age, summary, bannerImageKey, profileImageKey } = editUserDetailsBodySchema.parse(request.body) | ||
const { role } = request.user | ||
|
||
if (role === 'STUDENT') { | ||
const editStudentDetailsUseCase = makeEditStudentDetailsUseCase() | ||
|
||
console.log(request.user) | ||
|
||
const result = await editStudentDetailsUseCase.exec({ | ||
email, | ||
age, | ||
summary, | ||
bannerImageKey, | ||
profileImageKey, | ||
studentId: request.user.sub | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case ResourceNotFoundError: | ||
return await reply.status(404).send({ message: error.message }) | ||
default: | ||
return await reply.status(500).send({ message: error.message }) | ||
} | ||
} | ||
|
||
const studentMapper = makeStudentMapper() | ||
const user = await studentMapper.toPrisma(result.value.student) | ||
|
||
return await reply.status(200).send({ | ||
user: UserPresenter.toHTTP(user) | ||
}) | ||
} | ||
|
||
if (role === 'INSTRUCTOR') { | ||
const EditInstructorDetailsUseCase = makeEditInstructorDetailsUseCase() | ||
|
||
const result = await EditInstructorDetailsUseCase.exec({ | ||
email, | ||
age, | ||
summary, | ||
bannerImageKey, | ||
profileImageKey, | ||
instructorId: request.user.sub | ||
}) | ||
|
||
if (result.isLeft()) { | ||
const error = result.value | ||
|
||
switch (error.constructor) { | ||
case ResourceNotFoundError: | ||
return await reply.status(404).send({ message: error.message }) | ||
default: | ||
return await reply.status(500).send({ message: error.message }) | ||
} | ||
} | ||
|
||
const instructorMapper = makeInstructorMapper() | ||
const user = await instructorMapper.toPrisma(result.value.instructor) | ||
|
||
return await reply.status(200).send({ | ||
user: UserPresenter.toHTTP(user) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { type FastifyInstance } from 'fastify' | ||
import { authenticateController } from '../controllers/authenticate' | ||
import { authenticateUserController } from '../controllers/authenticate' | ||
import { editUserDetailsController } from '../controllers/edit-user-details' | ||
import { getUserDetailsController } from '../controllers/get-user-details' | ||
import { registerUserController } from '../controllers/register-user' | ||
import { verifyJwt } from '../middlewares/verify-jwt' | ||
|
||
export async function userRoutes(app: FastifyInstance) { | ||
app.post('/users', registerUserController) | ||
app.post('/sessions', authenticateController) | ||
app.post('/sessions', authenticateUserController) | ||
|
||
app.put('/users', { onRequest: verifyJwt }, editUserDetailsController) | ||
|
||
app.get('/users/:userId', getUserDetailsController) | ||
} |
File renamed without changes.