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

[feat] : 프로그램 전체 조회 페이지, 상세 페이지 조회 api #53

Merged
merged 12 commits into from
May 21, 2024
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package kusitms.jangkku.domain.keyword.domain;

import jakarta.persistence.*;
import kusitms.jangkku.domain.program.domain.ProgramsImageKeyword;
import kusitms.jangkku.global.common.dao.BaseEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand All @@ -17,4 +22,7 @@ public class Keyword {

@Column(name = "name", nullable = false, length = 20)
private String name;

@OneToMany(mappedBy = "keyword",cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<ProgramsImageKeyword> programsImageKeywords = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kusitms.jangkku.domain.program.api;

import kusitms.jangkku.domain.program.dto.ProgramDetailDto;
import kusitms.jangkku.domain.program.dto.ProgramDto;
import kusitms.jangkku.domain.program.application.ProgramServiceImpl;
import kusitms.jangkku.global.common.ApiResponse;
import kusitms.jangkku.global.common.code.BaseCode;
import kusitms.jangkku.global.common.constant.SuccessStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static kusitms.jangkku.domain.program.dto.ProgramDto.*;

@RestController
@RequestMapping("/api/programs")
@RequiredArgsConstructor
public class ProgramController {

private final ProgramServiceImpl programService;

//자기이해 조회 - 전체
@GetMapping("/main/self-understanding")
public ResponseEntity<ApiResponse<List<ProgrmsMainResponsetDto>>> findMainSelfUnderstanding() {
return ApiResponse.onSuccess(SuccessStatus._OK, programService.getMainSelfUnderstanding());
}

//브랜딩 조회 - 전체
@GetMapping("/main/branding")
public ResponseEntity<ApiResponse<List<ProgrmsMainResponsetDto>>> findMainBranding(@RequestHeader("Authorization") String authorizationHeader) {
return ApiResponse.onSuccess(SuccessStatus._OK, programService.getMainBranding(authorizationHeader));
}

//자기이해 조회 - 필터링
@PostMapping("/more/self-understanding")
private ResponseEntity<ApiResponse<List<ProgrmsMainResponsetDto>>> findMoreSelfUnderstanding(@RequestBody ProgramSelfUnderstandingRequestDto requestDto) {
return ApiResponse.onSuccess(SuccessStatus._OK, programService.getMoreSelfUnderstanding(requestDto));
}

//브랜딩 조회 - 필터링
@PostMapping("/more/branding")
private ResponseEntity<ApiResponse<List<ProgrmsMainResponsetDto>>> findMoreSelfUnderstanding(@RequestBody ProgramBrandingRequestDto requestDto) {
return ApiResponse.onSuccess(SuccessStatus._OK, programService.getMoreBranding(requestDto));
}

//프로그램 상세조회
@GetMapping("/{form}/{programId}")
private ResponseEntity<ApiResponse<ProgramDetailDto.ProgramDetailResponseDto>> findDetailProgram(@RequestHeader("Authorization") String authorizationHeader,
@PathVariable(name = "form") String form,@PathVariable(name = "program_id") Long programId) {
return ApiResponse.onSuccess(SuccessStatus._OK, programService.getDetailProgram(authorizationHeader, programId, form));
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kusitms.jangkku.domain.program.application;

import kusitms.jangkku.domain.program.dto.ProgramDetailDto;
import kusitms.jangkku.domain.program.dto.ProgramDto;

import java.util.List;

public interface ProgramService {
List<ProgramDto.ProgrmsMainResponsetDto> getMainSelfUnderstanding();

List<ProgramDto.ProgrmsMainResponsetDto> getMainBranding(String authorizationHeader);

List<ProgramDto.ProgrmsMainResponsetDto> getMoreSelfUnderstanding(ProgramDto.ProgramSelfUnderstandingRequestDto requestDto);

List<ProgramDto.ProgrmsMainResponsetDto> getMoreBranding(ProgramDto.ProgramBrandingRequestDto requestDto);

ProgramDetailDto.ProgramDetailResponseDto getDetailProgram(String authorizationHeader, Long programId, String form);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package kusitms.jangkku.domain.program.application;

import kusitms.jangkku.domain.program.dao.BrandingRepository;
import kusitms.jangkku.domain.program.dao.SelfUnderstandingsRepository;
import kusitms.jangkku.domain.program.domain.Branding;
import kusitms.jangkku.domain.program.domain.SelfUnderstanding;
import kusitms.jangkku.domain.program.dto.ProgramDetailDto;
import kusitms.jangkku.domain.program.dto.ProgramDto;
import kusitms.jangkku.domain.program.exception.ProgramException;
import kusitms.jangkku.domain.user.dao.UserInterestRepository;
import kusitms.jangkku.domain.user.dao.UserKeywordRepository;
import kusitms.jangkku.domain.user.dao.UserRepository;
import kusitms.jangkku.domain.user.domain.User;
import kusitms.jangkku.domain.user.exception.UserException;
import kusitms.jangkku.global.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import static kusitms.jangkku.domain.program.exception.ProgramErrorResult.NOT_FOUND_PROGRAM;
import static kusitms.jangkku.domain.user.exception.UserErrorResult.NOT_FOUND_USER;

@Service
@RequiredArgsConstructor
@Transactional
public class ProgramServiceImpl implements ProgramService {

private final JwtUtil jwtUtil;
private final SelfUnderstandingsRepository selfUnderstandingsRepository;
private final UserKeywordRepository userKeywordRepository;
private final BrandingRepository brandingRepository;
private final UserInterestRepository userInterestRepository;
private final UserRepository userRepository;

@Override
public List<ProgramDto.ProgrmsMainResponsetDto> getMainSelfUnderstanding() {
return selfUnderstandingsRepository.findTop9ByOrderByCreatedDateDesc()
.stream().map(ProgramDto.ProgrmsMainResponsetDto::of).collect(Collectors.toList());
}

@Override
public List<ProgramDto.ProgrmsMainResponsetDto> getMainBranding(String authorizationHeader) {
UUID userId = findUserIdFromauthorizationHeader(authorizationHeader);
List<String> keywords = findAllUserKeyword(findUserByUUID(userId)); //유저의 키워드
System.out.println("kewords : " + keywords);
List<String> interests = findAllUserInterest(findUserByUUID(userId));//유저의 관심분야
System.out.println("interests : " + interests);
//브랜딩 프로그램에서 유저의 관심분야와 키워드
return findAllBrandingByUsersKeywordsAndInterests(keywords, interests);
}

private UUID findUserIdFromauthorizationHeader(String authorizationHeader) {
String token = jwtUtil.getTokenFromHeader(authorizationHeader);
jwtUtil.getUserIdFromToken(token);
return UUID.fromString(jwtUtil.getUserIdFromToken(token));
}

@Override
public List<ProgramDto.ProgrmsMainResponsetDto> getMoreSelfUnderstanding(ProgramDto.ProgramSelfUnderstandingRequestDto requestDto) {
// if (requestDto.getForm())
return findAllSelfUnderstanding();
}

@Override
public List<ProgramDto.ProgrmsMainResponsetDto> getMoreBranding(ProgramDto.ProgramBrandingRequestDto requestDto) {

return null;
}

@Override
public ProgramDetailDto.ProgramDetailResponseDto getDetailProgram(String authorizationHeader, Long programId, String form) {
UUID userId = findUserIdFromauthorizationHeader(authorizationHeader);
User user = findUserByUUID(userId);
if (form.equals("self-understanding")) {
return ProgramDetailDto.ProgramDetailResponseDto.of(findSelfUnderStandingById(programId), findAllUserKeyword(user));
} else if (form.equals("branding")) {
return ProgramDetailDto.ProgramDetailResponseDto.of(findBrandingById(programId), findAllUserKeyword(user));
} else throw new ProgramException(NOT_FOUND_PROGRAM);
}


private SelfUnderstanding findSelfUnderStandingById(Long id) {
return selfUnderstandingsRepository.findById(id).orElseThrow(() -> new ProgramException(NOT_FOUND_PROGRAM));
}

private Branding findBrandingById(Long id) {
return brandingRepository.findById(id).orElseThrow(() -> new ProgramException(NOT_FOUND_PROGRAM));
}

private List<ProgramDto.ProgrmsMainResponsetDto> findAllSelfUnderstanding() {
return selfUnderstandingsRepository.findAllByOrderByCreatedDateDesc().stream().map(ProgramDto.ProgrmsMainResponsetDto::of).collect(Collectors.toList());
}

private List<ProgramDto.ProgrmsMainResponsetDto> findAllFormSelfUnderstanding() {
return null;
}

private List<String> findAllUserKeyword(User user) {
return userKeywordRepository.findAllByUser(user)
.stream().map(v -> v.getKeyword().getName()).toList();
}

private List<String> findAllUserInterest(User user) {
return userInterestRepository.findAllByUser(user)
.stream().map(v -> v.getInterest().getName()).toList();
}

private List<ProgramDto.ProgrmsMainResponsetDto> findAllBrandingByUsersKeywordsAndInterests(List<String> keywords, List<String> interests) {
return brandingRepository.findByProgramsImageKeywordsInOrProgramsInterestsIn(keywords, interests)
.stream().map(ProgramDto.ProgrmsMainResponsetDto::of).collect(Collectors.toList());
}

private User findUserByUUID(UUID userId) {
return userRepository.findByUserId(userId).orElseThrow(() -> new UserException(NOT_FOUND_USER));
}

}
27 changes: 27 additions & 0 deletions src/main/java/kusitms/jangkku/domain/program/constant/FORM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kusitms.jangkku.domain.program.constant;

import java.util.Arrays;

public enum FORM {
ONLINE("ONLINE","온라인"),
OFFLINE("OFFLINE","오프라인"),
ALL("ALL","온오프라인");

private final String code;
private final String name;


FORM(String code, String name){
this.code = code;
this.name=name;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kusitms.jangkku.domain.program.dao;

import io.lettuce.core.dynamic.annotation.Param;
import kusitms.jangkku.domain.program.domain.Branding;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface BrandingRepository extends JpaRepository<Branding, Long> {

@Query("SELECT DISTINCT b FROM Branding b " +
"JOIN b.programsImageKeywords pik " +
"JOIN b.programsInterests pi " +
"WHERE pik.keyword.name IN :keywords " +
"OR pi.interest.name IN :interests")
List<Branding> findByProgramsImageKeywordsInOrProgramsInterestsIn(@Param("keywords") List<String> keywords, @Param("interests") List<String> interests);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kusitms.jangkku.domain.program.dao;

import kusitms.jangkku.domain.program.domain.SelfUnderstanding;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public interface SelfUnderstandingsRepository extends JpaRepository<SelfUnderstanding,Long> {
List<SelfUnderstanding> findTop9ByOrderByCreatedDateDesc();

List<SelfUnderstanding> findAllByPrice(int price);

List<SelfUnderstanding> findAllByOrderByCreatedDateDesc();
}
42 changes: 42 additions & 0 deletions src/main/java/kusitms/jangkku/domain/program/domain/Branding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package kusitms.jangkku.domain.program.domain;

import jakarta.persistence.*;
import kusitms.jangkku.global.common.dao.BaseEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Table(name="branding_programs")
public class Branding extends BaseEntity{

@Column(name="name")
private String name;

@Column(name="one_line_description")
private String oneLineDescription;

@Column(name = "link")
private String link;

@Column(name="branding_url")
private String brandingUrl;

@Column(name = "description_url")
private String descriptionUrl;

@OneToMany(mappedBy = "branding",cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<ProgramsImageKeyword> programsImageKeywords = new ArrayList<>();

@OneToMany(mappedBy = "branding",cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<ProgramsInterest> programsInterests = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "program_provdiers_id", foreignKey = @ForeignKey(name = "branding_programs_program_provdiers_id"))
private ProgramProvider programProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package kusitms.jangkku.domain.program.domain;

import jakarta.persistence.*;
import kusitms.jangkku.global.common.dao.BaseEntity;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "programs_providers")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class ProgramProvider extends BaseEntity {

@Column(name = "name")
private String providerName;

@Column(name = "job")
private String providerJob;

@Column(name = "keyword")
private String providerKeyword;

@Column(name = "image")
private String providerImage;

@OneToMany(mappedBy = "programProvider",cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<Branding> brandings = new ArrayList<>();

@OneToMany(mappedBy = "programProvider",cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<SelfUnderstanding> selfUnderstandings = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kusitms.jangkku.domain.program.domain;

import jakarta.persistence.*;
import kusitms.jangkku.domain.keyword.domain.Keyword;
import kusitms.jangkku.global.common.dao.BaseEntity;

@Entity
@Table(name = "programs_image_keywords")
public class ProgramsImageKeyword extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "keywords_id", foreignKey = @ForeignKey(name = "programs_images_keywords_fk_keywords_id"))
private Keyword keyword;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "brandings_id", foreignKey = @ForeignKey(name = "programs_images_brandings_id"))
private Branding branding;
}
Loading
Loading