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

코드 리뷰~ #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
build/
out/
.gradle
.idea

src/main/resources/application-db.yml
src/main/resources/application.properties
src/main/resources/application.yml
18 changes: 18 additions & 0 deletions src/main/java/kr/where/backend/BackendApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.where.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableRetry
@SpringBootApplication
public class BackendApplication {

public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}

}
16 changes: 16 additions & 0 deletions src/main/java/kr/where/backend/exception/CustomException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.where.backend.exception;

import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class CustomException extends RuntimeException{
private final int errorCode;
private final String errorMessage;

public <T extends Enum<T> & ErrorCode> CustomException(final T errorType) {
errorCode = errorType.getErrorCode();
errorMessage = errorType.getErrorMessage();
}
}
6 changes: 6 additions & 0 deletions src/main/java/kr/where/backend/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.where.backend.exception;

public interface ErrorCode {
int getErrorCode();
String getErrorMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package kr.where.backend.exception;

import kr.where.backend.auth.authUser.exception.AuthUserException;
import kr.where.backend.exception.httpError.HttpResourceErrorCode;
import kr.where.backend.exception.httpError.HttpResourceException;
import kr.where.backend.group.exception.GroupException;
import kr.where.backend.group.exception.GroupMemberException;
import kr.where.backend.join.exception.JoinException;
import kr.where.backend.jwt.exception.JwtException;
import kr.where.backend.member.exception.MemberException;
import kr.where.backend.api.exception.JsonException;
import kr.where.backend.api.exception.RequestException;
import kr.where.backend.oauthtoken.exception.OAuthTokenException;
import kr.where.backend.search.exception.SearchException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ExceptionHandleController {

@ExceptionHandler({MemberException.NoMemberException.class, GroupException.NoGroupException.class})
public ResponseEntity<String> handleNoResultException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.toString());
}

@ExceptionHandler({MemberException.DuplicatedMemberException.class, GroupException.DuplicatedGroupNameException.class,
GroupMemberException.class})
public ResponseEntity<String> handleDuplicatedException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.CONFLICT).body(e.toString());
}

@ExceptionHandler(GroupException.CannotModifyGroupException.class)
public ResponseEntity<String> handleCannotModifiedException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.toString());
}

@ExceptionHandler(OAuthTokenException.class)
public ResponseEntity<String> handleOAuthTokenException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.toString());
}

@ExceptionHandler(JwtException.class)
public ResponseEntity<String> handleJwtTokenException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.toString());
}

@ExceptionHandler(RequestException.class)
public ResponseEntity<String> handleBadRequestException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.toString());
}

@ExceptionHandler(JsonException.class)
public ResponseEntity<String> handleJsonException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.toString());
}

@ExceptionHandler(JoinException.class)
public ResponseEntity<String> handleJoinException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.toString());
}

@ExceptionHandler(SearchException.class)
public ResponseEntity<String> handleSearchException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.toString());
}

@ExceptionHandler(AuthUserException.class)
public ResponseEntity<String> handleAuthUserException(final CustomException e) {
log.info(e.toString());

return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.toString());
}

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<String> handleMissingParameterException() {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(HttpResourceException.of(HttpResourceErrorCode.NO_PARAMETERS));
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<String> handleNoRequestBodyException() {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(HttpResourceException.of(HttpResourceErrorCode.NO_REQUEST_BODY));
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<String> handleUnsupportedMethodException() {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(HttpResourceException.of(HttpResourceErrorCode.NO_SUPPORTED_METHOD));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> handleMethodArgumentNotValidException() {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(HttpResourceException.of(HttpResourceErrorCode.NOT_METHOD_VALID_ARGUMENT));
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleNoResourceException() {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("관리자에게 요청하세요.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.where.backend.exception.httpError;

import kr.where.backend.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum HttpResourceErrorCode implements ErrorCode {

NO_PARAMETERS(1700, "파라미터가 없는 Api 요청입니다."),
NO_REQUEST_BODY(1701, "Request body가 없는 Api 요청입니다."),
NO_SUPPORTED_METHOD(1702, "지원하지 않는 Http Method 요청입니다."),
NOT_METHOD_VALID_ARGUMENT(1703, "Method에 맞는 arguments가 없습니다");

private final int errorCode;
private final String errorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.where.backend.exception.httpError;

import kr.where.backend.exception.ErrorCode;

public class HttpResourceException {
public static String of(final ErrorCode errorCode) {
return "CustomException(errorCode=" + errorCode.getErrorCode() +
", " + "errorMessage=" +
errorCode.getErrorMessage() + ")";
}
}
88 changes: 88 additions & 0 deletions src/main/java/kr/where/backend/location/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package kr.where.backend.location;

import jakarta.persistence.*;
import kr.where.backend.member.Member;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Slf4j
public class Location {

@Id
@Column(name = "location_id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long locationId;

@OneToOne(mappedBy = "location", fetch = FetchType.LAZY)
private Member member;

@Column(length = 30)
private String customLocation;

@Column(length = 10)
private String imacLocation;

private LocalDateTime customUpdatedAt;

private LocalDateTime imacUpdatedAt;

public Location(final Member member, final String imacLocation) {
this.member = member;
this.imacLocation = imacLocation;
this.imacUpdatedAt = LocalDateTime.now();
}

public Location(final String imacLocation) {
this.imacLocation = imacLocation;
this.imacUpdatedAt = LocalDateTime.now();
}

public void setMember(Member member) {
this.member = member;
}

public void setCustomLocation(final String customLocation) {
this.customLocation = customLocation;
this.customUpdatedAt = LocalDateTime.now();
}

public void setImacLocation(final String imacLocation) {
this.imacLocation = imacLocation;
this.imacUpdatedAt = LocalDateTime.now();
}

public void initLocation() {
this.imacLocation = null;
this.imacUpdatedAt = LocalDateTime.now();
this.customLocation = null;
this.customUpdatedAt = LocalDateTime.now();
}

public String getLocation() {
if (!this.member.isAgree()) {
return this.imacLocation;
} else {
if (customLocation == null && imacLocation == null) {
return null;
} else if (customLocation == null) {
return imacLocation;
} else if (imacLocation == null) {
return customLocation;
} else {
if (customUpdatedAt.isAfter(imacUpdatedAt)) {
return customLocation;
} else {
return imacLocation;
}
}
}
}

}
54 changes: 54 additions & 0 deletions src/main/java/kr/where/backend/location/LocationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kr.where.backend.location;

import jakarta.validation.Valid;
import kr.where.backend.auth.authUser.AuthUser;
import kr.where.backend.location.dto.ResponseLocationDTO;
import kr.where.backend.location.dto.UpdateCustomLocationDTO;
import kr.where.backend.location.swagger.LocationApiDocs;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/v3/location")
@RequiredArgsConstructor
public class LocationController implements LocationApiDocs {

private final LocationService locationService;

/**
* 수동자리 업데이트
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수동자리 업데이트는 유저가 수동으로 자리 설정 할 때 호출되는 api인가요??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 맞습니다~

*
* @param updateCustomLocation
* @return ResponseEntity(responseLocationDTO)
*/
@PostMapping("/custom")
public ResponseEntity<ResponseLocationDTO> updateCustomLocation(@RequestBody @Valid final UpdateCustomLocationDTO updateCustomLocation) {
final AuthUser authUser = AuthUser.of();
final ResponseLocationDTO responseLocationDto = locationService.updateCustomLocation(updateCustomLocation,
authUser);

return ResponseEntity.ok(responseLocationDto);
}

/**
* 수동자리 초기화(삭제)
*
* @return ResponseEntity(responseLocationDTO)
*/
@DeleteMapping("/custom")
public ResponseEntity<ResponseLocationDTO> deleteCustomLocation() {
final AuthUser authUser = AuthUser.of();
final ResponseLocationDTO responseLocationDto = locationService.deleteCustomLocation(authUser);

return ResponseEntity.ok(responseLocationDto);
}

}
11 changes: 11 additions & 0 deletions src/main/java/kr/where/backend/location/LocationRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.where.backend.location;

import kr.where.backend.member.Member;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
Location findByMember(Member member);
}
Loading