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

[9주차/리키] 워크북 제출합니다. #44

Open
wants to merge 2 commits into
base: leeky/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package javalab.umc7th_mission;
package javalab;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/javalab/global/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package javalab.global.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

@Bean
public OpenAPI UMCstudyAPI() {
Info info = new Info()
.title("UMC Server WorkBook API")
.description("UMC Server WorkBook API 명세서")
.version("1.0.0");

String jwtSchemeName = "JWT TOKEN";

SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName);
Components components = new Components()
.addSecuritySchemes(jwtSchemeName, new SecurityScheme()
.name(jwtSchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT"));

return new OpenAPI()
.addServersItem(new Server().url("/"))
.info(info)
.addSecurityItem(securityRequirement)
.components(components);
}
}
22 changes: 22 additions & 0 deletions src/main/java/javalab/global/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package javalab.global.config;

import javalab.global.validation.resolver.PageArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
private final PageArgumentResolver pageArgumentResolver;

public WebConfig(PageArgumentResolver pageArgumentResolver) {
this.pageArgumentResolver = pageArgumentResolver;
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(pageArgumentResolver);
}
}
18 changes: 18 additions & 0 deletions src/main/java/javalab/global/validation/annotation/CheckPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package javalab.global.validation.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import javalab.global.validation.validator.PageValidator;

import java.lang.annotation.*;

@Documented
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PageValidator.class)
public @interface CheckPage {
String message() default "page 번호가 잘못되었습니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package javalab.global.validation.annotation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import javalab.global.validation.validator.CategoriesExistValidator;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = CategoriesExistValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExistCategories {

String message() default "해당하는 카테고리가 존재하지 않습니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package javalab.global.validation.resolver;

import javalab.global.validation.annotation.CheckPage;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class PageArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(CheckPage.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String pageParam = webRequest.getParameter(parameter.getParameterName());
if (pageParam == null) {
return 0;
}

int page = Integer.parseInt(pageParam);
if (page < 1) {
throw new IllegalArgumentException("페이지 번호가 너무 작습니다.");
}

return page - 1;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package javalab.global.validation.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import javalab.global.apiPayload.code.status.ErrorStatus;
import javalab.global.validation.annotation.ExistCategories;
import javalab.umc7th_mission.repository.CategoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CategoriesExistValidator implements ConstraintValidator<ExistCategories, List<Long>> {

private final CategoryRepository categoryRepository;

@Override
public void initialize(ExistCategories constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

@Override
public boolean isValid(List<Long> longs, ConstraintValidatorContext constraintValidatorContext) {
boolean isValid = longs.stream()
.allMatch(value -> categoryRepository.existsById(value));

if (!isValid) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate(ErrorStatus.CATEGORY_NOT_FOUND.toString())
.addConstraintViolation();
}
return isValid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package javalab.global.validation.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import javalab.global.validation.annotation.CheckPage;

public class PageValidator implements ConstraintValidator<CheckPage, Integer> {

@Override
public void initialize(CheckPage constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

@Override
public boolean isValid(Integer page, ConstraintValidatorContext constraintValidatorContext) {
if (page == null) {
return true;
}

return page >= 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.web.dto.request.MemberRequestDTO;
import javalab.umc7th_mission.web.dto.response.MemberResponseDTO;

public class MemberConverter {

public static MemberResponseDTO.JoinResultDTO toJoinResult(Member member) {
return MemberResponseDTO.JoinResultDTO.builder()
.memberId(member.getId())
.createdAt(member.getCreateAt())
.build();
}

public static Member toEntity(MemberRequestDTO.JoinDto request) {
return Member.builder()
.address(request.getAddress())
.birth(request.getBirth())
.gender(request.getGender())
.name(request.getName())
.point(0l)
.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.enums.Status;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import javalab.umc7th_mission.web.dto.request.MissionRequestDTO;
import javalab.umc7th_mission.web.dto.response.MissionResponseDTO;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MissionConverter {

public Mission toEntity(MissionRequestDTO.AddMission request, Restaurant restaurant) {
return Mission.builder()
.restaurant(restaurant)
.content(request.getContent())
.reward(request.getReward())
.build();
}

public MissionResponseDTO.AddMission toAddMissionResponse(Mission mission) {
return MissionResponseDTO.AddMission.builder()
.missionId(mission.getId())
.restaurantId(mission.getRestaurant().getId())
.createdAt(mission.getCreateAt())
.build();
}

public MemberMission toMMEntity(MissionRequestDTO.ActiveMission request, Member member,Mission mission) {
return MemberMission.builder()
.member(member)
.mission(mission)
.status(Status.ACTIVE)
.classificationNumber(request.getClassificationNumber())
.deadline(request.getDeadline())
.build();
}

public MissionResponseDTO.ActiveMission toActiveMission(MemberMission memberMission) {
return MissionResponseDTO.ActiveMission.builder()
.missionId(memberMission.getMission().getId())
.deadline(memberMission.getDeadline())
.build();
}

public MissionResponseDTO.ActiveMissionList toMissionInfoList(Page<MemberMission> memberMissions) {
List<MissionResponseDTO.MissionInfo> list = memberMissions.stream().map(
mm -> MissionResponseDTO.MissionInfo.builder()
.restaurantId(mm.getMission().getRestaurant().getId())
.restaurantName(mm.getMission().getRestaurant().getName())
.content(mm.getMission().getContent())
.reward(mm.getMission().getReward())
.deadline(mm.getDeadline()).build()
).toList();

return MissionResponseDTO.ActiveMissionList.builder()
.missionInfos(list)
.build();
}

public MissionResponseDTO.CompleteMission toCompleteMission(MemberMission memberMission) {
return MissionResponseDTO.CompleteMission.builder()
.memberId(memberMission.getMember().getId())
.missionId(memberMission.getMission().getId())
.status(memberMission.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.web.dto.request.RestaurantRequestDTO;
import javalab.umc7th_mission.web.dto.response.RestaurantResponseDTO;
import org.springframework.stereotype.Component;

@Component
public class RestaurantConverter {
public Restaurant toEntity(RestaurantRequestDTO.AddRestaurant request) {
return Restaurant.builder()
.name(request.getName())
.address(request.getAddress())
.type(request.getType())
.build();
}

public RestaurantResponseDTO.AddRestaurant toAddRestaurantResponse(Restaurant restaurant) {
return RestaurantResponseDTO.AddRestaurant.builder()
.restaurantId(restaurant.getId())
.address(restaurant.getAddress())
.createdAt(restaurant.getCreateAt())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Member;
import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.domain.Review;
import javalab.umc7th_mission.web.dto.request.ReviewRequestDTO;
import javalab.umc7th_mission.web.dto.response.ReviewResponseDTO;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReviewConverter {

public Review toEntity(ReviewRequestDTO.AddReview request, Member member, Restaurant restaurant) {
return Review.builder()
.member(member)
.restaurant(restaurant)
.content(request.getContent())
.score(request.getScore())
.build();
}

public ReviewResponseDTO.addReview toAddReviewResponse(Review review) {
return ReviewResponseDTO.addReview.builder()
.reviewId(review.getId())
.createdAt(review.getCreateAt())
.build();
}

public ReviewResponseDTO.getAllMyReview toAllMyReview(Page<Review> reviews, Member member) {
List<ReviewResponseDTO.ReviewInfo> list = reviews.stream().map(
review -> ReviewResponseDTO.ReviewInfo.builder()
.storeName(review.getRestaurant().getName())
.score(review.getScore())
.content(review.getContent())
.createAt(review.getCreateAt())
.build())
.toList();

return ReviewResponseDTO.getAllMyReview.builder()
.memberId(member.getId())
.memberName(member.getName())
.reviewInfoList(list)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MemberMission {

@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(10) DEFAULT 'NEW'")
@Setter
private Status status;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package javalab.umc7th_mission.repository;

import javalab.umc7th_mission.domain.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
Loading