diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/BookmarkController.java b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/BookmarkController.java index 8731efee..f2d2b39a 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/BookmarkController.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/BookmarkController.java @@ -3,12 +3,13 @@ import com.group1.programminglanguagesforum.Constants.EndpointConstants; import com.group1.programminglanguagesforum.DTOs.Responses.BookmarkQuestionResponseDto; import com.group1.programminglanguagesforum.DTOs.Responses.QuestionSummaryDto; -import com.group1.programminglanguagesforum.DTOs.Responses.ErrorResponse; import com.group1.programminglanguagesforum.DTOs.Responses.GenericApiResponse; import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException; import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler; import com.group1.programminglanguagesforum.Services.BookmarkService; import com.group1.programminglanguagesforum.Util.ApiResponseBuilder; + +import jakarta.persistence.EntityExistsException; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -19,7 +20,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; @@ -40,22 +40,12 @@ public ResponseEntity> bookmarkQ bookmarkQuestionResponseDto ); return buildResponse(genericApiResponse, HttpStatus.OK); - - } catch (NoSuchElementException e) { - ErrorResponse errorResponse = ErrorResponse.builder() - .errorMessage(e.getMessage()) - .stackTrace(Arrays.toString(e.getStackTrace())) - .build(); - GenericApiResponse genericApiResponse = ApiResponseBuilder.buildErrorResponse( - BookmarkQuestionResponseDto.class, - "An error occurred while bookmarking the question", - HttpStatus.NOT_FOUND.value(), - errorResponse - ); - return buildResponse(genericApiResponse, HttpStatus.NOT_FOUND); + return ExceptionResponseHandler.NoSuchElementException(e); } catch (UnauthorizedAccessException e) { return ExceptionResponseHandler.UnauthorizedAccessException(e); + } catch (EntityExistsException e) { + return ExceptionResponseHandler.EntityExistsException(e); } } @@ -70,19 +60,8 @@ public ResponseEntity> removeBoo bookmarkQuestionResponseDto ); return buildResponse(genericApiResponse, HttpStatus.OK); - } - catch (NoSuchElementException e) { - ErrorResponse errorResponse = ErrorResponse.builder() - .errorMessage(e.getMessage()) - .stackTrace(Arrays.toString(e.getStackTrace())) - .build(); - GenericApiResponse genericApiResponse = ApiResponseBuilder.buildErrorResponse( - BookmarkQuestionResponseDto.class, - "An error occurred while deleting the bookmark", - HttpStatus.NOT_FOUND.value(), - errorResponse - ); - return buildResponse(genericApiResponse, HttpStatus.NOT_FOUND); + } catch (NoSuchElementException e) { + return ExceptionResponseHandler.NoSuchElementException(e); } catch (UnauthorizedAccessException e) { return ExceptionResponseHandler.UnauthorizedAccessException(e); } diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Exceptions/ExceptionResponseHandler.java b/backend/src/main/java/com/group1/programminglanguagesforum/Exceptions/ExceptionResponseHandler.java index bf941861..391358c5 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Exceptions/ExceptionResponseHandler.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Exceptions/ExceptionResponseHandler.java @@ -120,7 +120,7 @@ public static ResponseEntity> EntityExistsException(En GenericApiResponse response = ApiResponseBuilder.buildErrorResponse( UserProfileResponseDto.class, e.getMessage(), - HttpStatus.CONFLICT.value(), + HttpStatus.BAD_REQUEST.value(), errorResponse ); diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/BookmarkService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/BookmarkService.java index c08fd12c..b33beb04 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/BookmarkService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/BookmarkService.java @@ -8,6 +8,7 @@ import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException; import com.group1.programminglanguagesforum.Repositories.BookmarkRepository; +import jakarta.persistence.EntityExistsException; import lombok.RequiredArgsConstructor; import org.modelmapper.ModelMapper; @@ -30,7 +31,11 @@ public BookmarkQuestionResponseDto bookmarkQuestion(Long questionId) throws Unau User user = userContextService.getCurrentUser(); Optional question = questionService.findById(questionId); - Question questionEntity = question.orElseThrow(); + Question questionEntity = question.orElseThrow(() -> new NoSuchElementException("Question not found")); + + bookmarkRepository.findByUserAndQuestion(user, questionEntity).ifPresent(bookmark -> { + throw new EntityExistsException("Question already bookmarked"); + }); Bookmark bookmark = Bookmark.builder() .user(user) @@ -45,8 +50,8 @@ public BookmarkQuestionResponseDto bookmarkQuestion(Long questionId) throws Unau public BookmarkQuestionResponseDto removeBookmark(Long questionId) throws UnauthorizedAccessException { User user = userContextService.getCurrentUser(); - Question question = questionService.findById(questionId).orElseThrow(); - Bookmark bookmark = bookmarkRepository.findByUserAndQuestion(user, question).orElseThrow(NoSuchElementException::new); + Question question = questionService.findById(questionId).orElseThrow(() -> new NoSuchElementException("Question not found")); + Bookmark bookmark = bookmarkRepository.findByUserAndQuestion(user, question).orElseThrow(() -> new NoSuchElementException("Bookmark not found")); bookmarkRepository.delete(bookmark); diff --git a/swagger/openapi.yml b/swagger/openapi.yml index 1f2498cd..88b152ea 100644 --- a/swagger/openapi.yml +++ b/swagger/openapi.yml @@ -551,6 +551,8 @@ paths: responses: "200": description: Question successfully bookmarked + "400": + $ref: "#/components/responses/BadRequestResponse" "401": $ref: "#/components/responses/UnauthorizedResponse" "404":