Skip to content

Commit

Permalink
Merge pull request #61 from LIKELION-TEAM4-HACKATHON/feature/culture-…
Browse files Browse the repository at this point in the history
…detail

전체 문화 간단 조회 기능 추가
  • Loading branch information
chaeyoungeee authored Jul 27, 2024
2 parents da09f68 + 2e2ca3d commit d3ff62a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package likelion.MZConnent.api.culture;

import likelion.MZConnent.domain.club.RegionCategory;
import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.culture.CultureCategory;
import likelion.MZConnent.dto.culture.request.CreateCultureRequest;
import likelion.MZConnent.repository.club.RegionCategoryRepository;
import likelion.MZConnent.repository.culture.CultureCategoryRepository;
import likelion.MZConnent.repository.culture.CultureRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,6 +24,7 @@
public class CreateCultureController {
private final CultureRepository cultureRepository;
private final CultureCategoryRepository cultureCategoryRepository;
private final RegionCategoryRepository regionCategoryRepository;

@PostMapping("/api/cultures")
public ResponseEntity<Culture> createCulture(@RequestBody CreateCultureRequest request) {
Expand All @@ -30,6 +33,8 @@ public ResponseEntity<Culture> createCulture(@RequestBody CreateCultureRequest r

CultureCategory cultureCategory = cultureCategoryRepository.findById(request.getCultureCategoryId()).orElseThrow(()-> new IllegalArgumentException("해당하는 카테고리가 존재하지 않습니다."));

RegionCategory regionCategory = regionCategoryRepository.findById(request.getRegionId()).orElseThrow(()-> new IllegalArgumentException("해당하는 지역이 존재하지 않습니다."));

Culture newCulture = Culture.builder()
.name(request.getName())
.summary(request.getSummary())
Expand All @@ -38,6 +43,7 @@ public ResponseEntity<Culture> createCulture(@RequestBody CreateCultureRequest r
.getCultureImageUrl())
.recommendedMember(request.getRecommendedMember())
.cultureCategory(cultureCategory)
.region(regionCategory)
.build();

Culture savedCulture = cultureRepository.save(newCulture);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package likelion.MZConnent.api.culture;

import likelion.MZConnent.dto.culture.response.CultureCategoryResponse;
import likelion.MZConnent.dto.culture.response.CulturesSimpleResponse;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.service.culture.CultureCategoryService;
import likelion.MZConnent.service.culture.CultureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -14,11 +19,21 @@
public class CultureController {
private final CultureCategoryService cultureCategoryService;

private final CultureService cultureService;

// 전체 문화 카테고리 조회
@GetMapping("/api/categories/culture")
public ResponseEntity<CultureCategoryResponse> getAllCultureCategories() {
CultureCategoryResponse all = cultureCategoryService.getAllCultureCategories();
log.info("전체 문화 카테고리: {}", all.getCultureCategories());
return ResponseEntity.ok(all);
}

// 전체 문화 간단 조회
@GetMapping("/api/cultures")
ResponseEntity<PageContentResponse> getReviewSimpleList(@RequestParam(required = false, defaultValue = "0", value = "category") Long category, @RequestParam(required = false, defaultValue = "0", value = "page") int page ) {

PageContentResponse<CulturesSimpleResponse> response = cultureService.getCulturesSimpleList(category, page);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package likelion.MZConnent.domain.club;

import jakarta.persistence.*;
import likelion.MZConnent.domain.culture.Culture;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -20,5 +21,7 @@ public class RegionCategory {
@OneToMany(mappedBy = "region")
private List<Club> clubs;

@OneToMany(mappedBy = "region")
private List<Culture> cultures;
}

11 changes: 10 additions & 1 deletion src/main/java/likelion/MZConnent/domain/culture/Culture.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package likelion.MZConnent.domain.culture;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import likelion.MZConnent.domain.club.Club;
import likelion.MZConnent.domain.club.RegionCategory;
import likelion.MZConnent.domain.review.Review;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -42,6 +44,11 @@ public class Culture {
@JoinColumn(name = "cultureCategoryId", nullable = false)
private CultureCategory cultureCategory;

@ManyToOne
@JoinColumn(name = "region_id")
@JsonIgnore
private RegionCategory region;

@OneToMany(mappedBy = "culture")
private List<Club> clubs;

Expand All @@ -51,8 +58,9 @@ public class Culture {
@OneToMany(mappedBy = "culture")
private List<Review> reviews;


@Builder
public Culture(int interestCount, String content, String cultureImageUrl, String name, String summary, int clubCount, String recommendedMember, CultureCategory cultureCategory) {
public Culture(int interestCount, String content, String cultureImageUrl, String name, String summary, int clubCount, String recommendedMember, CultureCategory cultureCategory, RegionCategory region) {
this.interestCount = interestCount;
this.content = content;
this.cultureImageUrl = cultureImageUrl;
Expand All @@ -61,5 +69,6 @@ public Culture(int interestCount, String content, String cultureImageUrl, String
this.clubCount = clubCount;
this.recommendedMember = recommendedMember;
this.cultureCategory = cultureCategory;
this.region = region;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class CreateCultureRequest {
private String recommendedMember;

private Long cultureCategoryId;

private Long regionId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package likelion.MZConnent.dto.culture.response;

import likelion.MZConnent.domain.culture.Culture;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CulturesSimpleResponse {
private Long cultureId;
private String name;
private String summary;
private String cultureImageUrl;
private String cultureCategoryName;
private String regionName;
private int interestCount;
private int clubCount;

@Builder
public CulturesSimpleResponse(Culture culture) {
this.cultureId = culture.getCultureId();
this.name = culture.getName();
this.summary = culture.getSummary();
this.cultureImageUrl = culture.getCultureImageUrl();
this.cultureCategoryName = culture.getCultureCategory().getName();
this.regionName = culture.getRegion().getName();
this.interestCount = culture.getInterestCount();
this.clubCount = culture.getClubCount();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package likelion.MZConnent.repository.culture;

import io.lettuce.core.dynamic.annotation.Param;
import likelion.MZConnent.domain.culture.Culture;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface CultureRepository extends JpaRepository<Culture, Long> {
@Query("SELECT c FROM Culture c WHERE :category = 0 OR c.cultureCategory.id = :category")
Page<Culture> findAllByCultureCategory(@Param("category") Long category, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package likelion.MZConnent.service.culture;

import likelion.MZConnent.domain.culture.Culture;
import likelion.MZConnent.domain.review.Review;
import likelion.MZConnent.dto.culture.response.CulturesSimpleResponse;
import likelion.MZConnent.dto.paging.response.PageContentResponse;
import likelion.MZConnent.dto.review.response.ReviewsSimpleResponse;
import likelion.MZConnent.repository.culture.CultureRepository;
import likelion.MZConnent.repository.review.ReviewRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class CultureService {
private final CultureRepository cultureRepository;
private final int PAGE_SIZE = 6;

public PageContentResponse<CulturesSimpleResponse> getCulturesSimpleList(Long cultureCategoryId, int page) {
Pageable pageable = PageRequest.of(page, PAGE_SIZE);

Page<Culture> cultures = cultureRepository.findAllByCultureCategory(cultureCategoryId, pageable);

List<CulturesSimpleResponse> cultureResponse = cultures.stream().map(culture -> CulturesSimpleResponse.builder()
.culture(culture).build()
).collect(Collectors.toList());

return PageContentResponse.<CulturesSimpleResponse>builder()
.content(cultureResponse)
.totalPages(cultures.getTotalPages())
.totalElements(cultures.getTotalElements())
.size(pageable.getPageSize())
.build();

}
}

0 comments on commit d3ff62a

Please sign in to comment.