Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] Fix/638-bookmark #639

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading