From c69eba7f27465b03e6d7d91b8551414452ad084e Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Fri, 26 Jul 2024 15:47:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=AC=B8=ED=99=94=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문화 등록할 때 쓸 간단한 등록 api 추가함 --- .../api/culture/CreateCultureController.java | 47 +++++++++++++++++++ .../MZConnent/config/SecurityConfig.java | 2 +- .../MZConnent/domain/culture/Culture.java | 4 +- .../domain/culture/CultureCategory.java | 3 -- .../culture/request/CreateCultureRequest.java | 25 ++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/main/java/likelion/MZConnent/api/culture/CreateCultureController.java create mode 100644 src/main/java/likelion/MZConnent/dto/culture/request/CreateCultureRequest.java diff --git a/src/main/java/likelion/MZConnent/api/culture/CreateCultureController.java b/src/main/java/likelion/MZConnent/api/culture/CreateCultureController.java new file mode 100644 index 0000000..3f59b94 --- /dev/null +++ b/src/main/java/likelion/MZConnent/api/culture/CreateCultureController.java @@ -0,0 +1,47 @@ +package likelion.MZConnent.api.culture; + +import likelion.MZConnent.domain.culture.Culture; +import likelion.MZConnent.domain.culture.CultureCategory; +import likelion.MZConnent.dto.culture.request.CreateCultureRequest; +import likelion.MZConnent.repository.culture.CultureCategoryRepository; +import likelion.MZConnent.repository.culture.CultureRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; +import java.util.Random; + +// 문화 추가용 +@RestController +@Slf4j +@RequiredArgsConstructor +public class CreateCultureController { + private final CultureRepository cultureRepository; + private final CultureCategoryRepository cultureCategoryRepository; + + @PostMapping("/api/cultures") + public ResponseEntity createCulture(@RequestBody CreateCultureRequest request) { + + log.info("culture request: {}", request); + + CultureCategory cultureCategory = cultureCategoryRepository.findById(request.getCultureCategoryId()).orElseThrow(()-> new IllegalArgumentException("해당하는 카테고리가 존재하지 않습니다.")); + + Culture newCulture = Culture.builder() + .name(request.getName()) + .summary(request.getSummary()) + .content(request.getContent()) + .cultureImageUrl(request + .getCultureImageUrl()) + .recommendedMember(request.getRecommendedMember()) + .cultureCategory(cultureCategory) + .build(); + + Culture savedCulture = cultureRepository.save(newCulture); + + return ResponseEntity.ok(savedCulture); + } +} diff --git a/src/main/java/likelion/MZConnent/config/SecurityConfig.java b/src/main/java/likelion/MZConnent/config/SecurityConfig.java index 7bb03f8..8ea181d 100644 --- a/src/main/java/likelion/MZConnent/config/SecurityConfig.java +++ b/src/main/java/likelion/MZConnent/config/SecurityConfig.java @@ -28,7 +28,7 @@ public class SecurityConfig { private final String[] adminUrl = {"/admin/**"}; private final String[] permitAllUrl = {"/error", "/api/auth/login", "/api/auth/logout", // 회원 - "/api/categories/culture", // 문화 + "/api/categories/culture", "/api/cultures", // 문화 "/api/clubs", "/api/clubs/*/join", // 동아리 "/api/clubs/*", "/swagger", "/swagger-ui.html", "/swagger-ui/**", "/api-docs", "/api-docs/**", "/v3/api-docs/**", //swagger diff --git a/src/main/java/likelion/MZConnent/domain/culture/Culture.java b/src/main/java/likelion/MZConnent/domain/culture/Culture.java index a61e21b..34e08e5 100644 --- a/src/main/java/likelion/MZConnent/domain/culture/Culture.java +++ b/src/main/java/likelion/MZConnent/domain/culture/Culture.java @@ -18,7 +18,7 @@ public class Culture { private Long cultureId; @Column(nullable = false) - private int interestCount; + private int interestCount = 0; @Column(columnDefinition = "TEXT", nullable = false) private String content; @@ -33,7 +33,7 @@ public class Culture { private String summary; @Column(nullable = false) - private int clubCount; + private int clubCount = 0; @Column(length = 100, nullable = false) private String recommendedMember; diff --git a/src/main/java/likelion/MZConnent/domain/culture/CultureCategory.java b/src/main/java/likelion/MZConnent/domain/culture/CultureCategory.java index 9df249b..f1da350 100644 --- a/src/main/java/likelion/MZConnent/domain/culture/CultureCategory.java +++ b/src/main/java/likelion/MZConnent/domain/culture/CultureCategory.java @@ -19,7 +19,4 @@ public class CultureCategory { @Column(length = 255, nullable = false) private String name; - - @OneToMany(mappedBy = "cultureCategory", cascade = CascadeType.ALL) - private List selfIntroductions = new ArrayList<>(); } diff --git a/src/main/java/likelion/MZConnent/dto/culture/request/CreateCultureRequest.java b/src/main/java/likelion/MZConnent/dto/culture/request/CreateCultureRequest.java new file mode 100644 index 0000000..30bfab2 --- /dev/null +++ b/src/main/java/likelion/MZConnent/dto/culture/request/CreateCultureRequest.java @@ -0,0 +1,25 @@ +package likelion.MZConnent.dto.culture.request; + +import jakarta.persistence.Column; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import likelion.MZConnent.domain.culture.CultureCategory; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class CreateCultureRequest { + private String name; + + private String summary; + + private String content; + + private String cultureImageUrl; + + private String recommendedMember; + + private Long cultureCategoryId; +} From 422ce22690e92be39856f5837f8c6bb4f293e73c Mon Sep 17 00:00:00 2001 From: chaeyoungeee Date: Fri, 26 Jul 2024 15:48:26 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 에러 메시지 출력하게 변경함 --- src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java b/src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java index d37a027..30d567d 100644 --- a/src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java +++ b/src/main/java/likelion/MZConnent/exception/ExceptionAdvice.java @@ -41,6 +41,7 @@ public ResponseEntity handleHttpMessageNotReadableException(Ex @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(RuntimeException.class) public ResponseEntity handleRuntimeException(RuntimeException e) { + log.info("error: {}", e); return new ResponseEntity<>( new ApiResponseJson(HttpStatus.INTERNAL_SERVER_ERROR, "서버에 오류가 발생했습니다."), HttpStatus.INTERNAL_SERVER_ERROR); }