diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java index 3ca92e7..aecbf2e 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java @@ -13,7 +13,10 @@ import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; +import java.util.Comparator; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; /** * Controller for Link-related operations such as create, delete, update and get info + statistics @@ -31,7 +34,7 @@ public class LinkController { private final LinkService linkService; private final UserService userService; private final EntityManager entityManager; - private final LinkInfoResponseMapper infoResponseMapper; + private final LinkInfoDtoMapper linkDtoMapper; /** * Controller method for creating a new link. @@ -134,6 +137,50 @@ public ResponseEntity refreshLink(@RequestParam UUID id) throw new ForbiddenException(OPERATION_FORBIDDEN_MSG); } } + //TODO: for info and all-links-info exclude all results of status DELETED! + @GetMapping("/info") + public ResponseEntity getInfoByShortLink(@RequestParam String shortLink) { + Link link = linkService.findByShortLink(shortLink); + if (doesUserHaveRightsForLinkById(link.getId())) { + LinkInfoDto dto = linkDtoMapper.mapLinkToDto(link); + LinkInfoResponse response = new LinkInfoResponse(List.of(dto), "ok"); + return ResponseEntity.ok(response); + } else { + throw new ForbiddenException(OPERATION_FORBIDDEN_MSG); + } + } + + @GetMapping("/all-links-info") + public ResponseEntity getAllLinksForUser() { + try { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + User requesterUser = userService.findByEmail(authentication.getName()); + List linksDto = linkService + .findAllByUser(requesterUser) + .stream() + .map(linkDtoMapper::mapLinkToDto) + .sorted(Comparator.comparing(LinkInfoDto::getUsageStatistics).reversed()) + .collect(Collectors.toList()); + return ResponseEntity.ok(new LinkInfoResponse(linksDto, "ok")); + } catch (Exception e) { + throw new InternalServerLinkException(); + } + + } + + //TODO: exclude all results of non-Active (or only of DELETED?) + @GetMapping("/url-usage-top-for-user") + public ResponseEntity getLinksStatsForUser() { + try { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + User requesterUser = userService.findByEmail(authentication.getName()); + List stats = linkService.getLinkUsageStatsByUserId(requesterUser.getId()); + stats.sort(Comparator.comparing(LinkStatisticsDto::getUsageStatistics).reversed()); + return ResponseEntity.ok(new LinkStatisticsResponse(stats, "ok")); + } catch (Exception e) { + throw new InternalServerLinkException(); + } + } /** * Generates a new short link. diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDto.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDto.java new file mode 100644 index 0000000..d353bab --- /dev/null +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDto.java @@ -0,0 +1,32 @@ +package com.linkurlshorter.urlshortener.link; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.util.UUID; + +/** + * Represents a dto containing information about a link. + * This class provides various properties related to a link, such as its ID, long link, + * short link, creation time, expiration time, usage statistics, and status. + * Instances of this class can be created using the provided builder pattern. + * + * @author Artem Poliakov + * @version 1.0 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class LinkInfoDto { + private UUID id; + private String longLink; + private String shortLink; + private LocalDateTime createdTime; + private LocalDateTime expirationTime; + private int usageStatistics; + private LinkStatus status; +} diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponseMapper.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDtoMapper.java similarity index 80% rename from src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponseMapper.java rename to src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDtoMapper.java index 7fbc493..ba35978 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponseMapper.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoDtoMapper.java @@ -13,9 +13,9 @@ * @version 1.0 */ @Component -public class LinkInfoResponseMapper { - public LinkInfoResponse mapLinkToResponse(Link link, String error) { - return LinkInfoResponse.builder() +public class LinkInfoDtoMapper { + public LinkInfoDto mapLinkToDto(Link link) { + return LinkInfoDto.builder() .id(link.getId()) .longLink(link.getLongLink()) .shortLink(link.getShortLink()) @@ -23,7 +23,6 @@ public LinkInfoResponse mapLinkToResponse(Link link, String error) { .expirationTime(link.getExpirationTime()) .usageStatistics(link.getStatistics()) .status(link.getStatus()) - .error(error) .build(); } } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponse.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponse.java index 496e598..170c52a 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponse.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkInfoResponse.java @@ -5,29 +5,13 @@ import lombok.Data; import lombok.NoArgsConstructor; -import java.time.LocalDateTime; -import java.util.UUID; +import java.util.List; -/** - * Represents a response containing information about a link. - * This class provides various properties related to a link, such as its ID, long link, - * short link, creation time, expiration time, usage statistics, and status. - * Instances of this class can be created using the provided builder pattern. - * - * @author Artem Poliakov - * @version 1.0 - */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class LinkInfoResponse { + private List linkDtoList; private String error; - private UUID id; - private String longLink; - private String shortLink; - private LocalDateTime createdTime; - private LocalDateTime expirationTime; - private int usageStatistics; - private LinkStatus status; } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkRepository.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkRepository.java index 1cd8cdd..7ac2e54 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkRepository.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkRepository.java @@ -1,5 +1,6 @@ package com.linkurlshorter.urlshortener.link; +import com.linkurlshorter.urlshortener.user.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.data.jpa.repository.Modifying; @@ -9,6 +10,7 @@ import java.sql.Timestamp; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -31,4 +33,10 @@ public interface LinkRepository extends JpaRepository { * @return An {@link java.util.Optional} containing the retrieved link entity, or empty if no link is found with the specified short link. */ Optional findByShortLink(String shortLink); + + List findAllByUser(User user); + + @Query("SELECT new com.linkurlshorter.urlshortener.link.LinkStatisticsDto(l.id, l.shortLink, l.statistics)" + + " FROM Link l WHERE l.user.id = :userId") + List getLinkUsageStatsForUser(@Param(value = "userId") UUID userId); } diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java index 1e93c38..2e45d91 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkService.java @@ -1,8 +1,10 @@ package com.linkurlshorter.urlshortener.link; +import com.linkurlshorter.urlshortener.user.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Objects; import java.util.UUID; @@ -86,6 +88,19 @@ public Link findByShortLink(String shortLink) { return link; } + public List findAllByUser(User user){ + if(Objects.isNull(user)){ + throw new NullLinkPropertyException(); + } + return linkRepository.findAllByUser(user); + } + + public List getLinkUsageStatsByUserId(UUID userId){ + if(Objects.isNull(userId)){ + throw new NullLinkPropertyException(); + } + return linkRepository.getLinkUsageStatsForUser(userId); + } /** * Marks a link entity as deleted by its short link. * diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsDto.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsDto.java new file mode 100644 index 0000000..6d9fbc8 --- /dev/null +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsDto.java @@ -0,0 +1,18 @@ +package com.linkurlshorter.urlshortener.link; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.UUID; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class LinkStatisticsDto { + private UUID id; + private String shortLink; + private int usageStatistics; +} diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsResponse.java b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsResponse.java new file mode 100644 index 0000000..a96dd69 --- /dev/null +++ b/src/main/java/com/linkurlshorter/urlshortener/link/LinkStatisticsResponse.java @@ -0,0 +1,17 @@ +package com.linkurlshorter.urlshortener.link; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class LinkStatisticsResponse { + private List linksStatsList; + private String error; +} diff --git a/src/main/java/com/linkurlshorter/urlshortener/user/UserRepository.java b/src/main/java/com/linkurlshorter/urlshortener/user/UserRepository.java index 2aa5502..be2fe2c 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/user/UserRepository.java +++ b/src/main/java/com/linkurlshorter/urlshortener/user/UserRepository.java @@ -1,5 +1,6 @@ package com.linkurlshorter.urlshortener.user; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query;