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

[8주차/피우] 워크북 제출합니다. #30

Merged
merged 2 commits into from
Nov 25, 2024
Merged
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,23 @@ public enum ErrorStatus implements BaseErrorCode {
ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "ARTICLE4001", "게시글이 없습니다."),

// For test
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트");
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "이거는 테스트"),

// 음식 카테고리 관련 에러
FOOD_CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, "FOOD4001", "음식 카테고리가 없습니다."),

// 지역 관련 에러
REGION_NOT_FOUND(HttpStatus.NOT_FOUND, "REGION4001", "지역이 존재하지 않습니다."),

// 식당 관련 에러
RESTAURANT_NOT_FOUND(HttpStatus.NOT_FOUND, "RESTAURANT4001", "식당이 존재하지 않습니다."),

// 파일 등록 에러
FILE_EXCEPTION(HttpStatus.BAD_REQUEST, "FILE4001", "파일이 등록되지 않았습니다."),

// 미션 관련 에러
ALREADY_CHALLENGE(HttpStatus.BAD_REQUEST, "MISSION4001", "이미 수행 중인 미션입니다.");

private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package javalab.umc7th_mission.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;

@Component
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {

public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
protected boolean canWrite(MediaType mediaType) {
return false;
}
}
39 changes: 39 additions & 0 deletions src/main/java/javalab/umc7th_mission/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package javalab.umc7th_mission.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";
// API 요청헤더에 인증정보 포함
SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName);
// SecuritySchemes 등록
Components components = new Components()
.addSecuritySchemes(jwtSchemeName, new SecurityScheme()
.name(jwtSchemeName)
.type(SecurityScheme.Type.HTTP) // HTTP 방식
.scheme("bearer")
.bearerFormat("JWT"));

return new OpenAPI()
.addServersItem(new Server().url("/"))
.info(info)
.addSecurityItem(securityRequirement)
.components(components);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.FoodCategory;
import javalab.umc7th_mission.domain.mapping.MemberFood;

import java.util.List;

public class MemberFoodConverter {

public static List<MemberFood> toMemberPreferList(List<FoodCategory> foodCategoryList){
return foodCategoryList.stream()
.map(foodCategory ->
MemberFood.builder()
.foodCategory(foodCategory)
.build()
).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Mission;
import javalab.umc7th_mission.domain.mapping.MemberMission;
import javalab.umc7th_mission.web.dto.Mission.MissionRequestDTO;
import javalab.umc7th_mission.web.dto.Mission.MissionResponseDTO;

public class MissionConverter {

public static MissionResponseDTO.JoinResultDTO toJoinResultDTO(Mission mission){
return MissionResponseDTO.JoinResultDTO.builder()
.missionId(mission.getId())
.createdAt(mission.getCreatedAt())
.build();
}

public static Mission toMission(MissionRequestDTO.JoinDto request){
return Mission.builder()
.content(request.getContent())
.point(request.getPoint())
.deadline(request.getDeadline())
.build();
}

public static MissionResponseDTO.ChallengeResultDTO toChallengeResultDTO(MemberMission memberMission) {
return MissionResponseDTO.ChallengeResultDTO.builder()
.memberMissionId(memberMission.getId())
.createdAt(memberMission.getCreatedAt())
.build();
}

public static MemberMission toMemberMission(MissionRequestDTO.ChallengeDto request) {
return MemberMission.builder()
.status(request.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.Restaurant;
import javalab.umc7th_mission.web.dto.Restaurant.RestaurantRequestDTO;
import javalab.umc7th_mission.web.dto.Restaurant.RestaurantResponseDTO;

public class RestaurantConverter {

public static RestaurantResponseDTO.JoinResultDTO toJoinResultDTO(Restaurant restaurant){
return RestaurantResponseDTO.JoinResultDTO.builder()
.restaurantId(restaurant.getId())
.createdAt(restaurant.getCreatedAt())
.build();
}

public static Restaurant toRestaurant(RestaurantRequestDTO.JoinDto request){
return Restaurant.builder()
.name(request.getRestaurantName())
.score(0.0f)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package javalab.umc7th_mission.converter;

import javalab.umc7th_mission.domain.*;
import javalab.umc7th_mission.web.dto.Review.ReviewRequestDTO;
import javalab.umc7th_mission.web.dto.Review.ReviewResponseDTO;

public class ReviewConverter {

public static ReviewResponseDTO.JoinResultDTO toJoinResultDTO(Review review){
return ReviewResponseDTO.JoinResultDTO.builder()
.reviewId(review.getId())
.createdAt(review.getCreatedAt())
.build();
}

public static Review toReview(ReviewRequestDTO.JoinDto request){
return Review.builder()
.content(request.getContent())
.score(request.getScore())
.build();
}
}
8 changes: 8 additions & 0 deletions src/main/java/javalab/umc7th_mission/domain/Mission.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public class Mission extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "region_id")
private Region region;

public void setRestaurant(Restaurant restaurant) {
this.restaurant = restaurant;
}

public void setRegion(Region region) {
this.region = region;
}
}
10 changes: 10 additions & 0 deletions src/main/java/javalab/umc7th_mission/domain/Restaurant.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ public class Restaurant extends BaseEntity {
private Region region;

@OneToMany(mappedBy = "restaurant")
@Builder.Default
private List<Review> reviewList = new ArrayList<>();

@OneToMany(mappedBy = "restaurant")
@Builder.Default
private List<Mission> missionList = new ArrayList<>();

public void setRegion(Region region) {
this.region = region;
}

public void setFoodCategory(FoodCategory foodCategory) {
this.foodCategory = foodCategory;
}
}
22 changes: 18 additions & 4 deletions src/main/java/javalab/umc7th_mission/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,29 @@ public class Review extends BaseEntity {
@NotNull
private String content;

@OneToMany(mappedBy = "review")
@Builder.Default
private List<ReviewImage> reviewImageList = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant_id")
private Restaurant restaurant;

@OneToMany(mappedBy = "review", cascade = CascadeType.ALL)
@Builder.Default
private List<ReviewImage> reviewImageList = new ArrayList<>();

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

public void setRestaurant(Restaurant restaurant) {
this.restaurant = restaurant;
}

// 연관관계 편의 메서드
public void addImage(ReviewImage image) {
this.reviewImageList.add(image);
image.setReview(this);
}
}
4 changes: 4 additions & 0 deletions src/main/java/javalab/umc7th_mission/domain/ReviewImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public class ReviewImage extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "review_id")
private Review review;

public void setReview(Review review) {
this.review = review;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package javalab.umc7th_mission.domain.enums;

public enum Gender {
MALE, FEMALE
MALE, FEMALE, NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public class MemberMission extends BaseEntity {

@Enumerated(EnumType.STRING)
private MissionStatus status;

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

public void setMission(Mission mission) {
this.mission = mission;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package javalab.umc7th_mission.repository.FoodCategoryRepository;

import javalab.umc7th_mission.domain.FoodCategory;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FoodCategoryRepository extends JpaRepository<FoodCategory, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberMissionRepository extends JpaRepository<MemberMission, Long>, MemberMissionRepositoryCustom {
boolean existsByMemberIdAndMissionId(Long memberId, Long missionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import javalab.umc7th_mission.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {
boolean existsByEmail(String email);
Optional<Member> findByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import ext.javalab.umc7th_mission.domain.QMission;
import ext.javalab.umc7th_mission.domain.QRestaurant;
import ext.javalab.umc7th_mission.domain.mapping.QMemberMission;
import javalab.umc7th_mission.web.dto.MissionDTO;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package javalab.umc7th_mission.repository.RegionRepository;

import javalab.umc7th_mission.domain.Region;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface RegionRepository extends JpaRepository<Region, Long> {
boolean existsByName(String name);
Optional<Region> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package javalab.umc7th_mission.repository.RestaurantRepository;

import javalab.umc7th_mission.domain.Restaurant;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RestaurantRepository extends JpaRepository<Restaurant, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package javalab.umc7th_mission.service.FileService;

import javalab.umc7th_mission.apiPayload.code.status.ErrorStatus;
import javalab.umc7th_mission.apiPayload.exception.GeneralException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

@Service
public class FileService {
private static final String UPLOAD_DIR = "C:\\Users\\User\\Desktop\\images";

public String saveFile(MultipartFile file) {
try {
String fileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
Path filePath = Paths.get(UPLOAD_DIR, fileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return filePath.toString();
} catch (IOException e) {
throw new GeneralException(ErrorStatus.FILE_EXCEPTION);
}
}
}
Loading