Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/#116
Browse files Browse the repository at this point in the history
  • Loading branch information
sdylockon committed Apr 27, 2023
2 parents 6eed28d + c8a97ac commit 7eda0ba
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -50,6 +51,15 @@ public ResponseEntity<ResultResponse> findRecentLogByUuid(@PathVariable UUID uui
return ResponseEntity.ok(ResultResponse.of(READ_BRANCH_RECENT_LOG_SUCCESS, branchRecentLogInfo));
}

@GetMapping("/{uuid}/merge")
public ResponseEntity<ResultResponse> getMergeResourceListByUuid(@PathVariable UUID uuid)
{
List<MergeResourceInfo> mergeResourceInfos = branchService.getMergeResources(uuid);
MergeResourceInfoList mergeResourceInfoList = new MergeResourceInfoList();
mergeResourceInfoList.addAllMergeResourceInfo(mergeResourceInfos);
return ResponseEntity.ok(ResultResponse.of(READ_BRANCH_MERGE_LIST, mergeResourceInfoList));
}


@PatchMapping("/{uuid}")
public ResponseEntity<ResultResponse> updateBranch(@PathVariable UUID uuid, @RequestBody Map<String,String> request)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package crepe.backend.domain.branch.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class MergeResourceInfo {

private String name;
private String link;
private boolean isDuplicated;

private boolean isNew;

@Builder
public MergeResourceInfo(String name, String link, boolean isDuplicated, boolean isNew) {
this.name = name;
this.link = link;
this.isDuplicated = isDuplicated;
this.isNew = isNew; //새로 추가된-변경된 파일인가
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package crepe.backend.domain.branch.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor
public class MergeResourceInfoList {
List<MergeResourceInfo> mergeResourceInfos = new ArrayList<>();

public void addAllMergeResourceInfo(List<MergeResourceInfo> mergeResourceInfos)
{
this.mergeResourceInfos.addAll(mergeResourceInfos);
}

public void addMergeResourceInfo(MergeResourceInfo mergeResourceInfo)
{
this.mergeResourceInfos.add(mergeResourceInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import crepe.backend.domain.branch.domain.entity.Branch;
import crepe.backend.domain.branch.domain.repository.BranchRepository;
import crepe.backend.domain.branch.dto.*;
import crepe.backend.domain.log.domain.entity.Layer;
import crepe.backend.domain.log.domain.entity.Log;
import crepe.backend.domain.log.domain.entity.Resource;
import crepe.backend.domain.log.domain.repository.LayerRepository;
import crepe.backend.domain.log.domain.repository.LogRepository;

import crepe.backend.domain.log.domain.repository.ResourceRepository;
import crepe.backend.domain.project.domain.entity.Project;
import crepe.backend.domain.project.exception.NotFoundResourceEntity;
import crepe.backend.domain.project.service.ProjectService;
import crepe.backend.domain.branch.exception.NotFoundBranchEntityException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;

@Service
@RequiredArgsConstructor
Expand All @@ -26,6 +28,9 @@ public class BranchService {
private final BranchRepository branchRepository;
private final ProjectService projectService;
private final LogRepository logRepository;
private final LayerRepository layerRepository;
private final ResourceRepository resourceRepository;


public BranchCreateInfo branchCreate(BranchCreate createRequest) { // 브랜치를 생성하는 모듈
Project findProject = projectService.getProjectById(createRequest.getProjectId());
Expand Down Expand Up @@ -111,11 +116,204 @@ public void deleteBranch(UUID uuid) {
private Log getRecentLogByBranch(Branch branch) {
return logRepository.findAllByBranchAndIsActiveTrueOrderByCreatedAtDesc(branch).get(0);
}

private Branch getBranchByUuid(UUID uuid) {
return branchRepository.findBranchByUuidAndIsActiveTrue(uuid).orElseThrow(NotFoundBranchEntityException::new);
}

public BranchRecentLogInfo findRecentLogInfoByUuid(UUID uuid) {
Branch branch = findBranchByUuid(uuid);
Log log = getRecentLogByBranch(branch);
return BranchRecentLogInfo.builder()
.uuid(log.getUuid())
.build();
}

public List<MergeResourceInfo> getMergeResources(UUID uuid) {

Branch branch = getBranchByUuid(uuid);
//메인브랜치는 삭제 될 일 없으니 0번 받아옴
Branch mainBranch = branch.getProject().getBranches().get(0);
System.out.println("==============================");
System.out.println("<branch>");
System.out.println(branch.getName());
System.out.println("------------------------------");
System.out.println(mainBranch.getName());

// isActive 상태의 가장 최신 로그
Log log = getRecentLogByBranch(branch);
Log mainLog = getRecentLogByBranch(mainBranch);
System.out.println("==============================");
System.out.println("<log>");
System.out.println(log.getMessage());
System.out.println("------------------------------");
System.out.println(mainLog.getMessage());

// 정렬된 모든 레이어 받아옴
List<Layer> layers = layerRepository.findAllByLogAndIsActiveTrueOrderBySequence(log);
List<Layer> mainLayers = layerRepository.findAllByLogAndIsActiveTrueOrderBySequence(mainLog);
System.out.println("==============================");
System.out.println("<layer>");
for(Layer layer: layers){
System.out.println(layer.getResource().getName());
}
System.out.println("------------------------------");
for(Layer layer: mainLayers){
System.out.println(layer.getResource().getName());
}

List<Resource> resources = getResourcesByLayer(layers);
List<Resource> mainResources = getResourcesByLayer(mainLayers);
System.out.println("==============================");
System.out.println("<resource>");
for(Resource resource: resources){
System.out.println(resource.getName());
}
System.out.println("------------------------------");
for(Resource resource: mainResources){
System.out.println(resource.getName());
}

return getMergeResourceInfoList(resources, mainResources);
}

//리소스 리스트 반환
private List<Resource> getResourcesByLayer(List<Layer> layers) {
List<Resource> resources = new ArrayList<>();
for(Layer layer: layers) {
if (layer.getResource().isActive() == true) {
resources.add(layer.getResource());
}
//resources.add(getResourceById(layer.getResource().getId()));
}
return resources;
}

//리소스 아이디로 리소스 찾기
private Resource getResourceById(Long id) {
return resourceRepository.findResourceByIdAndIsActiveTrue(id).orElseThrow(NotFoundResourceEntity::new);
}

private List<MergeResourceInfo> getMergeResourceInfoList(List<Resource> resources, List<Resource> mainResources) {
List<MergeResourceInfo> mergeResourceInfos = new ArrayList<>();

int index = 0;
boolean isIn = false;

List<List<String>> branchFileInfos = getFileInfoList(resources);
List<List<String>> mainFileInfos = getFileInfoList(mainResources);

System.out.println("==============================");
System.out.println("branch: "+branchFileInfos);
System.out.println("main: "+mainFileInfos);

while ((!branchFileInfos.isEmpty()) &&(!mainFileInfos.isEmpty())) {
// 브랜치의 0번 요소 가져오기
String currData = branchFileInfos.get(0).get(0);
System.out.println("data(branch):"+currData);
// 메인에 해당 파일이 있는지 검사
for (List<String> mainFileInfo: mainFileInfos) {
if (mainFileInfo.contains(currData)) {
isIn = true;
index = mainFileInfos.indexOf(mainFileInfo);
}
}
if (isIn) { // 있으면
System.out.println("* Isin:"+isIn);
mergeResourceInfos.add(mapMergeResourceInfo(
branchFileInfos.get(0).get(0), //fileName
branchFileInfos.get(0).get(1), //fileLink
true,
true));
branchFileInfos.remove(0);
mainFileInfos.remove(index);
isIn = false;
} else { // 없으면
System.out.println("* Isin:"+isIn);
mergeResourceInfos.add(mapMergeResourceInfo(
branchFileInfos.get(0).get(0),
branchFileInfos.get(0).get(1),
false,
true));
branchFileInfos.remove(0);
}
// 메인의 0번 요소 가져오기
if (mainFileInfos.isEmpty()) {
break;
}
currData = mainFileInfos.get(0).get(0);
System.out.println("data(main):"+currData);
// 브랜치에 해당 파일이 있는지 검사
for (List<String> branchFileInfo: branchFileInfos) {
if (branchFileInfo.contains(currData)) {
isIn = true;
index = branchFileInfos.indexOf(branchFileInfo);
}
}
if (isIn) { // 있으면
System.out.println("* Isin:"+isIn);
isIn = false;
} else { // 없으면
System.out.println("* Isin:"+isIn);
mergeResourceInfos.add(mapMergeResourceInfo(
mainFileInfos.get(0).get(0),
mainFileInfos.get(0).get(1),
false,
false));
mainFileInfos.remove(0);
}
}
// 브랜치에 리소스 요소가 남은 경우
if (!branchFileInfos.isEmpty()) {
System.out.println("data(branch):" + branchFileInfos.get(0).get(0));

for (List<String> branchFileInfo: branchFileInfos) {
mergeResourceInfos.add(mapMergeResourceInfo(
branchFileInfos.get(0).get(0),
branchFileInfos.get(0).get(1),
false,
true));
}
}

if (!mainFileInfos.isEmpty()) {
System.out.println("data(main):" + branchFileInfos.get(0).get(0));
for (List<String> mainFileInfo: mainFileInfos) {
mergeResourceInfos.add(mapMergeResourceInfo(
mainFileInfos.get(0).get(0),
mainFileInfos.get(0).get(1),
false,
false));
}
}

System.out.println("==============================");
System.out.println("<mergeResourceInfo>");
for (MergeResourceInfo mergeResourceInfo: mergeResourceInfos){
System.out.println(mergeResourceInfo.getName());
System.out.println(mergeResourceInfo.getLink());
}

return mergeResourceInfos;
}

private List<List<String>> getFileInfoList(List<Resource> resources) {
List<List<String>> fileInfos = new ArrayList<>();
for (Resource resource: resources) {
fileInfos.add(Arrays.asList(resource.getName(), resource.getLink()));
}
return fileInfos;
}

private MergeResourceInfo mapMergeResourceInfo(String name,
String link,
boolean isDuplicated,
boolean isNew) {
return MergeResourceInfo.builder()
.name(name)
.link(link)
.isDuplicated(isDuplicated)
.isNew(isNew)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import crepe.backend.domain.log.domain.entity.Log;
import crepe.backend.domain.log.domain.entity.Resource;
import crepe.backend.domain.log.domain.repository.ResourceRepository;
import crepe.backend.domain.log.dto.LogCreateRequest;
import crepe.backend.domain.log.dto.LogUuidInfo;
import crepe.backend.domain.log.service.LayerService;
import crepe.backend.domain.log.service.LogService;
import crepe.backend.domain.log.service.ResourceService;
import crepe.backend.global.exception.BusinessException;
Expand All @@ -30,7 +28,6 @@ public class LogController {

private final LogService logService;
private final ResourceService resourceService;
private final LayerService layerService;

private final S3Service s3Service;

Expand All @@ -46,7 +43,6 @@ public ResponseEntity<ResultResponse> createLog(
Log log = logService.createLog(request);
LogUuidInfo logUuidInfo = logService.createLogUuidInfo(log);
logService.createLayer(log, resources);
// layerService.createLayer(log, resources);

return ResponseEntity.ok(ResultResponse.of(ResultCode.CREATE_LOG_SUCCESS, logUuidInfo));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import crepe.backend.domain.log.domain.entity.Layer;
import crepe.backend.domain.log.domain.entity.Log;
import crepe.backend.domain.log.domain.entity.Resource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -10,4 +11,6 @@
@Repository
public interface LayerRepository extends JpaRepository<Layer, Long> {
List<Layer> findAllByLogAndIsActiveTrueOrderBySequence(Log log);

Layer findByResourceAndIsActiveTrue(Resource resource);
}
Loading

0 comments on commit 7eda0ba

Please sign in to comment.