Skip to content

Commit

Permalink
Add additional indexes to feedback table (#52)
Browse files Browse the repository at this point in the history
* Add additional indexes to the feedback table for improved query performance on employee_id, created_at, is_anonymous, department

* DROP unnecessary INDEX idx_feedback_is_anonymous

* DROP unnecessary INDEX idx_feedback_department

* Validate strings length (#53)

* Implement validation for feedback content

* space after comma

* change to BadRequestException
  • Loading branch information
RonAzar authored Sep 26, 2024
1 parent 728b908 commit 59fac37
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.hibob.academy.employee_feedback_feature.service

import com.hibob.academy.employee_feedback_feature.dao.*
import com.hibob.academy.employee_feedback_feature.validation.ValidateSubmission.Companion.validateTextSubmission
import org.springframework.beans.factory.annotation.Autowired

import org.springframework.stereotype.Component

@Component
class FeedbackService @Autowired constructor(private val feedbackDao: FeedbackDao) {
fun submitFeedback(feedbackSubmission: FeedbackSubmission): Long {
validateTextSubmission(feedbackSubmission.feedbackText, 300)
validateTextSubmission(feedbackSubmission.department, 100)

return feedbackDao.submitFeedback(feedbackSubmission)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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 com.hibob.academy.employee_feedback_feature.validation.ValidateSubmission.Companion.validateTextSubmission
import jakarta.ws.rs.BadRequestException
import jakarta.ws.rs.NotFoundException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
Expand All @@ -13,13 +15,14 @@ class ResponseService @Autowired constructor(
private val feedbackDao: FeedbackDao
) {
fun submitResponse(newFeedbackResponse: ResponseSubmission, employeeId: Long, companyId: Long): Long {
validateTextSubmission(newFeedbackResponse.responseText, 300)
val feedbackExists = feedbackDao.getFeedbackByFeedbackId(companyId, newFeedbackResponse.feedbackId)
feedbackExists?.let {
if (!feedbackExists.isAnonymous) {
return responseDao.submitResponse(newFeedbackResponse, employeeId)
}

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

throw NotFoundException("Feedback with ID ${newFeedbackResponse.feedbackId} does not exist.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hibob.academy.employee_feedback_feature.validation

import jakarta.ws.rs.BadRequestException

class ValidateSubmission {
companion object{
fun validateTextSubmission(submittedString: String, maxLength: Long): Boolean {
if (submittedString.length > maxLength) {
throw BadRequestException("Error: Input too long!")
}
if (submittedString.isEmpty()) {
throw BadRequestException("Error: Empty Submission!")
}
return true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE INDEX IF NOT EXISTS idx_feedback_employee_id ON feedback (employee_id);

CREATE INDEX IF NOT EXISTS idx_feedback_created_at ON feedback (created_at);

CREATE INDEX IF NOT EXISTS idx_feedback_is_anonymous ON feedback (is_anonymous);

CREATE INDEX IF NOT EXISTS idx_feedback_department ON feedback (department);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS idx_feedback_is_anonymous;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX IF EXISTS idx_feedback_department;

0 comments on commit 59fac37

Please sign in to comment.