Skip to content

Commit

Permalink
Merge pull request #654 from bounswe/lab-9
Browse files Browse the repository at this point in the history
Lab-9 PR
  • Loading branch information
EnesBaserr authored Dec 10, 2024
2 parents 94135ed + 82f5462 commit c3863b3
Show file tree
Hide file tree
Showing 2 changed files with 16 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
@Repository
public interface TagRepository extends JpaRepository<Tag,Long> {
List<Tag> findAllByIdIn(List<Long> ids);
@Query("SELECT t FROM Tag t " +
"JOIN Question q ON t MEMBER OF q.tags " +
"GROUP BY t.id " +
"ORDER BY COUNT(q.id) DESC")

Page<Tag> findTagsByTagNameContainingIgnoreCase(String tagName, Pageable pageable);
@Query("SELECT t FROM Tag t JOIN t.followers u WHERE u.id = :userId")
Expand Down

0 comments on commit c3863b3

Please sign in to comment.