Skip to content

Commit

Permalink
Merge pull request #190 from softeerbootcamp4th/docs/#189-url-swagger
Browse files Browse the repository at this point in the history
docs/#189-url-swagger
  • Loading branch information
wjddn2165 authored Aug 20, 2024
2 parents 84eca76 + 77446f5 commit f83e54d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
/.idea/
/Server/out/
/Server/src/main/resources/application-local.yml
/logs/
/Server/logs/
/logs/

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import JGS.CasperEvent.domain.url.dto.ShortenUrlResponseDto;
import JGS.CasperEvent.domain.url.service.UrlService;
import JGS.CasperEvent.global.entity.BaseUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -26,21 +29,25 @@ public UrlController(UrlService urlService) {
this.urlService = urlService;
}

// 공유링크 생성
@Operation(summary = "공유 링크 생성", description = "사용자가 공유할 URL을 단축 링크로 생성합니다.")
@ApiResponse(responseCode = "201", description = "Short URL creation successful")
@PostMapping
public ResponseEntity<ShortenUrlResponseDto> generateShortUrl(HttpServletRequest request) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
public ResponseEntity<ShortenUrlResponseDto> generateShortUrl(HttpServletRequest request)
throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
BaseUser user = (BaseUser) request.getAttribute("user");
return ResponseEntity
.status(HttpStatus.CREATED)
.body(urlService.generateShortUrl(user));
}

// 공유링크 접속
@Operation(summary = "공유 링크 접속", description = "단축 링크를 통해 원본 URL로 리다이렉트합니다.")
@ApiResponse(responseCode = "302", description = "Redirect successful")
@GetMapping("/{encodedId}")
public ResponseEntity<Void> redirectOriginalUrl(@PathVariable String encodedId){
public ResponseEntity<Void> redirectOriginalUrl(@PathVariable String encodedId) {
String originalUrl = urlService.getOriginalUrl(encodedId);
return ResponseEntity
.status(HttpStatus.FOUND)
.header("Location", urlService.getOriginalUrl(encodedId))
.header("Location", originalUrl)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public ShortenUrlResponseDto generateShortUrl(BaseUser user) throws NoSuchPaddin
}

// 원본 url 조회 테스트
public String getOriginalUrl(String encodedId){
public String getOriginalUrl(String encodedId) {
Long urlId = Base62Utils.decode(encodedId);
Url url = urlRepository.findById(urlId).orElseThrow(NoSuchElementException::new);
Url url = urlRepository.findById(urlId).orElseGet(() -> new Url(clientUrl));
return url.getOriginalUrl();
}

Expand Down

0 comments on commit f83e54d

Please sign in to comment.