From b0322e952286f7ba275db278d7dea25f895ec4a7 Mon Sep 17 00:00:00 2001 From: Ron Azar Date: Thu, 26 Sep 2024 08:45:23 +0300 Subject: [PATCH] Phase 2 - Step 4.2: complete FeedbackResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit •Implemented API endpoints for get Status, update Status, get Filtered Feedbacks --- .../resource/FeedbackResource.kt | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/hibob/academy/employee_feedback_feature/resource/FeedbackResource.kt b/src/main/kotlin/com/hibob/academy/employee_feedback_feature/resource/FeedbackResource.kt index 7d4c7d87a..b91a4dca8 100644 --- a/src/main/kotlin/com/hibob/academy/employee_feedback_feature/resource/FeedbackResource.kt +++ b/src/main/kotlin/com/hibob/academy/employee_feedback_feature/resource/FeedbackResource.kt @@ -1,7 +1,6 @@ package com.hibob.academy.employee_feedback_feature.resource -import com.hibob.academy.employee_feedback_feature.dao.FeedbackRequest -import com.hibob.academy.employee_feedback_feature.dao.FeedbackSubmission +import com.hibob.academy.employee_feedback_feature.dao.* import com.hibob.academy.employee_feedback_feature.service.FeedbackService import jakarta.ws.rs.* import jakarta.ws.rs.container.ContainerRequestContext @@ -28,7 +27,13 @@ class FeedbackResource(private val feedbackService: FeedbackService) { null } - val feedbackSubmission = FeedbackSubmission(employeeId, companyId, newFeedback.feedbackText, newFeedback.isAnonymous, newFeedback.department) + val feedbackSubmission = FeedbackSubmission( + employeeId, + companyId, + newFeedback.feedbackText, + newFeedback.isAnonymous, + newFeedback.department + ) return Response.ok(feedbackService.submitFeedback(feedbackSubmission)).build() } @@ -53,7 +58,58 @@ class FeedbackResource(private val feedbackService: FeedbackService) { return if (hasPermission(employeeRole, Permissions.VIEW_ALL_FEEDBACKS)) { Response.ok(feedbackService.getAllFeedbacks(companyId)).build() } else { - Response.status(Response.Status.FORBIDDEN).entity("UNAUTHORIZED: You do not have access to view feedbacks").build() + Response.status(Response.Status.UNAUTHORIZED) + .entity("UNAUTHORIZED: You do not have access to view feedbacks") + .build() + } + } + + @Path("view/filter") + @POST + @Produces(MediaType.APPLICATION_JSON) + fun getFilteredFeedbacks(@Context requestContext: ContainerRequestContext, filter: FeedbackFilter): Response { + val companyId = extractClaimAsLong(requestContext, "companyId")!! + val employeeRole = extractRole(requestContext)!! + + return if (hasPermission(employeeRole, Permissions.VIEW_ALL_FEEDBACKS)) { + Response.ok(feedbackService.getFeedbacksUsingFilter(filter, companyId)).build() + } else { + Response.status(Response.Status.UNAUTHORIZED) + .entity("UNAUTHORIZED: You do not have access to view feedbacks").build() + } + } + + @Path("view/status/{feedbackId}") + @GET + @Produces(MediaType.APPLICATION_JSON) + fun getStatus( + @Context requestContext: ContainerRequestContext, + @PathParam("feedbackId") feedbackId: Long + ): Response { + val role = extractRole(requestContext)!! + val companyId = extractClaimAsLong(requestContext, "companyId")!! + val searchedFeedback = SearchedFeedback(companyId, feedbackId) + + return if (hasPermission(role, Permissions.VIEW_ALL_FEEDBACKS)) { + Response.ok("feedback status: ${feedbackService.getFeedbackStatus(searchedFeedback)}").build() + } else { + Response.status(Response.Status.UNAUTHORIZED) + .entity("UNAUTHORIZED: You do not have access to view feedback status!").build() + } + } + + @Path("update/status") + @PUT + @Produces(MediaType.APPLICATION_JSON) + fun updateStatus(@Context requestContext: ContainerRequestContext, updateFeedback: UpdateFeedbackStatus): Response { + val companyId = extractClaimAsLong(requestContext, "companyId")!! + val role = extractRole(requestContext)!! + + return if (hasPermission(role, Permissions.CHANGE_FEEDBACK_STATUS)) { + Response.ok(feedbackService.updateFeedbackStatus(updateFeedback, companyId)).build() + } else { + Response.status(Response.Status.UNAUTHORIZED) + .entity("UNAUTHORIZED: You do not have access to change feedback status!").build() } } } \ No newline at end of file