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 #26

Merged
merged 1 commit into from
Mar 6, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.backend.soullive_a.controller;

import com.backend.soullive_a.dto.response.model.popularity.ModelPopularityResponse;
import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.service.ModelPopularityService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/model/popularity")
public class ModelPopularityController {

private final ModelPopularityService modelPopularityService;

@GetMapping("")
public BaseResponse<ModelPopularityResponse> getModelPopularity(
@RequestParam String name
) {
return BaseResponse.<ModelPopularityResponse>builder()
.isSuccess(true)
.code(200)
.message(name+" 모델 화제성 조회에 성공했습니다.")
.data(modelPopularityService.getModelPopularity(name))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.dto.response.model.popularity;

import com.backend.soullive_a.constant.AgeType;

public record ModelPopularAgeResponse(
Long ageId,
AgeType ageType
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.soullive_a.dto.response.model.popularity;

import com.backend.soullive_a.constant.GenderType;

public record ModelPopularGenderResponse(
Long genderId,
GenderType gender
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.backend.soullive_a.dto.response.model.popularity;

import com.backend.soullive_a.entity.model.popularity.ModelPopularAge;
import com.backend.soullive_a.entity.model.popularity.ModelPopularGender;
import com.backend.soullive_a.entity.model.popularity.ModelPopularity;
import com.backend.soullive_a.entity.model.popularity.ModelScheduledWork;
import lombok.Builder;

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

@Builder
public record ModelPopularityResponse(
Long modelPopularityId,
Long modelId,
String scoreUrl,
String aiComment,
List<ModelPopularGenderResponse> genders,
List<ModelPopularAgeResponse> ages,
String snsUrl,
String searchUrl,
String brandScoreUrl,
List<ModelScheduledWorkResponse> modelScheduledWorks
) {
public static ModelPopularityResponse fromModel(
ModelPopularity modelPopularity,
List<ModelPopularGender> modelPopularGender,
List<ModelPopularAge> modelPopularAge,
List<ModelScheduledWork> modelScheduledWork
) {
List<ModelPopularGenderResponse> modelPopularGenders = modelPopularGender.stream()
.map(gender -> new ModelPopularGenderResponse(gender.getId(), gender.getGender()))
.collect(Collectors.toList());

List<ModelPopularAgeResponse> modelPopularAges = modelPopularAge.stream()
.map(age -> new ModelPopularAgeResponse(age.getId(), age.getAge()))
.collect(Collectors.toList());

List<ModelScheduledWorkResponse> modelScheduledWorks = modelScheduledWork.stream()
.map(work -> new ModelScheduledWorkResponse(
work.getId(),
work.getImageUrl(),
work.getYear(),
work.getCategory(),
work.getTitle(),
work.getIsMainActor(),
work.getGenre()))
.collect(Collectors.toList());

return ModelPopularityResponse.builder()
.modelId(modelPopularity.getModel().getId())
.modelPopularityId(modelPopularity.getId())
.scoreUrl(modelPopularity.getScoreUrl())
.aiComment(modelPopularity.getAiComment())
.genders(modelPopularGenders)
.ages(modelPopularAges)
.snsUrl(modelPopularity.getSnsUrl())
.searchUrl(modelPopularity.getSearchUrl())
.brandScoreUrl(modelPopularity.getBrandScoreUrl())
.modelScheduledWorks(modelScheduledWorks)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.dto.response.model.popularity;

public record ModelScheduledWorkResponse(
Long modelScheduledWorkId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 dto에서 id를 전부 다 뺐는데, 있는게 좋을까요?

Copy link
Member Author

@sominyun sominyun Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세개 리뷰가 다 같은 내용이라 한번에 쓰자면 프론트에서는 사실 쓰는 값은 아니지만, 값을 잘 받아온건지 확인하는 용도로 쓰는 거라 편한대로 하는게 좋을것같아요. 저는 모든 dto에 id를 넣는편입니다..!

Copy link
Member Author

@sominyun sominyun Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니까 바꾸는게 나을것같기도 하네요... 바꿔볼게요!

String imageUrl,
Integer year,
String category,
String title,
Boolean isMainActor,
String genre
) {
}
4 changes: 3 additions & 1 deletion src/main/java/com/backend/soullive_a/entity/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@Getter
Expand All @@ -23,6 +22,9 @@ public class Model {
@Column(name = "BIRTH", nullable = false)
private LocalDate birth;

@Column(name = "AGE", nullable = false)
private String age;

@Column(name = "JOB", nullable = false)
private String job;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.backend.soullive_a.entity.model.popularity;

import com.backend.soullive_a.constant.AgeType;
import com.backend.soullive_a.entity.Product;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "MODEL_POPULAR_AGE")
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ModelPopularAge {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "AGE_ID")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MODEL_POPULARITY_ID", nullable = false)
private ModelPopularity modelPopularity;

@Enumerated(EnumType.STRING)
@Column(name = "AGE_TYPE")
private AgeType age;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backend.soullive_a.entity.model.popularity;

import com.backend.soullive_a.constant.GenderType;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "MODEL_POPULAR_GENDER")
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ModelPopularGender {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "GENDER_ID")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MODEL_POPULARITY_ID", nullable = false)
private ModelPopularity modelPopularity;

@Enumerated(EnumType.STRING)
@Column(name = "GENDER_TYPE")
private GenderType gender;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.backend.soullive_a.entity.model.popularity;

import com.backend.soullive_a.entity.model.Model;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "MODEL_POPULARITY")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ModelPopularity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MODEL_POPULARITY_ID", nullable = false)
private Long id;

@OneToOne
@JoinColumn(name = "MODEL_ID", nullable = false)
private Model model;

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

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

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

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

@Column(name = "BRAND_SCORE_URL")
private String brandScoreUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.backend.soullive_a.entity.model.popularity;

import com.backend.soullive_a.entity.model.Model;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "MODEL_SCHEDULED_WORK")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ModelScheduledWork {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MODEL_SCHEDULED_WORK_ID", nullable = false)
private Long id;

@JoinColumn(name = "IMAGE_URL")
private String imageUrl;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MODEL_POPULARITY_ID", nullable = false)
private ModelPopularity modelPopularity;

@Column(name = "YEAR", nullable = false)
private Integer year;

@Column(name = "CATEGORY", nullable = false)
private String category;

@Column(name = "TITLE", nullable = false)
private String title;

@Column(name = "IS_MAIN_ACTOR", nullable = false)
private Boolean isMainActor;

@Column(name = "GENRE", nullable = false)
private String genre;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository.model.popularity;

import com.backend.soullive_a.entity.model.popularity.ModelPopularAge;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ModelPopularAgeRepository extends JpaRepository<ModelPopularAge, Long> {

List<ModelPopularAge> findAllByModelPopularityModelModelName(String name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModelPopularityModelModelName 이게 상당히 어렵네.
ModelPopularity 필드의 model필드의 modelname를 가져오는거야?
그렇다면 엔티티끼리 join하는건 jpa에서 해주는건가?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞아요 jpa가 알아서 찾아주는거


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository.model.popularity;

import com.backend.soullive_a.entity.model.popularity.ModelPopularGender;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ModelPopularGenderRepository extends JpaRepository<ModelPopularGender, Long> {

List<ModelPopularGender> findAllByModelPopularityModelModelName(String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.backend.soullive_a.repository.model.popularity;

import com.backend.soullive_a.entity.model.popularity.ModelPopularity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ModelPopularityRepository extends JpaRepository<ModelPopularity, Long> {

ModelPopularity findByModelModelName(String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.soullive_a.repository.model.popularity;

import com.backend.soullive_a.entity.model.popularity.ModelScheduledWork;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ModelScheduledWorkRepository extends JpaRepository<ModelScheduledWork, Long> {

List<ModelScheduledWork> findAllByModelPopularityModelModelName(String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.backend.soullive_a.service;

import com.backend.soullive_a.dto.response.model.popularity.ModelPopularityResponse;

public interface ModelPopularityService {

ModelPopularityResponse getModelPopularity(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.backend.soullive_a.service.impl;

import com.backend.soullive_a.dto.response.model.popularity.ModelPopularityResponse;
import com.backend.soullive_a.entity.model.popularity.ModelPopularAge;
import com.backend.soullive_a.entity.model.popularity.ModelPopularGender;
import com.backend.soullive_a.entity.model.popularity.ModelPopularity;
import com.backend.soullive_a.entity.model.popularity.ModelScheduledWork;
import com.backend.soullive_a.repository.model.popularity.ModelPopularAgeRepository;
import com.backend.soullive_a.repository.model.popularity.ModelPopularGenderRepository;
import com.backend.soullive_a.repository.model.popularity.ModelPopularityRepository;
import com.backend.soullive_a.repository.model.popularity.ModelScheduledWorkRepository;
import com.backend.soullive_a.service.ModelPopularityService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ModelPopularityServiceImpl implements ModelPopularityService {

private final ModelPopularityRepository modelPopularityRepository;
private final ModelPopularAgeRepository modelPopularAgeRepository;
private final ModelPopularGenderRepository modelPopularGenderRepository;
private final ModelScheduledWorkRepository modelScheduledWorkRepository;

@Override
@Transactional(readOnly = true)
public ModelPopularityResponse getModelPopularity(String name) {
ModelPopularity modelPopularity = modelPopularityRepository.findByModelModelName(name);
List<ModelPopularGender> modelPopularGenders = modelPopularGenderRepository.findAllByModelPopularityModelModelName(name);
List<ModelPopularAge> modelPopularAges = modelPopularAgeRepository.findAllByModelPopularityModelModelName(name);
List<ModelScheduledWork> modelScheduledWorks = modelScheduledWorkRepository.findAllByModelPopularityModelModelName(name);
return ModelPopularityResponse.fromModel(modelPopularity, modelPopularGenders, modelPopularAges, modelScheduledWorks);
}
}
12 changes: 11 additions & 1 deletion src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
INSERT INTO soullive.user (user_id, password, phone_number) VALUES (1, 'password', '010-0000-0000');
-- git 에는 soullive로 해야함
INSERT INTO soullive.user (user_id, password, phone_number) VALUES (1, 'password', '010-0000-0000');
INSERT INTO soullive.model (model_id, model_name, birth, age, job, info, agency, ai_rate) VALUES (1,'김희애', '1967-04-23','56세','텔런트/영화배우','','YG 엔터테인먼트',4.0);
INSERT INTO soullive.model_popularity (model_popularity_id,model_id,score_url,ai_comment,sns_url,search_url,brand_score_url) values (1,1,null,'최근 화제성에서는 다소 약한 모습을 보여주고 있지만 3040 남녀 모두에게 높은 인지도를 갖고 있고 앞으로 2개의 주연 활동을 앞두고 있어 더 큰 화제성을 갖을 것으로 예상됩니다.',null,null,null);
INSERT INTO soullive.model_popular_gender (gender_id, model_popularity_id, gender_type) values ('1','1','MALE');
INSERT INTO soullive.model_popular_gender (gender_id, model_popularity_id, gender_type) values ('2','1','FEMALE');
INSERT INTO soullive.model_popular_age (age_id, model_popularity_id,age_type) values (1,1,'THIRTY');
INSERT INTO soullive.model_popular_age (age_id, model_popularity_id,age_type) values (2,1,'FORTY');
Copy link
Contributor

@j2noo j2noo Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같은 테이블에 삽입할 때는

insert into tbl () 
values (1,name)
       (2,name)

이런식으로 줄이면 좋을 것 같아!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

근데 만약에 이렇게 하면 예를 들어 우리 모델을 15명 넣으면 줄이 오른쪽으로 계속 늘어나는 건가?

INSERT INTO soullive.model_popular_age (age_id, model_popularity_id,age_type) values (3,1,'FIFTY');
INSERT INTO soullive.model_scheduled_work (model_scheduled_work_id,image_url,model_popularity_id,year,category,title,is_main_actor,genre) values (1,null, 1, 2024, '방영 예정 영화','돌풍',true,'공포');
INSERT INTO soullive.model_scheduled_work (model_scheduled_work_id,image_url,model_popularity_id,year,category,title,is_main_actor,genre) values (2,null, 1, 2024, '방영 예정 영화','보통의 가족',true,'공포');
Loading