Skip to content

Commit

Permalink
reface : 루트 경로 변경(org.service -> com), 컨트롤러 간결화, 팩토링 패턴 추가, DDL,DML 파…
Browse files Browse the repository at this point in the history
…일 추가, 주석 추가, 메인 페이지 이동REST api 삭제, Model 적용, 코드 리팩토링
  • Loading branch information
HwangHarim committed Jul 2, 2024
1 parent d627cda commit ec545e4
Show file tree
Hide file tree
Showing 52 changed files with 276 additions and 268 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

group = 'org.service'
group = 'com'
version = '0.0.1-SNAPSHOT'

java {
Expand Down
6 changes: 6 additions & 0 deletions sql/DDL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE `short_url` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`origin_url` VARCHAR(255) NOT NULL,
`create_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT = 20000;
1 change: 1 addition & 0 deletions sql/DML.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `short_url` (`origin_url`) VALUES ('https://example.com');
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener;
package com.urlshortener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.service.urlshortener.batch.job;
package com.urlshortener.batch.job;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.service.urlshortener.shortener.domain.OriginUrl;
import org.service.urlshortener.shortener.repository.OriginUrlRepository;
import com.urlshortener.shortener.domain.ShortUrl;
import com.urlshortener.shortener.repository.ShortUrlRepository;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
Expand All @@ -12,12 +12,12 @@
@Component
@RequiredArgsConstructor
public class UrlDeleteJob {
private final OriginUrlRepository OriginUrlRepository;
private final ShortUrlRepository OriginUrlRepository;

@Transactional
public void removeSixMonthsOldData() {
LocalDateTime sixMonthsAgo = LocalDateTime.now().minusMonths(6);
List<OriginUrl> oldDates = OriginUrlRepository.findByCreatedAtBefore(sixMonthsAgo);
List<ShortUrl> oldDates = OriginUrlRepository.findByCreatedAtBefore(sixMonthsAgo);
OriginUrlRepository.deleteAll(oldDates);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.service.urlshortener.batch.scheudler;
package com.urlshortener.batch.scheudler;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.service.urlshortener.batch.job.UrlDeleteJob;
import org.service.urlshortener.error.dto.ErrorMessage;
import org.service.urlshortener.error.exception.url.NotFinishRemoveSixMonthsOldDataException;
import com.urlshortener.batch.job.UrlDeleteJob;
import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.url.NotFinishRemoveSixMonthsOldDataException;
import org.springframework.scheduling.annotation.Scheduled;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.cache;
package com.urlshortener.cache;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.service.urlshortener.cache;
package com.urlshortener.cache;

import org.service.urlshortener.shortener.dto.ShortUrlModel;
import com.urlshortener.shortener.dto.model.ShortUrlModel;

import java.time.Duration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.service.urlshortener.cache;
package com.urlshortener.cache;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.service.urlshortener.util.MapperUtil;
import com.urlshortener.util.MapperUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.common.entity;
package com.urlshortener.common.entity;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/urlshortener/common/response/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.urlshortener.common.response;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ResponseDto<T> implements Serializable {
private T data;

public static <T> ResponseEntity<ResponseDto<T>> ok(T data) {
return ResponseEntity.ok(new ResponseDto<T>(data));
}

public static <T> ResponseEntity<ResponseDto<T>> created(T data) {
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseDto<T>(data));
}

public static ResponseEntity<Void> noContent() {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.config.database;
package com.urlshortener.config.database;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.config.database;
package com.urlshortener.config.database;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.config.scheduler;
package com.urlshortener.config.scheduler;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.service.urlshortener.config.web;
package com.urlshortener.config.web;

import com.urlshortener.ratelimit.interceptor.ClientIdInterceptor;
import lombok.RequiredArgsConstructor;
import org.service.urlshortener.ratelimit.interceptor.ClientIdInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.service.urlshortener.error;

package com.urlshortener.error;

import lombok.extern.slf4j.Slf4j;
import org.service.urlshortener.error.dto.ErrorMessage;
import org.service.urlshortener.error.dto.ErrorResponseDto;
import org.service.urlshortener.error.exception.BusinessException;
import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.dto.ErrorResponseDto;
import com.urlshortener.error.exception.BusinessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.error.dto;
package com.urlshortener.error.dto;

import lombok.Getter;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.error.dto;
package com.urlshortener.error.dto;

import lombok.Data;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.service.urlshortener.error.exception;
package com.urlshortener.error.exception;


import lombok.Getter;
import org.service.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.dto.ErrorMessage;

@Getter
public class BusinessException extends RuntimeException{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.service.urlshortener.error.exception;
package com.urlshortener.error.exception;

import org.service.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.dto.ErrorMessage;

public class InvalidJsonDataException extends BusinessException {
public InvalidJsonDataException(ErrorMessage message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.urlshortener.error.exception.health;


import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.BusinessException;

public class ExampleException extends BusinessException {
public ExampleException(ErrorMessage message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.service.urlshortener.error.exception.url;
package com.urlshortener.error.exception.url;

import org.service.urlshortener.error.dto.ErrorMessage;
import org.service.urlshortener.error.exception.BusinessException;
import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.BusinessException;

public class NotFinishRemoveSixMonthsOldDataException extends BusinessException {
public NotFinishRemoveSixMonthsOldDataException(ErrorMessage message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.urlshortener.error.exception.url;

import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.BusinessException;

public class NotFoundUrlException extends BusinessException {
public NotFoundUrlException(ErrorMessage message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.urlshortener.error.exception.url;

import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.BusinessException;

public class RateLimitExceededException extends BusinessException {

public RateLimitExceededException(ErrorMessage message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.service.urlshortener.health.controller;
package com.urlshortener.health.controller;

import org.service.urlshortener.common.response.ResponseDto;
import org.service.urlshortener.common.response.ResponseMessage;
import com.urlshortener.common.response.ResponseDto;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -10,6 +9,6 @@
public class HealthRestController {
@GetMapping("/api/v1/health")
public ResponseEntity<?> getHealth() {
return ResponseDto.toResponseEntity(ResponseMessage.SUCCESS, "health good~~!");
return ResponseDto.ok("health good~~!");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.health.controller;
package com.urlshortener.health.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.service.urlshortener.ratelimit.interceptor;
package com.urlshortener.ratelimit.interceptor;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.service.urlshortener.error.dto.ErrorMessage;
import org.service.urlshortener.error.exception.url.RateLimitExceededException;
import org.service.urlshortener.ratelimit.service.RateLimitService;
import com.urlshortener.error.dto.ErrorMessage;
import com.urlshortener.error.exception.url.RateLimitExceededException;
import com.urlshortener.ratelimit.service.RateLimitService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.ratelimit.service;
package com.urlshortener.ratelimit.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.service.urlshortener.shortener.comtroller.rest;
package com.urlshortener.shortener.comtroller.rest;

import com.urlshortener.common.response.ResponseDto;
import com.urlshortener.shortener.dto.request.OriginUrlRequest;
import com.urlshortener.shortener.dto.request.ShortCodeRequest;
import com.urlshortener.shortener.service.ShortenerService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.service.urlshortener.common.response.ResponseDto;
import org.service.urlshortener.common.response.ResponseMessage;
import org.service.urlshortener.shortener.domain.vo.UrlDomain;
import org.service.urlshortener.shortener.dto.request.OriginUrlRequest;
import org.service.urlshortener.shortener.dto.request.ShortCodeRequest;
import org.service.urlshortener.shortener.service.ShortenerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.io.IOException;

Expand All @@ -31,10 +28,10 @@ public class ShortenerRestController {
public ResponseEntity<?> createShortUrl(
@RequestBody OriginUrlRequest originUrlRequest
) {
var shortUrl = UrlDomain.URL + shortenerService.createShortUrl(originUrlRequest).getShortCode();
var shortUrl = shortenerService.createShortUrl(originUrlRequest).getShortCode();
log.debug("short={}", shortUrl);

return ResponseDto.toResponseEntity(ResponseMessage.SUCCESS, shortUrl);
return ResponseDto.created(shortUrl);
}

/**
Expand All @@ -46,21 +43,13 @@ public ResponseEntity<?> createShortUrl(
*/
@GetMapping("{shortCode}")
public void getOriginUrl(
@PathVariable("shortCode") String shortCode,
@PathVariable("shortCode") ShortCodeRequest shortCode,
HttpServletResponse response
) throws IOException {
log.debug("shortCode = {}", shortCode);
var originUrl = shortenerService.getOriginUrl(new ShortCodeRequest(shortCode)).getOriginUrl();
var originUrl = shortenerService.getOriginUrl(shortCode).getOriginUrl();
log.debug("originUrl = {}", originUrl);

response.sendRedirect(originUrl);
}

/**
* 메인 페이지로 리다이렉드하는 API
*/
@GetMapping()
public RedirectView getPage() {
return new RedirectView("/main");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.service.urlshortener.shortener.comtroller.view;
package com.urlshortener.shortener.comtroller.view;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
Expand All @@ -8,10 +8,18 @@
@Controller
public class ShortenerViewController {
/**
* @return (main.jsp) 메인 화면을 반환합니다.
* @return 메인 화면을 반환합니다.
*/
@GetMapping("/main")
public String page() {
return "main";
}

/**
* @return 메인 화면을 반환합니다.
*/
@GetMapping("/")
public String homePage() {
return "main";
}
}
Loading

0 comments on commit ec545e4

Please sign in to comment.