Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first version of link controller with all endpoints. !bug is present!… #39

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -134,6 +137,50 @@ public ResponseEntity<LinkModifyingResponse> 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<LinkInfoResponse> 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<LinkInfoResponse> getAllLinksForUser() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User requesterUser = userService.findByEmail(authentication.getName());
List<LinkInfoDto> 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<LinkStatisticsResponse> getLinksStatsForUser() {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User requesterUser = userService.findByEmail(authentication.getName());
List<LinkStatisticsDto> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
* @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())
.createdTime(link.getCreatedTime())
.expirationTime(link.getExpirationTime())
.usageStatistics(link.getStatistics())
.status(link.getStatus())
.error(error)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LinkInfoDto> 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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,6 +10,7 @@

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -31,4 +33,10 @@ public interface LinkRepository extends JpaRepository<Link, UUID> {
* @return An {@link java.util.Optional} containing the retrieved link entity, or empty if no link is found with the specified short link.
*/
Optional<Link> findByShortLink(String shortLink);

List<Link> 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<LinkStatisticsDto> getLinkUsageStatsForUser(@Param(value = "userId") UUID userId);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -86,6 +88,19 @@ public Link findByShortLink(String shortLink) {
return link;
}

public List<Link> findAllByUser(User user){
if(Objects.isNull(user)){
throw new NullLinkPropertyException();
}
return linkRepository.findAllByUser(user);
}

public List<LinkStatisticsDto> getLinkUsageStatsByUserId(UUID userId){
if(Objects.isNull(userId)){
throw new NullLinkPropertyException();
}
return linkRepository.getLinkUsageStatsForUser(userId);
}
/**
* Marks a link entity as deleted by its short link.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<LinkStatisticsDto> linksStatsList;
private String error;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading