Skip to content

Commit

Permalink
Refactor: 문서 단독 조회시 기여자 정보 전달
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhboy committed Mar 11, 2024
1 parent 6c2713a commit ae2bf26
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 169 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.project.bumawiki.domain.contribute.domain;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.user.domain.User;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -19,30 +11,42 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.user.domain.User;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@CreatedDate
private LocalDateTime createdAt;
@CreatedDate
private LocalDateTime createdAt;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User contributor;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User contributor;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "docs_id")
private Docs docs;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "docs_id")
private Docs docs;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "version_docs_id")
private VersionDocs versionDocs;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "version_docs_id")
private VersionDocs versionDocs;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.project.bumawiki.domain.contribute.domain.repository;

import java.util.List;

import com.project.bumawiki.domain.contribute.domain.Contribute;
import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.user.domain.User;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ContributeRepository extends JpaRepository<Contribute, Long> {
@Query("select distinct c.contributor from Contribute c where c.docs = :docs")
List<User> findUserAllByDocs(Docs docs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public class DocsCreateUpdateController {
private final DocsCreateService docsCreateService;

@PostMapping("/create")
public ResponseEntity<DocsResponseDto> createDocs(@RequestBody DocsCreateRequestDto request) throws IOException {
public ResponseEntity<Long> createDocs(@RequestBody DocsCreateRequestDto request) throws IOException {
return ResponseEntity.ok(docsCreateService.execute(request));
}

@PutMapping("/update/{title}")
public DocsResponseDto updateDocs(@RequestHeader("Authorization") String bearer, @PathVariable String title, @RequestBody DocsUpdateRequestDto request) throws IOException {
return ResponseEntity.ok(docsUpdateService.execute(bearer, title, request)).getBody();
public ResponseEntity<Long> updateDocs(@RequestHeader("Authorization") String bearer, @PathVariable String title, @RequestBody DocsUpdateRequestDto request) throws IOException {
return ResponseEntity.ok(docsUpdateService.execute(bearer, title, request));
}

@PutMapping("/update/title/{title}")
public ResponseEntity<DocsResponseDto> updateDocsTitle(@RequestBody DocsTitleUpdateRequestDto requestDto, @PathVariable String title) {
public ResponseEntity<Long> updateDocsTitle(@RequestBody DocsTitleUpdateRequestDto requestDto, @PathVariable String title) {
return ResponseEntity.ok(docsUpdateService.titleUpdate(title, requestDto));
}

@PutMapping("/update/docsType")
public ResponseEntity<DocsResponseDto> updateDocsType(@RequestBody DocsTypeUpdateDto requestDto) {
public ResponseEntity<Long> updateDocsType(@RequestBody DocsTypeUpdateDto requestDto) {
return ResponseEntity.ok(docsUpdateService.DocsTypeUpdate(requestDto));
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
package com.project.bumawiki.domain.docs.presentation.dto.response;

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

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.docs.domain.type.DocsType;
import lombok.Getter;
import com.project.bumawiki.domain.user.domain.User;
import com.project.bumawiki.domain.user.presentation.dto.SimpleUserDto;

import java.time.LocalDateTime;
import lombok.Getter;

@Getter
public class DocsResponseDto {

private final Long id;
private final String title;
private final String contents;
private final DocsType docsType;
private final LocalDateTime lastModifiedAt;
private final int enroll;

public DocsResponseDto(Docs docs) {
int lastValueOfDocsVersion = docs.getDocsVersion().size() - 1;
VersionDocs versionDocs = docs.getDocsVersion().get(lastValueOfDocsVersion);

this.id = docs.getId();
this.title = docs.getTitle();
this.contents = versionDocs.getContents();
this.lastModifiedAt = docs.getLastModifiedAt();
this.docsType = docs.getDocsType();
this.enroll = docs.getEnroll();
}
private final Long id;
private final String title;
private final String contents;
private final DocsType docsType;
private final LocalDateTime lastModifiedAt;
private final int enroll;
private final List<SimpleUserDto> contributors;

public DocsResponseDto(Docs docs, List<User> contributors) {
int lastValueOfDocsVersion = docs.getDocsVersion().size() - 1;
VersionDocs versionDocs = docs.getDocsVersion().get(lastValueOfDocsVersion);

this.id = docs.getId();
this.title = docs.getTitle();
this.contents = versionDocs.getContents();
this.lastModifiedAt = docs.getLastModifiedAt();
this.docsType = docs.getDocsType();
this.enroll = docs.getEnroll();
this.contributors = contributors.stream()
.map(SimpleUserDto::new)
.collect(Collectors.toList());
}
}

Original file line number Diff line number Diff line change
@@ -1,85 +1,89 @@
package com.project.bumawiki.domain.docs.service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.project.bumawiki.domain.contribute.domain.Contribute;
import com.project.bumawiki.domain.contribute.service.ContributeService;
import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.docs.domain.repository.VersionDocsRepository;
import com.project.bumawiki.domain.docs.domain.repository.DocsRepositoryMapper;
import com.project.bumawiki.domain.docs.domain.repository.VersionDocsRepository;
import com.project.bumawiki.domain.docs.presentation.dto.request.DocsCreateRequestDto;
import com.project.bumawiki.domain.docs.presentation.dto.response.DocsResponseDto;
import com.project.bumawiki.domain.image.service.ImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class DocsCreateService {
private final VersionDocsRepository versionDocsRepository;
private final ImageService imageService;
private final ContributeService contributeService;
private final DocsRepositoryMapper docsRepositoryMapper;

@Transactional
public DocsResponseDto execute(final DocsCreateRequestDto docsCreateRequestDto) throws IOException {

docsRepositoryMapper.checkTitleAlreadyExist(docsCreateRequestDto.getTitle());

Docs docs = docsRepositoryMapper.createDocs(docsCreateRequestDto);

VersionDocs savedVersionDocs = saveVersionDocs(docsCreateRequestDto, docs.getId());
Contribute contribute = contributeService.setContribute(savedVersionDocs);

setVersionDocs(docs, savedVersionDocs, contribute);

return new DocsResponseDto(docs);
}

private static void setVersionDocs(final Docs docs, final VersionDocs savedVersionDocs, final Contribute contribute) {
List<VersionDocs> versionDocs = new ArrayList<>();

versionDocs.add(savedVersionDocs);

savedVersionDocs.updateContributor(contribute);

docs.setVersionDocs(versionDocs);
}

private void setImageUrl(final DocsCreateRequestDto docsCreateRequestDto, final MultipartFile[] files) throws IOException {
if(files != null){
ArrayList<String> FileUrl = imageService.GetFileUrl(files, docsCreateRequestDto.getTitle());
setImageUrlInContents(docsCreateRequestDto,FileUrl);
}
}

/**
* 프론트가 [사진1]이라고 보낸거 우리가 저장한 이미지 주소로 바꾸는 로직
*/
public void setImageUrlInContents(final DocsCreateRequestDto docsCreateRequestDto, final ArrayList<String> urls){
String content = docsCreateRequestDto.getContents();
for (String url : urls) {
content = content.replaceFirst("<<사진>>",url);
}
docsCreateRequestDto.updateContent(content);
}

private VersionDocs saveVersionDocs(final DocsCreateRequestDto docsCreateRequestDto, final Long id){
return versionDocsRepository.save(
VersionDocs.builder()
.docsId(id)
.contents(docsCreateRequestDto.getContents())
.thisVersionCreatedAt(LocalDateTime.now())
.build()
);
}
private final VersionDocsRepository versionDocsRepository;
private final ImageService imageService;
private final ContributeService contributeService;
private final DocsRepositoryMapper docsRepositoryMapper;

@Transactional
public Long execute(final DocsCreateRequestDto docsCreateRequestDto) throws IOException {

docsRepositoryMapper.checkTitleAlreadyExist(docsCreateRequestDto.getTitle());

Docs docs = docsRepositoryMapper.createDocs(docsCreateRequestDto);

VersionDocs savedVersionDocs = saveVersionDocs(docsCreateRequestDto, docs.getId());
Contribute contribute = contributeService.setContribute(savedVersionDocs);

setVersionDocs(docs, savedVersionDocs, contribute);

return docs.getId();
}

private static void setVersionDocs(final Docs docs, final VersionDocs savedVersionDocs,
final Contribute contribute) {
List<VersionDocs> versionDocs = new ArrayList<>();

versionDocs.add(savedVersionDocs);

savedVersionDocs.updateContributor(contribute);

docs.setVersionDocs(versionDocs);
}

private void setImageUrl(final DocsCreateRequestDto docsCreateRequestDto, final MultipartFile[] files) throws
IOException {
if (files != null) {
ArrayList<String> FileUrl = imageService.GetFileUrl(files, docsCreateRequestDto.getTitle());
setImageUrlInContents(docsCreateRequestDto, FileUrl);
}
}

/**
* 프론트가 [사진1]이라고 보낸거 우리가 저장한 이미지 주소로 바꾸는 로직
*/
public void setImageUrlInContents(final DocsCreateRequestDto docsCreateRequestDto, final ArrayList<String> urls) {
String content = docsCreateRequestDto.getContents();
for (String url : urls) {
content = content.replaceFirst("<<사진>>", url);
}
docsCreateRequestDto.updateContent(content);
}

private VersionDocs saveVersionDocs(final DocsCreateRequestDto docsCreateRequestDto, final Long id) {
return versionDocsRepository.save(
VersionDocs.builder()
.docsId(id)
.contents(docsCreateRequestDto.getContents())
.thisVersionCreatedAt(LocalDateTime.now())
.build()
);
}
}


Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.bumawiki.domain.docs.service;

import com.project.bumawiki.domain.contribute.domain.repository.ContributeRepository;
import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.domain.docs.domain.repository.DocsRepository;
Expand All @@ -10,6 +11,8 @@
import com.project.bumawiki.domain.docs.presentation.dto.TeacherResponseDto;
import com.project.bumawiki.domain.docs.presentation.dto.VersionDocsSummaryDto;
import com.project.bumawiki.domain.docs.presentation.dto.response.*;
import com.project.bumawiki.domain.user.domain.User;

import lombok.RequiredArgsConstructor;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
import org.springframework.data.domain.Pageable;
Expand All @@ -32,6 +35,7 @@
@Transactional(readOnly = true)
public class DocsInformationService {
private final DocsRepository docsRepository;
private final ContributeRepository contributeRepository;

public List<DocsNameAndEnrollResponseDto> findByDocsType(final DocsType docsType) {
List<Docs> allStudent = docsRepository.findByDocsType(docsType);
Expand Down Expand Up @@ -77,7 +81,9 @@ public DocsResponseDto findDocs(String title) {
Docs docs = docsRepository.findByTitle(title).
orElseThrow(() -> DocsNotFoundException.EXCEPTION);

return new DocsResponseDto(docs);
List<User> contributors = contributeRepository.findUserAllByDocs(docs);

return new DocsResponseDto(docs, contributors);
}

public VersionResponseDto findDocsVersion(String title) {
Expand Down
Loading

0 comments on commit ae2bf26

Please sign in to comment.