Skip to content

Commit

Permalink
Merge pull request #140 from 4m9d/feat/#139/develop-material-user-fee…
Browse files Browse the repository at this point in the history
…dback(swm-392)

Feat/#139/develop material user feedback(swm 392)
  • Loading branch information
D-w-nJ authored Nov 22, 2023
2 parents 98e19ca + a88a705 commit db578f3
Show file tree
Hide file tree
Showing 43 changed files with 654 additions and 75 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

## gradle build
- name: Build with Gradle
run: ./gradlew clean build -x test
run: ./gradlew clean build -x test -Dspring.profiles.active=prod

## AWS에 로그인
- name: Configure AWS credentials
Expand All @@ -45,7 +45,7 @@ jobs:
ECR_REPOSITORY: be-repo
IMAGE_TAG: release
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker build --build-arg SPRING_PROFILE=prod -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "IMAGE=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_ENV
## ECS task 정의
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/com/m9d/sroom/ai/AiService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.m9d.sroom.ai;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.m9d.sroom.ai.vo.MaterialResultsVo;
import com.google.gson.*;
import com.m9d.sroom.ai.vo.MaterialResultVo;
import com.m9d.sroom.ai.vo.MaterialVo;
import com.m9d.sroom.material.MaterialSaver;
import lombok.RequiredArgsConstructor;
Expand All @@ -15,6 +14,8 @@
import org.springframework.web.util.UriComponentsBuilder;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
Expand Down Expand Up @@ -50,9 +51,7 @@ public void saveResultFromFastApi() {
log.debug("response body from gpt server = {}", resultStr);
}

MaterialVo resultVo = getMaterialVo(resultStr);

for (MaterialResultsVo materialVo : resultVo.getResults()) {
for (MaterialResultVo materialVo : getMaterialResults(resultStr)) {
saveResultEach(materialVo);
}
}
Expand Down Expand Up @@ -89,7 +88,28 @@ private MaterialVo getMaterialVo(String resultStr) throws NullPointerException {
return resultVo;
}

public void saveResultEach(MaterialResultsVo materialVo) {
private List<MaterialResultVo> getMaterialResults(String resultStr) {
List<MaterialResultVo> resultsVoList = new ArrayList<>();

try {
JsonObject jsonObject = JsonParser.parseString(resultStr).getAsJsonObject();
JsonArray resultsArray = jsonObject.getAsJsonArray("results");

for (JsonElement resultElement : resultsArray) {
try {
log.debug("fastApi response divided into each materialVo");
resultsVoList.add(gson.fromJson(resultElement, MaterialResultVo.class));
} catch (JsonSyntaxException e) {
log.error("Failed to parse JSON to MaterialResultVo: {}", resultElement.toString(), e);
}
}
} catch (Exception e) {
log.error("Failed to parse JSON to MaterialResultVo. Input JSON: {}", resultStr, e);
}
return resultsVoList;
}

public void saveResultEach(MaterialResultVo materialVo) {
try {
materialSaver.saveMaterials(materialVo);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public class QuizTypeNotMatchException extends NotMatchException {
public QuizTypeNotMatchException(int type) {
super(MESSAGE + type);
}

public QuizTypeNotMatchException(String message) {
super(MESSAGE + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Setter
@RequiredArgsConstructor
@Getter
public class MaterialResultsVo {
public class MaterialResultVo {

private String video_id;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/m9d/sroom/ai/vo/MaterialVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@Data
public class MaterialVo {

private List<MaterialResultsVo> results;
private List<MaterialResultVo> results;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.m9d.sroom.common.entity;

import com.m9d.sroom.material.model.MaterialType;
import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.jdbc.core.RowMapper;

@Data
@Builder
public class MaterialFeedbackEntity {

@Id
private Long feedbackId;

private Long memberId;

private Long contentId;

private int contentType;

private boolean satisfactory;

public static RowMapper<MaterialFeedbackEntity> getRowMapper() {
return (rs, rowNum) -> MaterialFeedbackEntity.builder()
.feedbackId(rs.getLong("feedback_id"))
.memberId(rs.getLong("member_id"))
.contentId(rs.getLong("content_id"))
.contentType(rs.getInt("content_type"))
.satisfactory(rs.getBoolean("rating"))
.build();
}

public MaterialType getContentType() {
return MaterialType.from(contentType);
}

public static MaterialFeedbackEntity createForSave(Long memberId, MaterialType type, Long contentId,
boolean satisfactory) {
return MaterialFeedbackEntity.builder()
.memberId(memberId)
.contentType(type.getValue())
.contentId(contentId)
.satisfactory(satisfactory)
.build();
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/m9d/sroom/common/entity/QuizEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class QuizEntity {

private Integer choiceAnswer;

private Integer positiveFeedbackCount;

private Integer negativeFeedbackCount;

public static RowMapper<QuizEntity> getRowMapper() {
return (rs, rowNum) -> QuizEntity.builder()
.id(rs.getLong("quiz_id"))
Expand All @@ -29,6 +33,8 @@ public static RowMapper<QuizEntity> getRowMapper() {
.question(rs.getString("question"))
.subjectiveAnswer(rs.getString("subjective_answer"))
.choiceAnswer(rs.getInt("choice_answer"))
.positiveFeedbackCount(rs.getInt("positive_feedback_count"))
.negativeFeedbackCount(rs.getInt("negative_feedback_count"))
.build();
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/m9d/sroom/common/entity/SummaryEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ public class SummaryEntity {

private boolean modified;

private Integer positiveFeedbackCount;

private Integer negativeFeedbackCount;

public static RowMapper<SummaryEntity> getRowMapper() {
return (rs, rowNum) -> SummaryEntity.builder()
.id(rs.getLong("summary_id"))
.videoId(rs.getLong("video_id"))
.content(rs.getString("content"))
.updatedAt(rs.getTimestamp("updated_time"))
.modified(rs.getBoolean("is_modified"))
.positiveFeedbackCount(rs.getInt("positive_feedback_count"))
.negativeFeedbackCount(rs.getInt("negative_feedback_count"))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Getter
public abstract class DuplicationException extends RuntimeException {

private final int statusCode = HttpStatus.NOT_FOUND.value();
private final int statusCode = HttpStatus.BAD_REQUEST.value();
private String message;

public DuplicationException(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
public abstract class NotMatchException extends RuntimeException {

private final int statusCode = HttpStatus.NOT_FOUND.value();
private final int statusCode = HttpStatus.BAD_REQUEST.value();
private String message;

public NotMatchException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.m9d.sroom.common.repository.materialfeedback;

import com.m9d.sroom.common.entity.MaterialFeedbackEntity;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class MaterialFeedbackJdbcRepositoryImpl implements MaterialFeedbackRepository {

private final JdbcTemplate jdbcTemplate;

public MaterialFeedbackJdbcRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public MaterialFeedbackEntity save(MaterialFeedbackEntity feedbackEntity) {
jdbcTemplate.update(MaterialFeedbackRepositorySql.SAVE,
feedbackEntity.getMemberId(),
feedbackEntity.getContentId(),
feedbackEntity.getContentType().getValue(),
feedbackEntity.isSatisfactory());
return getById(jdbcTemplate.queryForObject(MaterialFeedbackRepositorySql.GET_LAST_ID, Long.class));
}

@Override
public MaterialFeedbackEntity getById(Long id) {
return jdbcTemplate.queryForObject(MaterialFeedbackRepositorySql.GET_BY_ID,
MaterialFeedbackEntity.getRowMapper(), id);
}

@Override
public Optional<MaterialFeedbackEntity> findByMemberIdAndTypeAndMaterialId(Long memberId, int materialType,
Long materialId) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject(
MaterialFeedbackRepositorySql.GET_BY_MEMBER_ID_AND_TYPE_AND_MATERIAL_ID,
MaterialFeedbackEntity.getRowMapper(), memberId, materialType, materialId));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.m9d.sroom.common.repository.materialfeedback;

import com.m9d.sroom.common.entity.MaterialFeedbackEntity;

import java.util.Optional;

public interface MaterialFeedbackRepository {

MaterialFeedbackEntity save(MaterialFeedbackEntity feedbackEntity);

MaterialFeedbackEntity getById(Long id);

Optional<MaterialFeedbackEntity> findByMemberIdAndTypeAndMaterialId(Long memberId, int materialType,
Long materialId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.m9d.sroom.common.repository.materialfeedback

class MaterialFeedbackRepositorySql {

public static final String GET_LAST_ID = """
SELECT LAST_INSERT_ID()
"""

public static final String GET_BY_ID = """
SELECT
feedback_id, member_id, content_id, content_type, rating
FROM MATERIAL_FEEDBACK
WHERE feedback_id = ?
"""

public static final String SAVE = """
INSERT
INTO MATERIAL_FEEDBACK (member_id, content_id, content_type, rating)
VALUES (?, ?, ?, ?)
"""

public static final String GET_BY_MEMBER_ID = """
SELECT
feedback_id, member_id, content_id, content_type, rating
FROM MATERIAL_FEEDBACK
WHERE member_id = ?
"""

public static final String GET_BY_MEMBER_ID_AND_TYPE_AND_MATERIAL_ID = """
SELECT
feedback_id, member_id, content_id, content_type, rating
FROM MATERIAL_FEEDBACK
WHERE member_id = ?
AND content_type = ?
AND content_id = ?
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ public QuizEntity getById(Long quizId) {
@Override
public Optional<QuizEntity> findById(Long quizId) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject(QuizRepositorySql.GET_BY_ID, QuizEntity.getRowMapper(), quizId));
return Optional.ofNullable(jdbcTemplate.queryForObject(QuizRepositorySql.GET_BY_ID,
QuizEntity.getRowMapper(), quizId));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}

@Override
public void feedbackPositive(Long quizId) {
jdbcTemplate.update(QuizRepositorySql.UPDATE_POSITIVE_FEEDBACK_COUNT, quizId);
}

@Override
public void feedbackNegative(Long quizId) {
jdbcTemplate.update(QuizRepositorySql.UPDATE_NEGATIVE_FEEDBACK_COUNT, quizId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface QuizRepository {
QuizEntity getById(Long quizId);

Optional<QuizEntity> findById(Long quizId);

void feedbackPositive(Long quizId);

void feedbackNegative(Long quizId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ class QuizRepositorySql {

public static final String GET_LIST_BY_VIDEO_ID = """
SELECT
quiz_id, video_id, type, question, subjective_answer, choice_answer
quiz_id, video_id, type, question, subjective_answer, choice_answer, positive_feedback_count,
negative_feedback_count
FROM QUIZ
WHERE video_id = ?
"""

public static final String GET_BY_ID = """
SELECT
quiz_id, video_id, type, question, subjective_answer, choice_answer
quiz_id, video_id, type, question, subjective_answer, choice_answer, positive_feedback_count,
negative_feedback_count
FROM QUIZ
WHERE quiz_id = ?
"""

public static final String UPDATE_POSITIVE_FEEDBACK_COUNT = """
UPDATE QUIZ
SET positive_feedback_count = positive_feedback_count + 1
WHERE quiz_id = ?
"""

public static final String UPDATE_NEGATIVE_FEEDBACK_COUNT = """
UPDATE QUIZ
SET negative_feedback_count = negative_feedback_count + 1
WHERE quiz_id = ?
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ public SummaryEntity updateById(Long summaryId, SummaryEntity summary) {
summaryId);
return getById(summaryId);
}

@Override
public void feedbackPositive(Long summaryId) {
jdbcTemplate.update(SummaryRepositorySql.UPDATE_POSITIVE_FEEDBACK_COUNT, summaryId);
}

@Override
public void feedbackNegative(Long summaryId) {
jdbcTemplate.update(SummaryRepositorySql.UPDATE_NEGATIVE_FEEDBACK_COUNT, summaryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public interface SummaryRepository {
Optional<SummaryEntity> findById(Long summaryId);

SummaryEntity updateById(Long summaryId, SummaryEntity summary);

void feedbackPositive(Long summaryId);

void feedbackNegative(Long summaryId);
}
Loading

0 comments on commit db578f3

Please sign in to comment.