Skip to content

Commit

Permalink
Merge pull request #92 from nastiausenko/adding_only-active_link_cont…
Browse files Browse the repository at this point in the history
…roller_method

Added only-active-links endpoint
  • Loading branch information
IvanShalaev1990 authored Apr 24, 2024
2 parents 29950f0 + fb57ceb commit d9494da
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ public ResponseEntity<LinkInfoResponse> getAllLinksForUser() {
return ResponseEntity.ok(new LinkInfoResponse(linksDto, "ok"));
}

/**
* Retrieves a list of active links associated with the currently authenticated user.
* This method first fetches the user ID of the currently authenticated user using the UserService.
* Then, it calls the LinkService to retrieve all active links associated with the user identified by the user ID.
*
* @return A list of active LinkDto objects associated with the currently authenticated user.
*/
@GetMapping("/active-links")
public List<LinkInfoDto> getOnlyActiveLinks(){
UUID userId = userService.findByEmail(SecurityContextHolder.getContext().getAuthentication().getName()).getId();
return linkService
.findAllActiveByUserId(userId)
.stream()
.map(linkDtoMapper::mapLinkToDto)
.toList();
}

/**
* Retrieves usage statistics for all links associated with the authenticated user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,35 @@ public interface LinkRepository extends JpaRepository<Link, UUID> {
*/
Optional<Link> findByShortLink(String shortLink);

/**
* Retrieves a list of links associated with the specified user ID, excluding those with a status of 'DELETED'.
* This method executes a JPQL query to fetch all links associated with the given user ID,
* excluding those with a status set to 'DELETED'.
*
* @param userId The ID of the user whose links are to be retrieved.
* @return A list of Link objects associated with the specified user ID, excluding links with a status of 'DELETED'.
*/
@Query("SELECT l from Link l WHERE l.user.id = :userId AND l.status <> 'DELETED'")
List<Link> findAllByUserId(@Param(value = "userId") UUID userId);

/**
* Retrieves a list of active links associated with the specified user ID.
* <p>
* This method executes a JPQL query to fetch all active links associated with the given user ID.
* Active links are those whose status is set to 'ACTIVE'.
*
* @param userId The ID of the user whose active links are to be retrieved.
* @return A list of active Link objects associated with the specified user ID.
*/
@Query("SELECT l FROM Link l WHERE l.user.id = :userId AND l.status = 'ACTIVE'")
List<Link> findAllActiveByUserId(@Param("userId") UUID userId);

List<Link> findAllByUser(User user);

@Query("SELECT new com.linkurlshorter.urlshortener.link.dto.LinkStatisticsDto(l.id, l.shortLink, l.statistics)" +
" FROM Link l WHERE l.user.id = :userId AND l.status <> 'DELETED'")
List<LinkStatisticsDto> getLinkUsageStatsForUser(@Param(value = "userId") UUID userId);

/**
* Deletes a link entity by its ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import redis.clients.jedis.JedisPool;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -149,14 +150,37 @@ public Link findByShortLink(String shortLink) {
if (link.getStatus() == LinkStatus.DELETED) {
throw new DeletedLinkException();
}
fixLinkStatusesAndReturnFixed(List.of(link));
return link;
}

public List<Link> findAllByUserId(UUID userId) {
if (Objects.isNull(userId)) {
throw new NullLinkPropertyException();
}
return linkRepository.findAllByUserId(userId);
List<Link> allByUserId = linkRepository.findAllByUserId(userId);
fixLinkStatusesAndReturnFixed(allByUserId);
return allByUserId;
}

/**
* Retrieves a list of active links associated with the specified user ID and updates the status of expired links.
* This method first checks if the provided user ID is null. If so, it throws a NullLinkPropertyException.
* It then retrieves all active links associated with the given user ID from the LinkRepository.
* Any links with expiration times before the current time are marked as inactive. All the rest truly active links are then returned.
*
* @param userId The ID of the user whose active links are to be retrieved.
* @return A list of active Link objects associated with the specified user ID.
* @throws NullLinkPropertyException if the provided user ID is null.
*/
public List<Link> findAllActiveByUserId(UUID userId) {
if (Objects.isNull(userId)) {
throw new NullLinkPropertyException();
}
List<Link> allActiveByUserId = linkRepository.findAllActiveByUserId(userId);
List<Link> fixedToInactive = fixLinkStatusesAndReturnFixed(allActiveByUserId);
allActiveByUserId.removeAll(fixedToInactive);
return allActiveByUserId;
}

public List<LinkStatisticsDto> getLinkUsageStatsByUserId(UUID userId) {
Expand Down Expand Up @@ -196,4 +220,15 @@ public void deleteByShortLink(String shortLink) {
public boolean doesLinkExist(String shortLink) {
return linkRepository.findByShortLink(shortLink).isPresent();
}

private List<Link> fixLinkStatusesAndReturnFixed(List<Link> allActiveByUserId) {
List<Link> toBeStatusFixed = new ArrayList<>();
for (Link link : allActiveByUserId) {
if (link.getExpirationTime().isBefore(LocalDateTime.now())) {
link.setStatus(LinkStatus.INACTIVE);
toBeStatusFixed.add(link);
}
}
return toBeStatusFixed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,11 @@ void findByShortLinkDeletedTest() {
void findByAllByUserIdTest() {
UUID userId = UUID.fromString("84991c79-f6a9-4b7b-b1b4-0d66c0b92c81");
User user = User.builder().id(userId).build();
LocalDateTime expirationTime = LocalDateTime.now().plusDays(1);
List<Link> userLinks = Arrays.asList(
Link.builder().id(UUID.randomUUID()).user(user).build(),
Link.builder().id(UUID.randomUUID()).user(user).build(),
Link.builder().id(UUID.randomUUID()).user(user).build()
Link.builder().id(UUID.randomUUID()).user(user).expirationTime(expirationTime).build(),
Link.builder().id(UUID.randomUUID()).user(user).expirationTime(expirationTime).build(),
Link.builder().id(UUID.randomUUID()).user(user).expirationTime(expirationTime).build()
);

when(linkRepository.findAllByUserId(userId)).thenReturn(userLinks);
Expand Down

0 comments on commit d9494da

Please sign in to comment.