Skip to content

Commit

Permalink
Feat(#86): 블록 순번 속성 추가, 순번 지정, 순번 변경 api 기능 구현, 테스트 코드 구현 (#87)
Browse files Browse the repository at this point in the history
* Feat(#86): 블록 순번 속성 추가, 순번 지정, 순번 변경 api 기능 구현

* Test(#86): 블록 순번 속성 추가와 지정함으로써 테스트 코드 구현

* Docs(#86): restdocs 문서 추가

* Docs(#86): restdocs 문서 수정을 위한 테스트 코드 수정

* Refactor(#86): 블록의 대시보드 반환을 위한 dType 속성, 반환 값 추가

* Style: 끝 줄 추가
  • Loading branch information
giwoong01 authored Aug 31, 2024
1 parent f9d36bd commit d9c03ab
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 20 deletions.
12 changes: 11 additions & 1 deletion src/docs/asciidoc/block.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ include::{snippets}/block/delete/path-parameters.adoc[]

==== 응답

include::{snippets}/block/delete/http-response.adoc[]
include::{snippets}/block/delete/http-response.adoc[]

=== 블록 상태 변경

==== 요청

include::{snippets}/block/change/http-request.adoc[]

==== 응답

include::{snippets}/block/change/http-response.adoc[]
2 changes: 1 addition & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
include::block.adoc[]
include::challenge.adoc[]

include::peronsalDashboard.adoc[]
include::personalDashboard.adoc[]
include::notification.adoc[]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockSaveReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockSequenceUpdateReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockUpdateReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.response.BlockInfoResDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.response.BlockListResDto;
Expand Down Expand Up @@ -71,4 +72,11 @@ public RspTemplate<BlockInfoResDto> findById(@CurrentUserEmail String email,
return new RspTemplate<>(HttpStatus.OK, "블록 상세보기", blockService.findById(email, blockId));
}

@PatchMapping("/change")
public RspTemplate<Void> changeBlocksSequence(@CurrentUserEmail String email,
@RequestBody BlockSequenceUpdateReqDto blockSequenceUpdateReqDto) {
blockService.changeBlocksSequence(email, blockSequenceUpdateReqDto);
return new RspTemplate<>(HttpStatus.OK, "블록 순서 변경");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public record BlockSaveReqDto(
String startDate,
String deadLine
) {
public Block toEntity(Member member, Dashboard dashboard) {
public Block toEntity(Member member, Dashboard dashboard, int lastSequence) {
return Block.builder()
.title(title)
.contents(contents)
.progress(progress)
.type(Type.BASIC)
.startDate(startDate)
.deadLine(deadLine)
.sequence(lastSequence + 1)
.member(member)
.dashboard(dashboard)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package shop.kkeujeok.kkeujeokbackend.block.api.dto.request;

import java.util.List;

public record BlockSequenceUpdateReqDto(
List<Long> notStartedList,
List<Long> inProgressList,
List<Long> completedList
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public record BlockInfoResDto(
String contents,
Progress progress,
Type type,
String dType,
String startDate,
String deadLine,
String nickname,
Expand All @@ -28,6 +29,7 @@ public static BlockInfoResDto from(Block block) {
.contents(block.getContents())
.progress(block.getProgress())
.type(block.getType())
.dType(block.getDashboard().getDType())
.startDate(block.getStartDate())
.deadLine(block.getDeadLine())
.nickname(block.getMember().getNickname())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockSaveReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockSequenceUpdateReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.request.BlockUpdateReqDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.response.BlockInfoResDto;
import shop.kkeujeok.kkeujeokbackend.block.api.dto.response.BlockListResDto;
Expand Down Expand Up @@ -38,7 +39,13 @@ public BlockInfoResDto save(String email, BlockSaveReqDto blockSaveReqDto) {
Member member = memberRepository.findByEmail(email).orElseThrow(MemberNotFoundException::new);
Dashboard dashboard = dashboardRepository.findById(blockSaveReqDto.dashboardId())
.orElseThrow(DashboardNotFoundException::new);
Block block = blockRepository.save(blockSaveReqDto.toEntity(member, dashboard));

int lastSequence = blockRepository.findLastSequenceByProgress(
member,
dashboard.getId(),
blockSaveReqDto.progress());

Block block = blockRepository.save(blockSaveReqDto.toEntity(member, dashboard, lastSequence));

return BlockInfoResDto.from(block);
}
Expand Down Expand Up @@ -99,6 +106,30 @@ public void delete(String email, Long blockId) {
block.statusUpdate();
}

// 블록 순번 변경
@Transactional
public void changeBlocksSequence(String email, BlockSequenceUpdateReqDto blockSequenceUpdateReqDto) {
Member member = memberRepository.findByEmail(email).orElseThrow(MemberNotFoundException::new);

updateBlockSequence(blockSequenceUpdateReqDto.notStartedList());
updateBlockSequence(blockSequenceUpdateReqDto.inProgressList());
updateBlockSequence(blockSequenceUpdateReqDto.completedList());
}

private void updateBlockSequence(List<Long> blockIds) {
int sequence = blockIds.size();

for (Long blockId : blockIds) {
Block block = blockRepository.findById(blockId).orElseThrow(BlockNotFoundException::new);

if (block.getSequence() != sequence) {
block.sequenceUpdate(sequence);
}

sequence--;
}
}

private Progress parseProgress(String progressString) {
try {
return Progress.valueOf(progressString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class Block extends BaseEntity {

private String deadLine;

private int sequence;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;
Expand All @@ -54,15 +56,15 @@ public class Block extends BaseEntity {

@Builder
private Block(String title, String contents, Progress progress, Type type, Member member, String startDate,
String deadLine,
Dashboard dashboard, Challenge challenge) {
String deadLine, int sequence, Dashboard dashboard, Challenge challenge) {
this.status = Status.ACTIVE;
this.title = title;
this.contents = contents;
this.progress = progress;
this.type = type;
this.startDate = startDate;
this.deadLine = deadLine;
this.sequence = sequence;
this.member = member;
this.dashboard = dashboard;
this.challenge = challenge;
Expand Down Expand Up @@ -99,4 +101,9 @@ public void updateChallengeStatus(Status status) {
}
this.status = status;
}

public void sequenceUpdate(int sequence) {
this.sequence = sequence;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.springframework.data.domain.Pageable;
import shop.kkeujeok.kkeujeokbackend.block.domain.Block;
import shop.kkeujeok.kkeujeokbackend.block.domain.Progress;
import shop.kkeujeok.kkeujeokbackend.member.domain.Member;

public interface BlockCustomRepository {
Page<Block> findByBlockWithProgress(Long dashboardId, Progress progress, Pageable pageable);

int findLastSequenceByProgress(Member member, Long dashboardId, Progress progress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -12,6 +13,7 @@
import shop.kkeujeok.kkeujeokbackend.block.domain.Block;
import shop.kkeujeok.kkeujeokbackend.block.domain.Progress;
import shop.kkeujeok.kkeujeokbackend.global.entity.Status;
import shop.kkeujeok.kkeujeokbackend.member.domain.Member;

@Repository
@Transactional(readOnly = true)
Expand All @@ -35,12 +37,29 @@ public Page<Block> findByBlockWithProgress(Long dashboardId, Progress progress,
.where(block.dashboard.id.eq(dashboardId)
.and(block.progress.eq(progress))
.and(block.status.eq(Status.ACTIVE)))
.orderBy(block.id.desc())
.orderBy(block.sequence.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

return new PageImpl<>(blocks, pageable, total);
}

@Override
public int findLastSequenceByProgress(Member member, Long dashboardId, Progress progress) {
return Optional.of(
Math.toIntExact(
queryFactory
.select(block.sequence)
.from(block)
.where(block.dashboard.id.eq(dashboardId)
.and(block.progress.eq(progress))
.and(block.status.eq(Status.ACTIVE)))
.stream()
.count()
)
)
.orElse(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@Entity
@Getter
@DiscriminatorColumn
@DiscriminatorColumn(name = "dtype")
@Inheritance(strategy = InheritanceType.JOINED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Dashboard extends BaseEntity {
Expand All @@ -36,17 +36,21 @@ public class Dashboard extends BaseEntity {
@Column(columnDefinition = "TEXT")
private String description;

@Column(name = "dtype", insertable = false, updatable = false)
private String dType;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@OneToMany(mappedBy = "dashboard", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Block> blocks = new ArrayList<>();

public Dashboard(String title, String description, Member member) {
public Dashboard(String title, String description, String dType, Member member) {
this.status = Status.ACTIVE;
this.title = title;
this.description = description;
this.dType = dType;
this.member = member;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class PersonalDashboard extends Dashboard {
private String category;

@Builder
private PersonalDashboard(String title, String description, Member member, boolean isPublic, String category) {
super(title, description, member);
private PersonalDashboard(String title, String description, String dType, Member member, boolean isPublic,
String category) {
super(title, description, dType, member);
this.category = category;
this.isPublic = isPublic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class TeamDashboard extends Dashboard {
private List<Document> documents = new ArrayList<>();

@Builder
private TeamDashboard(String title, String description, Member member) {
super(title, description, member);
private TeamDashboard(String title, String description, String dType, Member member) {
super(title, description, dType, member);
}

public void update(String updateTitle, String updateDescription) {
Expand Down
Loading

0 comments on commit d9c03ab

Please sign in to comment.