Skip to content

Commit

Permalink
Merge pull request #30 from survey-mate/feat/28
Browse files Browse the repository at this point in the history
feat: Data 도메인 및 DataService 일부 구현 #28
  • Loading branch information
JinhyeokFang authored Jan 20, 2024
2 parents b6b8986 + 641dacc commit 3932094
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.jinhy.survey_mate_api.data.application.dto;

import lombok.Builder;
import lombok.Getter;

public class DataServiceDTO {
@Builder
@Getter
public static class CreateDataDTO {
private String title;
private String description;
private Long price;
private String fileUrl;
}

@Builder
@Getter
public static class EditDataDTO {
private Long dataId;
private String title;
private String description;
private String fileUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package uk.jinhy.survey_mate_api.data.application.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import uk.jinhy.survey_mate_api.data.application.dto.DataServiceDTO;
import uk.jinhy.survey_mate_api.data.domain.entity.Data;
import uk.jinhy.survey_mate_api.data.domain.entity.PurchaseHistory;
import uk.jinhy.survey_mate_api.data.domain.repository.DataRepository;
import uk.jinhy.survey_mate_api.member.Member;

import java.util.List;

@RequiredArgsConstructor
@Service
public class DataService {
private final DataRepository dataRepository;

public Data createData(Member seller, DataServiceDTO.CreateDataDTO dto) {
Data data = Data.builder()
.seller(seller)
.fileUrl(dto.getFileUrl())
.title(dto.getTitle())
.description(dto.getDescription())
.price(dto.getPrice())
.seller(seller)
.build();
dataRepository.save(data);
return data;
}

@Transactional
public void editData(Member seller, DataServiceDTO.EditDataDTO dto) {
Long dataId = dto.getDataId();

Data data = dataRepository.findByDataId(dataId).get();
if(!data.getSeller().equals(seller)) { return; }

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

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

String newFileUrl = dto.getFileUrl();
if(newFileUrl != null) { data.updateFileUrl(newFileUrl); }

dataRepository.save(data);
}

@Transactional
public void deleteData(Member seller, Long dataId) {
Data data = dataRepository.findByDataId(dataId).get();
if(data.getSeller().equals(seller)) {
dataRepository.deleteById(dataId);
}
}

@Transactional
public void buyData(Member buyer, Long dataId) {
Data data = dataRepository.findByDataId(dataId).get();

PurchaseHistory purchaseHistory = PurchaseHistory.builder()
.data(data)
.buyer(buyer)
.build();
data.addPurchaseHistory(purchaseHistory);
dataRepository.save(data);
}

public Data getData(Long dataId) { return dataRepository.findByDataId(dataId).get(); }

public List<Data> getDataListAsBuyer(Member buyer) { return dataRepository.findByBuyer(buyer); }

public List<Data> getDataListAsSeller(Member seller) { return dataRepository.findByBuyer(seller); }

public List<Data> getRecentDataList() { return dataRepository.findRecentData(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.jinhy.survey_mate_api.data.application.service;

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

@RequiredArgsConstructor
@Service
public class DataServiceFacade {
private final DataService dataService;

@Transactional
public void buyData(Member buyer, Long dataId) {
// TODO
// Point 결제 로직 필요
dataService.buyData(buyer, dataId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.jinhy.survey_mate_api.data;
package uk.jinhy.survey_mate_api.data.domain.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
Expand All @@ -10,16 +10,18 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import uk.jinhy.survey_mate_api.data_comment.DataComment;
import uk.jinhy.survey_mate_api.member.Member;
import uk.jinhy.survey_mate_api.purchaseHistory.PurchaseHistory;

@Entity
@Getter
Expand All @@ -31,6 +33,9 @@ public class Data {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long dataId;

@CreatedDate
private LocalDate createdAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
@NotNull
Expand All @@ -39,8 +44,13 @@ public class Data {
@NotNull
private String fileUrl;

@NotNull
private String title;

@NotNull
private String description;

@NotNull
private Long price;

@Builder.Default
Expand All @@ -64,4 +74,7 @@ public void confirmSeller(Member seller){
seller.addData(this);
}

public void updateTitle(String newTitle) { title = newTitle; }
public void updateDescription(String newDescription) { title = newDescription; }
public void updateFileUrl(String newFileUrl) { title = newFileUrl; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.jinhy.survey_mate_api.purchaseHistory;
package uk.jinhy.survey_mate_api.data.domain.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -13,7 +13,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import uk.jinhy.survey_mate_api.data.Data;
import uk.jinhy.survey_mate_api.data.domain.entity.Data;
import uk.jinhy.survey_mate_api.member.Member;

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

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

import java.util.List;
import java.util.Optional;

public interface DataRepository extends JpaRepository<Data, Long> {
Optional<Data> findByDataId(Long id);
List<Data> findBySeller(Member member);
@Query("select data from Data data join PurchaseHistory purchase_history on data = purchase_history.data where purchase_history.buyer = :member")
List<Data> findByBuyer(Member member);
@Query("select data from Data data order by data.createdAt limit 15")
List<Data> findRecentData();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.jinhy.survey_mate_api.data.presentation;

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

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

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

public class DataControllerDTO {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import uk.jinhy.survey_mate_api.data.Data;
import uk.jinhy.survey_mate_api.data.domain.entity.Data;
import uk.jinhy.survey_mate_api.member.Member;

@Entity
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/uk/jinhy/survey_mate_api/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
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.domain.entity.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.data.domain.entity.PurchaseHistory;
import uk.jinhy.survey_mate_api.statement.Statement;
import uk.jinhy.survey_mate_api.survey.domain.entity.Survey;
import uk.jinhy.survey_mate_api.survey.domain.entity.SurveyComment;
Expand Down

0 comments on commit 3932094

Please sign in to comment.