Skip to content

Commit

Permalink
Merge pull request #18 from Weflo-B/feature/#15/insurance
Browse files Browse the repository at this point in the history
[feat] : 메시지 Enum 추가 & 보험 API 구현 완료
  • Loading branch information
bbbang105 authored Mar 8, 2024
2 parents 133afdc + b6dd84c commit 73fb290
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package Weflo.backend.controller.insurance;

import Weflo.backend.dto.user.response.InsuranceResponse;
import Weflo.backend.global.ApiResponse;
import Weflo.backend.global.status.Message;
import Weflo.backend.service.insurance.InsuranceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/api")
@RequestMapping(value = "/api/insurance")
public class InsuranceController {
private final InsuranceService insuranceService;
@GetMapping("/{user_id}")
public ApiResponse<InsuranceResponse> getInsuranceDetails(@PathVariable("user_id") Long userId) {
InsuranceResponse insuranceResponse = insuranceService.getInsuranceDetails(userId);

return ApiResponse.onSuccess(Message._GET_INSURANCE_MESSAGE.getMessage(), insuranceResponse);
}

@PostMapping("join/{user_id}")
public ApiResponse<InsuranceResponse> joinInsurance(@PathVariable("user_id") Long userId) {
InsuranceResponse insuranceResponse = insuranceService.joinInsurance(userId);

}
return ApiResponse.onSuccessCreated(Message._JOIN_SUCCESS_MESSAGE.getMessage(), insuranceResponse);
}
}
5 changes: 4 additions & 1 deletion src/main/java/Weflo/backend/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ public class User {
private String address;

@Column(name = "is_join")
private boolean isJoin;
private Boolean isJoin;

@Column(name = "join_date")
private LocalDate joinDate;

@Column(name = "update_date")
private LocalDate updateDate;

@Column(name = "insurance_rate")
private Integer insuranceRate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class InsuranceResponse {
private boolean isJoin;
private Boolean isJoin;
private LocalDate joinDate;
private LocalDate updateDate;
private int insuranceRate;
private Integer insuranceRate;
}
20 changes: 20 additions & 0 deletions src/main/java/Weflo/backend/global/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public static <T> ApiResponse<T> onSuccess(String message, T data) {
return new ApiResponse<>(true, SuccessStatus._OK.getCode(), message, data);
}

//아래부터는 POST시 사용❗️
//data만 들어가는 응답 포맷
public static <T> ApiResponse<T> onSuccessCreated(T data) {
return new ApiResponse<>(true, SuccessStatus._CREATED.getCode(), SuccessStatus._OK.getMessage(), data);
}

public static <T> ApiResponse<T> onSuccessCreated(BaseCode code, T data) {
return new ApiResponse<>(true, code.getReasonHttpStatus().getCode(),
code.getReasonHttpStatus().getMessage(), data);
}

public static <T> ApiResponse<T> onSuccessCreated(HttpStatus data) {
return new ApiResponse<>(true, SuccessStatus._CREATED.getCode(), SuccessStatus._OK.getMessage(), null);
}

//message까지 같이 넣을 수 있는 응답 포맷
public static <T> ApiResponse<T> onSuccessCreated(String message, T data) {
return new ApiResponse<>(true, SuccessStatus._CREATED.getCode(), message, data);
}

public static <T> ApiResponse<T> onFailure(String code, String message, T data) {
return new ApiResponse<>(false, code, message, data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Weflo/backend/global/config/CorsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedOriginPatterns(Collections.singletonList("http://localhost:3000")); // ⭐️ 허용할 origin
config.setAllowedOriginPatterns(Collections.singletonList("*")); // ⭐️ 허용할 origin
config.setAllowCredentials(true);
return config;
};
Expand All @@ -36,7 +36,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize ->
authorize
.requestMatchers("/api").permitAll() // api로 시작하는 uri에 대한 요청 허용
.requestMatchers("/**").permitAll()
);

return httpSecurity.build();
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/Weflo/backend/global/status/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Weflo.backend.global.status;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum Message {
_GET_ALL_ORDER_PARTS_MESSAGE("주문할 모든 부품을 조회합니다."),
_GET_ORDER_PARTS_DETAILS_MESSAGE("해당 드론의 부품 주문을 상세 조회합니다."),
_GET_QUOTATION_MESSAGE("견적서를 조회합니다."),
_ORDER_SUCCESS_MESSAGE("부품 주문에 성공했습니다."),
_GET_ALL_ORDER_HISTORIES_MESSAGE("주문 배송 내역을 조회합니다."),
_GET_DELIVERY_DETAILS_MESSAGE("특정 배송의 상세 내역을 조회합니다."),
_CHANGE_DELIVERY_STATUS_MESSAGE("배송 상태 변경에 성공했습니다."),
_GET_INSURANCE_MESSAGE("유저의 보험 페이지를 조회합니다."),
_JOIN_SUCCESS_MESSAGE("유저가 보험 가입에 성공했습니다."),
_GET_ALL_CHECK_HISTORIES_MESSAGE("모든 수리/점검 내역을 조회합니다."),
_GET_CHECK_HISTORY_DETAILS_MESSAGE("특정 드론의 수리/점검 상세 내역을 조회합니다."),
_DISPOSE_SUCCESS_MESSAGE("드론 폐기에 성공했습니다.");

private final String message;
}
3 changes: 2 additions & 1 deletion src/main/java/Weflo/backend/global/status/SuccessStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@Getter
@AllArgsConstructor
public enum SuccessStatus implements BaseCode {
_OK(HttpStatus.OK, "200", "성공 입니다.");
_OK(HttpStatus.OK, "200", "성공 입니다."),
_CREATED(HttpStatus.CREATED, "201", "성공적으로 생성되었습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Weflo.backend.repository.user;

import Weflo.backend.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Weflo.backend.service.insurance;

import Weflo.backend.dto.user.response.InsuranceResponse;

public interface InsuranceService {
InsuranceResponse getInsuranceDetails(Long userId);
InsuranceResponse joinInsurance(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Weflo.backend.service.insurance;

import Weflo.backend.domain.User;
import Weflo.backend.dto.user.response.InsuranceResponse;
import Weflo.backend.repository.user.UserRepository;
import com.amazonaws.services.kms.model.NotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
@RequiredArgsConstructor
public class InsuranceServiceImpl implements InsuranceService {
private final UserRepository userRepository;

@Override
public InsuranceResponse getInsuranceDetails(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("User not found"));

return InsuranceResponse.builder()
.isJoin(user.getIsJoin())
.joinDate(user.getJoinDate())
.updateDate(user.getUpdateDate())
.insuranceRate(user.getInsuranceRate())
.build();
}

@Override
public InsuranceResponse joinInsurance(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException("User not found"));

LocalDate now = LocalDate.now();

User newUserInfo = User.builder()
.id(user.getId())
.loginId(user.getLoginId())
.loginPassword(user.getLoginPassword())
.name(user.getName())
.tel(user.getTel())
.address(user.getAddress())
.isJoin(true)
.joinDate(now)
.updateDate(now.plusYears(3))
.insuranceRate(10)
.build();

userRepository.save(newUserInfo);

return InsuranceResponse.builder()
.isJoin(newUserInfo.getIsJoin())
.joinDate(newUserInfo.getJoinDate())
.updateDate(newUserInfo.getUpdateDate())
.insuranceRate(newUserInfo.getInsuranceRate())
.build();
}

}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring:

jpa:
hibernate:
ddl-auto: create
ddl-auto: update
dialect: org.hibernate.dialect.MySQL8Dialect
properties:
hibernate:
Expand Down

0 comments on commit 73fb290

Please sign in to comment.