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

[5기 손가현] Shorten-URL 과제 제출합니다. #72

Open
wants to merge 14 commits into
base: hyun2371
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
[Feat] 레이어별 로직 추가
hyun2371 committed Dec 17, 2023
commit f2d160f9c5b47f542efddce57083ec6ca8016950
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.prgrms.url_shortener.controller;

import com.prgrms.url_shortener.dto.ShortenUrlRequest;
import com.prgrms.url_shortener.dto.ShortenUrlResponse;
import com.prgrms.url_shortener.service.UrlService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
@RequiredArgsConstructor
public class UrlController {

private final UrlService urlService;

@GetMapping("/")
public String home(){
return "index";
}

@PostMapping("/url-shortener")
public String getShortenUrl(@Valid @ModelAttribute ShortenUrlRequest request, Model model) {
ShortenUrlResponse response = urlService.getShortUrl(request);
model.addAttribute("shortenUrl", response.shortenUrl());
model.addAttribute("requestCount", response.requestCount());

return "index";
}

@GetMapping("/url-shortener/{shortenUrl}")
public String redirectOriginUrl(@PathVariable String shortenUrl){
String originUrl = urlService.getOriginUrl(shortenUrl);
return "redirect:" + originUrl;
}
}
Original file line number Diff line number Diff line change
@@ -16,21 +16,21 @@
public class Url {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "url_name")
@Column(name = "url_id")
private Long id;

@Column(name = "origin_url", unique = true, nullable = false)
private String originUrl;

@Column(name = "short_url")
private String shortUrl;

@Column(name = "request_count")
private int requestCount;

public Url(String originUrl, String shortUrl) {
public Url(String originUrl) {
this.originUrl = originUrl;
this.shortUrl = shortUrl;
this.requestCount = 0;
this.requestCount = 1;
}

public void increaseRequestCount(){
requestCount++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.prgrms.url_shortener.repository;

import com.prgrms.url_shortener.entity.Url;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UrlRepository extends JpaRepository<Url, Long> {
Optional<Url> findByOriginUrl(String originUrl);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.prgrms.url_shortener.service;

import com.prgrms.url_shortener.algorithm.Base62Algorithm;
import com.prgrms.url_shortener.dto.ShortenUrlRequest;
import com.prgrms.url_shortener.dto.ShortenUrlResponse;
import com.prgrms.url_shortener.entity.Url;
import com.prgrms.url_shortener.exception.CustomException;
import com.prgrms.url_shortener.repository.UrlRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class UrlService {
private final UrlRepository urlRepository;

@Transactional
public ShortenUrlResponse getShortUrl(ShortenUrlRequest request) {
Url savedUrl = urlRepository.findByOriginUrl(request.originUrl())
.orElseGet(() -> urlRepository.save(new Url(request.originUrl())));
savedUrl.increaseRequestCount();
String shortenUrl = Base62Algorithm.encode(savedUrl.getId());
return new ShortenUrlResponse(shortenUrl, savedUrl.getRequestCount());
}

@Transactional(readOnly = true)
public String getOriginUrl(String shortenUrl) {
Long urlId = Base62Algorithm.decode(shortenUrl);
Url url = urlRepository.findById(urlId).orElseThrow(() -> {
throw new CustomException("존재하지 않는 URL입니다.");
});
return url.getOriginUrl();
}
}