Skip to content

Commit

Permalink
Merge pull request #27 from survey-mate/feat/19
Browse files Browse the repository at this point in the history
feat: Survey 도메인 및 SurveyService 일부 구현
  • Loading branch information
JinhyeokFang authored Jan 20, 2024
2 parents 6b65d17 + 11dd010 commit b6b8986
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 34 deletions.
11 changes: 8 additions & 3 deletions src/main/java/uk/jinhy/survey_mate_api/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import uk.jinhy.survey_mate_api.answer.Answer;
import uk.jinhy.survey_mate_api.survey.domain.entity.Answer;
import uk.jinhy.survey_mate_api.data.Data;
import uk.jinhy.survey_mate_api.data_comment.DataComment;
import uk.jinhy.survey_mate_api.deviceToken.DeviceToken;
import uk.jinhy.survey_mate_api.notification.Notification;
import uk.jinhy.survey_mate_api.purchaseHistory.PurchaseHistory;
import uk.jinhy.survey_mate_api.statement.Statement;
import uk.jinhy.survey_mate_api.survey.Survey;
import uk.jinhy.survey_mate_api.surveyComment.SurveyComment;
import uk.jinhy.survey_mate_api.survey.domain.entity.Survey;
import uk.jinhy.survey_mate_api.survey.domain.entity.SurveyComment;

@Entity
@Getter
Expand Down Expand Up @@ -131,4 +133,7 @@ public void addSurveyComment(SurveyComment surveyComment){
surveyCommentList.add(surveyComment);
}

public boolean equals(Member member) {
return Objects.equals(member.getMemberId(), this.memberId);
}
}
15 changes: 0 additions & 15 deletions src/main/java/uk/jinhy/survey_mate_api/survey/SurveyStatus.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.jinhy.survey_mate_api.survey.application.dto;

import lombok.Builder;
import lombok.Getter;

public class SurveyServiceDTO {
@Builder
@Getter
public static class CreateSurveyDTO {
private String linkUrl;
private String title;
private String description;
private Long reward;
private Long period;
}

@Builder
@Getter
public static class EditSurveyDTO {
private Long surveyId;
private String linkUrl;
private String title;
private String description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package uk.jinhy.survey_mate_api.survey.application.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import uk.jinhy.survey_mate_api.common.util.Util;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.survey.application.dto.SurveyServiceDTO;
import uk.jinhy.survey_mate_api.survey.domain.entity.Answer;
import uk.jinhy.survey_mate_api.survey.domain.entity.Survey;
import uk.jinhy.survey_mate_api.survey.domain.repository.SurveyRepository;

import java.time.LocalDateTime;
import java.util.List;

// TODO
// Exception 구현 시 예외 처리 추가
@RequiredArgsConstructor
@Service
public class SurveyService {
private final SurveyRepository surveyRepository;

public Survey createSurvey(Member registrant, SurveyServiceDTO.CreateSurveyDTO dto) {
Survey survey = Survey.builder()
.reward(dto.getReward())
.endedAt(LocalDateTime.now().plusDays(dto.getPeriod()))
.linkUrl(dto.getLinkUrl())
.rewardUrl(Util.generateRandomString())
.title(dto.getTitle())
.description(dto.getDescription())
.registrant(registrant)
.build();
surveyRepository.save(survey);
return survey;
}

@Transactional
public void editSurvey(Member registrant, SurveyServiceDTO.EditSurveyDTO dto) {
Long surveyId = dto.getSurveyId();
Survey survey = surveyRepository.findBySurveyId(surveyId).get();

if (!survey.getRegistrant().equals(registrant)) {
return;
}

String newTitle = dto.getTitle();
if (newTitle != null) {
survey.updateTitle(newTitle);
}

String newDescription = dto.getDescription();
if (newDescription != null) {
survey.updateDescription(newDescription);
}

String newLinkUrl = dto.getLinkUrl();
if (newLinkUrl != null) {
survey.updateLinkUrl(newLinkUrl);
}
}

@Transactional
public void deleteSurvey(Member registrant, Long surveyId) {
Survey survey = surveyRepository.findBySurveyId(surveyId).get();
if (survey.getRegistrant().equals(registrant)) {
surveyRepository.deleteById(surveyId);
}
}

public void addAnswer(Member respondent, Long surveyId) {
Survey survey = surveyRepository.findBySurveyId(surveyId).get();
if (!survey.isAnswered(respondent)) {
return;
}
Answer answer = Answer.builder()
.survey(survey)
.respondent(respondent)
.build();
survey.addAnswer(answer);
surveyRepository.save(survey);
}

public Survey getSurvey(Long surveyId) {
return surveyRepository.findBySurveyId(surveyId).get();
}

public List<Survey> getSurveyList(int pageNumber) {
Pageable pageable = PageRequest.of(pageNumber, 10);
return surveyRepository.findByEndedAtIsBefore(pageable, LocalDateTime.now());
}

public List<Survey> getMySurveyList(Member registrant) {
return surveyRepository.findByRegistrant(registrant);
}

public List<Survey> getAnsweredSurveyList(Member respondent) {
return surveyRepository.findByRespondent(respondent);
}

public List<Survey> getRecentSurveyList() {
return surveyRepository.findRecentSurvey();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package uk.jinhy.survey_mate_api.survey.application.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.survey.application.dto.SurveyServiceDTO;

@RequiredArgsConstructor
@Service
public class SurveyServiceFacade {
private final SurveyService surveyService;

@Transactional
public void createSurvey(Member registrant, SurveyServiceDTO.CreateSurveyDTO dto) {
// TODO
// Point 지불하는 로직 필요
surveyService.createSurvey(registrant, dto);
}

@Transactional
public void answerSurvey(Member respondent, Long surveyId) {
// TODO
// Point 지급하는 로직 필요
surveyService.addAnswer(respondent, surveyId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.jinhy.survey_mate_api.answer;
package uk.jinhy.survey_mate_api.survey.domain.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -14,7 +14,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.survey.Survey;
import uk.jinhy.survey_mate_api.survey.domain.entity.Survey;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package uk.jinhy.survey_mate_api.survey;
package uk.jinhy.survey_mate_api.survey.domain.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -12,17 +10,20 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import uk.jinhy.survey_mate_api.answer.Answer;
import org.springframework.data.annotation.CreatedDate;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.surveyComment.SurveyComment;

@Entity
@Getter
Expand All @@ -34,25 +35,27 @@ public class Survey {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long surveyId;

@CreatedDate
private LocalDate createdAt;

@NotNull
private String linkUrl;
private LocalDateTime endedAt;

@NotNull
private Long goal;
private String linkUrl;

@NotNull
private Long reward;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member registrant;

@NotNull
private Long numberOfQuestions;

private LocalTime estimatedTime;
private String title;

@Enumerated(EnumType.STRING)
private SurveyStatus status;
@NotNull
private String description;

@NotNull
private String rewardUrl;
Expand All @@ -77,4 +80,26 @@ public void confirmRegistrant(Member registrant){
this.registrant = registrant;
registrant.addSurvey(this);
}

public boolean isSurveyEnded() {
LocalDateTime currentTime = LocalDateTime.now();
return currentTime.isBefore(this.endedAt);
}

public boolean isAnswered(Member member) {
return answerList.stream()
.anyMatch(a -> a.getRespondent().equals(member));
}

public void updateTitle(String newTitle) {
title = newTitle;
}

public void updateLinkUrl(String newLinkUrl) {
linkUrl = newLinkUrl;
}

public void updateDescription(String newDescription) {
description = newDescription;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.jinhy.survey_mate_api.surveyComment;
package uk.jinhy.survey_mate_api.survey.domain.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -19,7 +19,6 @@
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.survey.Survey;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.jinhy.survey_mate_api.survey.domain.repository;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.survey.domain.entity.Survey;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface SurveyRepository extends JpaRepository<Survey, Long> {
List<Survey> findByEndedAtIsBefore(Pageable pageable, LocalDateTime time);
Optional<Survey> findBySurveyId(Long id);
Optional<Survey> findByRewardUrl(String url);
@Query("select survey from Survey survey order by survey.createdAt limit 15")
List<Survey> findRecentSurvey();
List<Survey> findByRegistrant(Member member);
@Query("select survey from Survey survey join Answer answer on survey = answer.survey where answer.respondent = :member")
List<Survey> findByRespondent(Member member);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.jinhy.survey_mate_api.survey.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import uk.jinhy.survey_mate_api.survey.application.service.SurveyService;

@RequiredArgsConstructor
@RequestMapping("/survey")
@Controller
public class SurveyController {
private final SurveyService surveyService;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package uk.jinhy.survey_mate_api.survey.presentation.converter;

public class SurveyConverter {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package uk.jinhy.survey_mate_api.survey.presentation.dto;

public class SurveyControllerDTO {
}

0 comments on commit b6b8986

Please sign in to comment.