Skip to content

Commit

Permalink
Response dao+service (#50)
Browse files Browse the repository at this point in the history
* Removing responder id from data class(ResponseSubmission) + adding check in service layer if feedback is anonymous

* fix in this case, throw not found.

* fix test, throw not found.
  • Loading branch information
RonAzar authored Sep 26, 2024
1 parent c3776f7 commit dc5b700
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class FeedbackResponseDao @Autowired constructor(private val sql: DSLContext) {
)
}

fun submitResponse(newFeedbackResponse: ResponseSubmission): Long {
fun submitResponse(newFeedbackResponse: ResponseSubmission, responderId: Long): Long {
return sql.insertInto(response)
.set(response.responseText, newFeedbackResponse.responseText)
.set(response.responderId, newFeedbackResponse.responderId)
.set(response.responderId, responderId)
.set(response.feedbackId, newFeedbackResponse.feedbackId)
.returning(response.id)
.fetchOne()!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class EmployeeData(val employeeId: Int, val role: EmployeeRole, val company
data class EmployeeLogin(val firstName: String, val lastName: String, val companyId: Int)

data class FeedbackResponseData(val responseId: Long, val responseText: String, val responderId: Long, val feedbackId: Long, val createdAt: LocalDateTime)
data class ResponseSubmission(val responseText: String, val responderId: Long, val feedbackId: Long)
data class ResponseSubmission(val responseText: String, val feedbackId: Long)

enum class EmployeeRole{
ADMIN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hibob.academy.employee_feedback_feature.service
import com.hibob.academy.employee_feedback_feature.dao.FeedbackDao
import com.hibob.academy.employee_feedback_feature.dao.FeedbackResponseDao
import com.hibob.academy.employee_feedback_feature.dao.ResponseSubmission
import jakarta.ws.rs.NotFoundException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

Expand All @@ -11,12 +12,16 @@ class ResponseService @Autowired constructor(
private val responseDao: FeedbackResponseDao,
private val feedbackDao: FeedbackDao
) {
fun submitResponse(newFeedbackResponse: ResponseSubmission, companyId: Long): Long {
fun submitResponse(newFeedbackResponse: ResponseSubmission, employeeId: Long, companyId: Long): Long {
val feedbackExists = feedbackDao.getFeedbackByFeedbackId(companyId, newFeedbackResponse.feedbackId)
feedbackExists?.let {
return responseDao.submitResponse(newFeedbackResponse)
if (!feedbackExists.isAnonymous) {
return responseDao.submitResponse(newFeedbackResponse, employeeId)
}

throw IllegalArgumentException("Sorry, this feedback is anonymous. You cannot respond to it!")
}

throw IllegalArgumentException("Feedback with ID ${newFeedbackResponse.feedbackId} does not exist.")
throw NotFoundException("Feedback with ID ${newFeedbackResponse.feedbackId} does not exist.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class RolePermissionValidator {
enum class Permissions {
VIEW_ALL_FEEDBACKS,
CHANGE_FEEDBACK_STATUS,
RESPONSE_TO_FEEDBACK
}


private val rolePermissions = mapOf(
EmployeeRole.HR to setOf(Permissions.VIEW_ALL_FEEDBACKS, Permissions.CHANGE_FEEDBACK_STATUS),
EmployeeRole.HR to setOf(Permissions.VIEW_ALL_FEEDBACKS, Permissions.CHANGE_FEEDBACK_STATUS, Permissions.RESPONSE_TO_FEEDBACK),
EmployeeRole.ADMIN to setOf(Permissions.VIEW_ALL_FEEDBACKS),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FeedbackResponseDaoTest @Autowired constructor(private val sql: DSLContext
private val responseText = "What a Response!"
private val responderId = -1L
private val feedbackId = -2L
private val testResponse = ResponseSubmission(responseText, responderId, feedbackId)
private val testResponse = ResponseSubmission(responseText, feedbackId)
private val dao = FeedbackResponseDao(sql)
val response = FeedbackResponseTable.instance

Expand All @@ -23,7 +23,7 @@ class FeedbackResponseDaoTest @Autowired constructor(private val sql: DSLContext

@Test
fun `Submit new response`() {
val submittedResponseId = dao.submitResponse(testResponse)
val submittedResponseId = dao.submitResponse(testResponse, responderId)
assertNotNull(submittedResponseId)
assertNotNull(dao.getResponseById(submittedResponseId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.hibob.academy.employee_feedback_feature.dao.FeedbackDao
import com.hibob.academy.employee_feedback_feature.dao.FeedbackData
import com.hibob.academy.employee_feedback_feature.dao.FeedbackResponseDao
import com.hibob.academy.employee_feedback_feature.dao.ResponseSubmission
import jakarta.ws.rs.NotFoundException
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -37,10 +38,10 @@ class ResponseServiceTest {
)

whenever(feedbackDao.getFeedbackByFeedbackId(companyId, feedbackId)).thenReturn(testFeedbackData)
whenever(responseDao.submitResponse(any())).thenReturn(responseId)
whenever(responseDao.submitResponse(any(), any())).thenReturn(responseId)

val responseSubmission = ResponseSubmission(responseText, responderId, feedbackId)
val result = responseService.submitResponse(responseSubmission, companyId)
val responseSubmission = ResponseSubmission(responseText, feedbackId)
val result = responseService.submitResponse(responseSubmission, responseId, companyId)

assertEquals(responseId, result)
}
Expand All @@ -49,10 +50,10 @@ class ResponseServiceTest {
fun `submitResponse should throw an exception if feedback does not exist`() {
whenever(feedbackDao.getFeedbackByFeedbackId(companyId, feedbackId)).thenReturn(null)

val responseSubmission = ResponseSubmission(responseText, responderId, feedbackId)
val responseSubmission = ResponseSubmission(responseText, feedbackId)

val exception = assertThrows<IllegalArgumentException> {
responseService.submitResponse(responseSubmission, companyId)
val exception = assertThrows<NotFoundException> {
responseService.submitResponse(responseSubmission, responseId, companyId)
}

assertEquals("Feedback with ID $feedbackId does not exist.", exception.message)
Expand Down

0 comments on commit dc5b700

Please sign in to comment.