-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #563 from bounswe/develop
- Loading branch information
Showing
79 changed files
with
4,893 additions
and
883 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
backend/src/main/java/com/group1/programminglanguagesforum/Controllers/AnswerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.group1.programminglanguagesforum.Controllers; | ||
|
||
import com.group1.programminglanguagesforum.Constants.EndpointConstants; | ||
import com.group1.programminglanguagesforum.DTOs.Requests.CreateAnswerRequestDto; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.CreateAnswerResponseDto; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.ErrorResponse; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.GenericApiResponse; | ||
import com.group1.programminglanguagesforum.DTOs.Responses.GetAnswersResponseDto; | ||
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException; | ||
import com.group1.programminglanguagesforum.Services.AnswerService; | ||
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Arrays; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") | ||
public class AnswerController extends BaseController { | ||
private final AnswerService answerService; | ||
|
||
@PutMapping(value= EndpointConstants.AnswerEndpoints.ANSWER_ID) | ||
public ResponseEntity<GenericApiResponse<CreateAnswerResponseDto>> updateAnswer( | ||
@PathVariable(value = "id") Long answerId, | ||
@RequestBody CreateAnswerRequestDto createAnswerRequestDto) throws UnauthorizedAccessException { | ||
CreateAnswerResponseDto response = answerService.updateAnswer(answerId, createAnswerRequestDto); | ||
GenericApiResponse<CreateAnswerResponseDto> apiResponse = GenericApiResponse.<CreateAnswerResponseDto>builder() | ||
.status(200) | ||
.message("Answer updated successfully") | ||
.data(response) | ||
.build(); | ||
return buildResponse(apiResponse, HttpStatus.OK); | ||
} | ||
@PostMapping(value= EndpointConstants.QuestionEndpoints.QUESTION_ANSWERS) | ||
public ResponseEntity<GenericApiResponse<CreateAnswerResponseDto>> createAnswer( | ||
@PathVariable(value = "questionId") Long questionId, | ||
@RequestBody CreateAnswerRequestDto createAnswerRequestDto) throws UnauthorizedAccessException { | ||
CreateAnswerResponseDto response = answerService.createAnswer(questionId, createAnswerRequestDto); | ||
GenericApiResponse<CreateAnswerResponseDto> apiResponse = GenericApiResponse.<CreateAnswerResponseDto>builder() | ||
.status(201) | ||
.message("Answer created successfully") | ||
.data(response) | ||
.build(); | ||
return buildResponse(apiResponse, HttpStatus.CREATED); | ||
} | ||
@DeleteMapping(value= EndpointConstants.AnswerEndpoints.ANSWER_ID) | ||
public ResponseEntity<GenericApiResponse<String>> deleteAnswer(@PathVariable(value = "id") Long answerId) throws UnauthorizedAccessException { | ||
try { | ||
answerService.deleteAnswer(answerId); | ||
GenericApiResponse<String> apiResponse = GenericApiResponse.<String>builder() | ||
.status(200) | ||
.message("Answer deleted successfully") | ||
.data("Answer deleted successfully") | ||
.build(); | ||
return buildResponse(apiResponse, HttpStatus.OK); | ||
} | ||
catch (Exception e){ | ||
ErrorResponse errorResponse = ErrorResponse.builder() | ||
.errorMessage(e.getMessage()) | ||
.stackTrace(Arrays.toString(e.getStackTrace())) | ||
.build(); | ||
ApiResponseBuilder.buildErrorResponse(String.class, "An error occurred", 500, errorResponse); | ||
return buildResponse(ApiResponseBuilder.buildErrorResponse(String.class, "An error occurred", 500, errorResponse), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
@GetMapping(value=EndpointConstants.QuestionEndpoints.QUESTION_ANSWERS) | ||
public ResponseEntity<GenericApiResponse<GetAnswersResponseDto>>getAnswersForQuestion(@PathVariable(value = "questionId") Long questionId) throws UnauthorizedAccessException { | ||
GetAnswersResponseDto response = answerService.getAnswersForQuestion(questionId); | ||
GenericApiResponse<GetAnswersResponseDto> apiResponse = GenericApiResponse.<GetAnswersResponseDto>builder() | ||
.status(200) | ||
.message("Answers retrieved successfully") | ||
.data(response) | ||
.build(); | ||
return buildResponse(apiResponse, HttpStatus.OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.