Skip to content

Commit

Permalink
fix(backend): don't allow duplicate bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
atakanyasar committed Dec 7, 2024
1 parent 1db41c8 commit f54c8f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -40,22 +40,12 @@ public ResponseEntity<GenericApiResponse<BookmarkQuestionResponseDto>> bookmarkQ
bookmarkQuestionResponseDto
);
return buildResponse(genericApiResponse, HttpStatus.OK);


} catch (NoSuchElementException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.errorMessage(e.getMessage())
.stackTrace(Arrays.toString(e.getStackTrace()))
.build();
GenericApiResponse<BookmarkQuestionResponseDto> 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);
}
}

Expand All @@ -70,19 +60,8 @@ public ResponseEntity<GenericApiResponse<BookmarkQuestionResponseDto>> removeBoo
bookmarkQuestionResponseDto
);
return buildResponse(genericApiResponse, HttpStatus.OK);
}
catch (NoSuchElementException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.errorMessage(e.getMessage())
.stackTrace(Arrays.toString(e.getStackTrace()))
.build();
GenericApiResponse<BookmarkQuestionResponseDto> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static <T> ResponseEntity<GenericApiResponse<T>> EntityExistsException(En
GenericApiResponse<T> response = ApiResponseBuilder.buildErrorResponse(
UserProfileResponseDto.class,
e.getMessage(),
HttpStatus.CONFLICT.value(),
HttpStatus.BAD_REQUEST.value(),
errorResponse
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,11 @@ public BookmarkQuestionResponseDto bookmarkQuestion(Long questionId) throws Unau
User user = userContextService.getCurrentUser();

Optional<Question> 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)
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions swagger/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ paths:
responses:
"200":
description: Question successfully bookmarked
"400":
$ref: "#/components/responses/BadRequestResponse"
"401":
$ref: "#/components/responses/UnauthorizedResponse"
"404":
Expand Down

0 comments on commit f54c8f9

Please sign in to comment.