Skip to content

Commit

Permalink
refactor(update): docs update로직 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
qlido committed Apr 10, 2024
1 parent 83b057c commit 59131a7
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void updateDocsType(DocsType docsType) {
this.docsType = docsType;
}

public void setModifiedTime(LocalDateTime lastModifiedAt) {
public void updateModifiedAt(LocalDateTime lastModifiedAt) {
this.lastModifiedAt = lastModifiedAt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.project.bumawiki.domain.docs.domain.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.lang.NonNull;

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

public interface VersionDocsRepository extends JpaRepository<VersionDocs, Long> {

@NonNull
VersionDocs findFirstByDocsOrderByVersionDesc(Docs docs);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public Docs findById(Long docsId) {
return docsRepository.findById(docsId)
.orElseThrow(() -> new BumawikiException(ErrorCode.DOCS_NOT_FOUND));
}

public Docs findByTitle(String title) {
return docsRepository.findByTitle(title)
.orElseThrow(() -> new BumawikiException(ErrorCode.DOCS_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.project.bumawiki.domain.docs.implementation;

import java.time.LocalDateTime;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.type.DocsType;
import com.project.bumawiki.domain.docs.domain.type.Status;
import com.project.bumawiki.global.annotation.Implementation;

@Implementation
public class DocsUpdater {
public void updateStatus(Docs docs, Status status) {
docs.updateStatus(status);
}

public void updateTitle(Docs docs, String title) {
docs.updateTitle(title);
}

public void updateType(Docs docs, DocsType docsType) {
docs.updateDocsType(docsType);
}

public void updateModifiedAt(Docs docs, LocalDateTime localDateTime) {
docs.updateModifiedAt(localDateTime);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.repository.DocsRepository;
import com.project.bumawiki.domain.docs.domain.type.DocsType;
import com.project.bumawiki.domain.docs.domain.type.Status;
import com.project.bumawiki.domain.docs.exception.NoUpdatableDocsException;
import com.project.bumawiki.domain.user.domain.User;
import com.project.bumawiki.global.annotation.Implementation;
import com.project.bumawiki.global.error.exception.BumawikiException;
import com.project.bumawiki.global.error.exception.ErrorCode;
Expand All @@ -24,4 +28,30 @@ public void checkDocsExist(Long docsId) {
throw new BumawikiException(ErrorCode.DOCS_NOT_FOUND);
}
}

public void checkUpdateOneSelf(User user, Docs docs) {
// 학생문서는 이름과 연도가 일치할 떄 수정이 불가함
if (docs.getDocsType().equals(DocsType.STUDENT)) {
if (docs.getTitle().contains(user.getName()) && docs.getEnroll().equals(user.getEnroll())) {
throw new BumawikiException(ErrorCode.CANNOT_CHANGE_YOUR_DOCS);
}
return;
}
// 일반문서는 이름이 일치할 때 수정이 불가함
if (docs.getTitle().contains(user.getName())) {
throw new BumawikiException(ErrorCode.CANNOT_CHANGE_YOUR_DOCS);
}
}

public void checkUpdatableDocsType(DocsType docsType) {
if (docsType.equals(DocsType.READONLY)) {
throw NoUpdatableDocsException.EXCEPTION;
}
}

public void checkConflicted(Docs docs) {
if (docs.getStatus().equals(Status.CONFLICTED)) {
throw new BumawikiException(ErrorCode.DOCS_CONFLICTED);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.project.bumawiki.domain.docs.implementation.versiondocs;

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.implementation.DocsUpdater;
import com.project.bumawiki.domain.user.domain.User;
import com.project.bumawiki.global.annotation.Implementation;

import lombok.RequiredArgsConstructor;

@Implementation
@RequiredArgsConstructor
public class VersionDocsCreator {
private final VersionDocsReader versionDocsReader;
private final DocsUpdater docsUpdater;
private final VersionDocsRepository versionDocsRepository;

public void create(Docs docs, User user, String contents) {
Integer lastVersion = versionDocsReader.findLastVersion(docs).getVersion();
VersionDocs versionDocs = versionDocsRepository.save(
new VersionDocs(
lastVersion + 1,
docs,
contents,
user
)
);
docsUpdater.updateModifiedAt(docs, versionDocs.getThisVersionCreatedAt());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.project.bumawiki.domain.docs.implementation.versiondocs;

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.global.annotation.Implementation;

import lombok.RequiredArgsConstructor;

@Implementation
@RequiredArgsConstructor
public class VersionDocsReader {
private final VersionDocsRepository versionDocsRepository;

public VersionDocs findLastVersion(Docs docs) {
return versionDocsRepository.findFirstByDocsOrderByVersionDesc(docs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.project.bumawiki.domain.docs.implementation.versiondocs;

import com.project.bumawiki.domain.docs.domain.Docs;
import com.project.bumawiki.domain.docs.domain.VersionDocs;
import com.project.bumawiki.global.annotation.Implementation;

import lombok.RequiredArgsConstructor;

@Implementation
@RequiredArgsConstructor
public class VersionDocsValidator {
private final VersionDocsReader versionDocsReader;

public boolean isConflict(Docs docs, Integer updatingVersion) {
VersionDocs lastVersionDocs = versionDocsReader.findLastVersion(docs);
return !lastVersionDocs.getVersion().equals(updatingVersion);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.project.bumawiki.domain.docs.presentation;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -26,31 +24,35 @@
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/docs")
@ResponseStatus(HttpStatus.NO_CONTENT)
public class DocsCreateUpdateController {

private final DocsUpdateService docsUpdateService;
private final DocsCreateService docsCreateService;

@PostMapping("/create")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void createDocs(@RequestBody DocsCreateRequestDto request) {
docsCreateService.execute(request.toEntity(), SecurityUtil.getCurrentUserWithLogin(), request.contents());
}

@PutMapping("/update/{title}")
public ResponseEntity<Long> updateDocs(@RequestHeader("Authorization") String bearer, @PathVariable String title,
public void updateDocs(@PathVariable String title,
@RequestBody DocsUpdateRequestDto request) {
return ResponseEntity.ok(docsUpdateService.execute(bearer, title, request));
docsUpdateService.execute(
SecurityUtil.getCurrentUserWithLogin(),
title,
request.contents(),
request.updatingVersion());
}

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

@PutMapping("/update/docsType")
public ResponseEntity<Long> updateDocsType(@RequestBody DocsTypeUpdateDto requestDto) {
return ResponseEntity.ok(docsUpdateService.docsTypeUpdate(requestDto));
public void updateDocsType(@RequestBody DocsTypeUpdateDto requestDto) {
docsUpdateService.docsTypeUpdate(requestDto.id(), requestDto.docsType());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.project.bumawiki.domain.docs.presentation.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;

@Getter
public class DocsTitleUpdateRequestDto {
public record DocsTitleUpdateRequestDto(
@NotBlank
private String title;
String title
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.project.bumawiki.domain.docs.domain.type.DocsType;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;

@Getter
public class DocsTypeUpdateDto {

@NotBlank
Long id;
@NotBlank
DocsType docsType;

import jakarta.validation.constraints.NotNull;

public record DocsTypeUpdateDto(
@NotNull
Long id,
@NotNull
DocsType docsType
) {
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.project.bumawiki.domain.docs.presentation.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import jakarta.validation.constraints.Positive;

@Getter
public class DocsUpdateRequestDto {

@NotNull
private String contents;
public record DocsUpdateRequestDto(
@NotBlank(message = "업데이트할 문서의 내용이 전달되지 않았습니다.")
String contents,

@Positive(message = "업데이트할 문서는 양수여야합니다")
@NotNull(message = "업데이트할 문서의 버전이 전달되지 않았습니다.")
private int updatingVersion;
Integer updatingVersion
) {
}
Loading

0 comments on commit 59131a7

Please sign in to comment.