Skip to content

Commit

Permalink
#82 [refactor] : URL DTO를 레코드로 리팩토링한다
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Oct 21, 2024
1 parent b4393ea commit 2a7df7f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 77 deletions.
17 changes: 10 additions & 7 deletions src/main/java/side/onetime/controller/UrlController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import side.onetime.dto.UrlDto;
import side.onetime.dto.url.request.ConvertToOriginalUrlRequest;
import side.onetime.dto.url.request.ConvertToShortenUrlRequest;
import side.onetime.dto.url.response.ConvertToOriginalUrlResponse;
import side.onetime.dto.url.response.ConvertToShortenUrlResponse;
import side.onetime.global.common.ApiResponse;
import side.onetime.global.common.status.SuccessStatus;
import side.onetime.service.UrlService;
Expand All @@ -19,19 +22,19 @@ public class UrlController {

// 원본 -> 단축 URL API
@PostMapping("/action-shorten")
public ResponseEntity<ApiResponse<UrlDto.ConvertToShortenUrlResponse>> convertToShortenUrl(
@RequestBody UrlDto.ConvertToShortenUrlRequest covertToShortenUrlRequest) {
public ResponseEntity<ApiResponse<ConvertToShortenUrlResponse>> convertToShortenUrl(
@RequestBody ConvertToShortenUrlRequest covertToShortenUrlRequest) {

UrlDto.ConvertToShortenUrlResponse convertToShortenUrlResponse = urlService.convertToShortenUrl(covertToShortenUrlRequest);
ConvertToShortenUrlResponse convertToShortenUrlResponse = urlService.convertToShortenUrl(covertToShortenUrlRequest);
return ApiResponse.onSuccess(SuccessStatus._CONVERT_TO_SHORTEN_URL, convertToShortenUrlResponse);
}

// 단축 -> 원본 URL API
@PostMapping("/action-original")
public ResponseEntity<ApiResponse<UrlDto.ConvertToOriginalUrlResponse>> convertToOriginalUrl(
@RequestBody UrlDto.ConvertToOriginalUrlRequest convertToOriginalUrlRequest) {
public ResponseEntity<ApiResponse<ConvertToOriginalUrlResponse>> convertToOriginalUrl(
@RequestBody ConvertToOriginalUrlRequest convertToOriginalUrlRequest) {

UrlDto.ConvertToOriginalUrlResponse convertToOriginalUrlResponse = urlService.convertToOriginalUrl(convertToOriginalUrlRequest);
ConvertToOriginalUrlResponse convertToOriginalUrlResponse = urlService.convertToOriginalUrl(convertToOriginalUrlRequest);
return ApiResponse.onSuccess(SuccessStatus._CONVERT_TO_ORIGINAL_URL, convertToOriginalUrlResponse);
}
}
63 changes: 0 additions & 63 deletions src/main/java/side/onetime/dto/UrlDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package side.onetime.dto.url.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ConvertToOriginalUrlRequest(
@NotBlank(message = "Shorten URL은 필수 값입니다.") String shortenUrl
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package side.onetime.dto.url.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ConvertToShortenUrlRequest(
@NotBlank(message = "Original URL은 필수 값입니다.") String originalUrl
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package side.onetime.dto.url.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ConvertToOriginalUrlResponse(
String originalUrl
) {
public static ConvertToOriginalUrlResponse of(String originalUrl) {
return new ConvertToOriginalUrlResponse(originalUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package side.onetime.dto.url.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ConvertToShortenUrlResponse(
String shortenUrl
) {
public static ConvertToShortenUrlResponse of(String shortenUrl) {
return new ConvertToShortenUrlResponse(shortenUrl);
}
}
17 changes: 10 additions & 7 deletions src/main/java/side/onetime/service/UrlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import side.onetime.dto.UrlDto;
import side.onetime.dto.url.request.ConvertToOriginalUrlRequest;
import side.onetime.dto.url.request.ConvertToShortenUrlRequest;
import side.onetime.dto.url.response.ConvertToOriginalUrlResponse;
import side.onetime.dto.url.response.ConvertToShortenUrlResponse;
import side.onetime.exception.EventErrorResult;
import side.onetime.exception.EventException;
import side.onetime.repository.EventRepository;
Expand All @@ -16,28 +19,28 @@ public class UrlService {
private final EventRepository eventRepository;

// URL 단축 메서드
public UrlDto.ConvertToShortenUrlResponse convertToShortenUrl(UrlDto.ConvertToShortenUrlRequest convertToShortenUrlRequest) {
String originalUrl = convertToShortenUrlRequest.getOriginalUrl();
public ConvertToShortenUrlResponse convertToShortenUrl(ConvertToShortenUrlRequest convertToShortenUrlRequest) {
String originalUrl = convertToShortenUrlRequest.originalUrl();

UUID eventId = extractEventIdFromUrl(originalUrl);
if (!eventRepository.existsByEventId(eventId)) {
throw new EventException(EventErrorResult._NOT_FOUND_EVENT);
}

return UrlDto.ConvertToShortenUrlResponse.of(Base62Util.convertToShortenUrl(originalUrl));
return ConvertToShortenUrlResponse.of(Base62Util.convertToShortenUrl(originalUrl));
}

// URL 복원 메서드
public UrlDto.ConvertToOriginalUrlResponse convertToOriginalUrl(UrlDto.ConvertToOriginalUrlRequest convertToOriginalUrlRequest) {
String shortenUrl = convertToOriginalUrlRequest.getShortenUrl();
public ConvertToOriginalUrlResponse convertToOriginalUrl(ConvertToOriginalUrlRequest convertToOriginalUrlRequest) {
String shortenUrl = convertToOriginalUrlRequest.shortenUrl();
String originalUrl = Base62Util.convertToOriginalUrl(shortenUrl);

UUID eventId = extractEventIdFromUrl(originalUrl);
if (!eventRepository.existsByEventId(eventId)) {
throw new EventException(EventErrorResult._NOT_FOUND_EVENT);
}

return UrlDto.ConvertToOriginalUrlResponse.of(originalUrl);
return ConvertToOriginalUrlResponse.of(originalUrl);
}

// URL에서 Event ID 추출
Expand Down

0 comments on commit 2a7df7f

Please sign in to comment.