Skip to content

Commit

Permalink
Tag creating will be checked based on reputation points.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesBaserr committed Dec 10, 2024
1 parent 1fb606f commit 82f5462
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException;
import com.group1.programminglanguagesforum.Services.TagService;
import com.group1.programminglanguagesforum.Services.UserContextService;
import com.group1.programminglanguagesforum.Services.UserService;
import com.group1.programminglanguagesforum.Util.ApiResponseBuilder;

import jakarta.persistence.EntityExistsException;
Expand All @@ -26,6 +27,7 @@

import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Optional;


@RestController
Expand All @@ -35,6 +37,7 @@ public class TagController extends BaseController {

private final TagService tagService;
private final UserContextService userContextService;
private final UserService userService;

@GetMapping(value = EndpointConstants.TagEndpoints.SEARCH)
public ResponseEntity<GenericApiResponse<TagSearchResponseDto>> tagSearch(
Expand Down Expand Up @@ -94,6 +97,13 @@ public ResponseEntity<GenericApiResponse<GetTagDetailsResponseDto>> getTagDetail
@PostMapping(value = EndpointConstants.TagEndpoints.BASE_PATH)
public ResponseEntity<GenericApiResponse<GetTagDetailsResponseDto>> createTag(@RequestBody CreateTagRequestDto dto){
try{
User user = userContextService.getCurrentUser();
if(userService.calculateReputation(user) < 50){
return ExceptionResponseHandler.IllegalArgumentException(
new IllegalArgumentException("User does not have enough reputation to create a tag which should be at least 50")
);
}

GetTagDetailsResponseDto tagDetails = tagService.createTag(dto);
GenericApiResponse<GetTagDetailsResponseDto> response = GenericApiResponse.<GetTagDetailsResponseDto>builder()
.status(201)
Expand All @@ -115,6 +125,8 @@ public ResponseEntity<GenericApiResponse<GetTagDetailsResponseDto>> createTag(@R
.build();
ApiResponseBuilder.buildErrorResponse(GetTagDetailsResponseDto.class, "Invalid tag type", 400, errorResponse);
return buildResponse(response, HttpStatus.BAD_REQUEST);
} catch (UnauthorizedAccessException e) {
return ExceptionResponseHandler.UnauthorizedAccessException(e);
}

}
Expand Down

0 comments on commit 82f5462

Please sign in to comment.