Skip to content

Commit

Permalink
feat-be: 대시보드 생성 (#65)
Browse files Browse the repository at this point in the history
* Create draft PR for #64

* feat(Dashboard): 대시보드 생성 기능 구현

- 대시보드 생성시 기본 프로세스 2개가 생성된다.
    - 서류지원 프로세스
    - 합격 프로세스

* refactor(DashboardService): create 메서드 @transactional 추가

* refactor(DashboardControllerTest): 불필요한 코드 제거

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: leetaehoon <xogns1514@gmail.com>
  • Loading branch information
2 people authored and Dobby-Kim committed Jul 22, 2024
1 parent 7db6f3b commit 9748b39
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/cruru/club/domain/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class Club {
@JoinColumn(name = "member_id")
private Member member;

public Club(String name, Member member) {
this.name = name;
this.member = member;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/cruru/club/exception/ClubNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cruru.club.exception;

import com.cruru.advice.NotFoundException;

public class ClubNotFoundException extends NotFoundException {

private static final String MESSAGE = "존재하지 않는 동아리입니다.";

public ClubNotFoundException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cruru.dashboard.controller;

import com.cruru.dashboard.controller.dto.DashboardCreateDto;
import com.cruru.dashboard.service.DashboardService;
import com.cruru.member.controller.dto.MemberCreateResponse;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/dashboards")
@RequiredArgsConstructor
public class DashboardController {

private final DashboardService dashboardService;

@PostMapping
public ResponseEntity<MemberCreateResponse> create(
@RequestParam(name = "club_id") Long clubId,
@RequestBody DashboardCreateDto request) {
Long dashboardId = dashboardService.create(clubId, request);
return ResponseEntity.created(URI.create("/v1/dashboards/" + dashboardId)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.cruru.dashboard.controller.dto;

public record DashboardCreateDto(String name) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class DashboardNotFoundException extends NotFoundException {

private static final String message = "존재하지 않는 대시보드입니다.";
private static final String MESSAGE = "존재하지 않는 대시보드입니다.";

public DashboardNotFoundException() {
super(message);
super(MESSAGE);
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/cruru/dashboard/service/DashboardService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.cruru.dashboard.service;

import com.cruru.club.domain.Club;
import com.cruru.club.domain.repository.ClubRepository;
import com.cruru.club.exception.ClubNotFoundException;
import com.cruru.dashboard.controller.dto.DashboardCreateDto;
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class DashboardService {

private static final Process DEFAULT_FIRST_PROCESS = new Process(1, "지원 접수", "지원자가 이력서를 제출하는 단계", null);
private static final Process DEFAULT_LAST_PROCESS = new Process(2, "합격", "지원자가 최종적으로 합격한 단계", null);

private final DashboardRepository dashboardRepository;
private final ClubRepository clubRepository;
private final ProcessRepository processRepository;

@Transactional
public Long create(long clubId, DashboardCreateDto request) {
Club club = clubRepository.findById(clubId).orElseThrow(ClubNotFoundException::new);

Dashboard savedDashboard = dashboardRepository.save(new Dashboard(request.name(), club));

Process firstProcess = new Process(
DEFAULT_FIRST_PROCESS.getSequence(),
DEFAULT_FIRST_PROCESS.getName(),
DEFAULT_FIRST_PROCESS.getDescription(),
savedDashboard
);

Process lastProcess = new Process(
DEFAULT_LAST_PROCESS.getSequence(),
DEFAULT_LAST_PROCESS.getName(),
DEFAULT_LAST_PROCESS.getDescription(),
savedDashboard
);

processRepository.saveAll(List.of(firstProcess, lastProcess));

return savedDashboard.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cruru.dashboard.controller;

import com.cruru.club.domain.Club;
import com.cruru.club.domain.repository.ClubRepository;
import com.cruru.dashboard.controller.dto.DashboardCreateDto;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;

@DisplayName("대시보드 컨트롤러 테스트")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DashboardControllerTest {

@LocalServerPort
private int port;

@Autowired
private ClubRepository clubRepository;

private Club club;

@BeforeEach
void setUp() {
RestAssured.port = port;
club = clubRepository.save(new Club("크루루 동아리", null));
}

@DisplayName("대시보드 생성 성공 시, 201을 응답한다.")
@Test
void read() {
DashboardCreateDto request = new DashboardCreateDto("크루루대시보드");

RestAssured.given().log().all()
.contentType(ContentType.JSON)
.body(request)
.when().post("/v1/dashboards?club_id=" + club.getId())
.then().log().all().statusCode(201);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.cruru.dashboard.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.cruru.applicant.domain.repository.ApplicantRepository;
import com.cruru.club.domain.Club;
import com.cruru.club.domain.repository.ClubRepository;
import com.cruru.dashboard.controller.dto.DashboardCreateDto;
import com.cruru.dashboard.domain.repository.DashboardRepository;
import com.cruru.process.domain.Process;
import com.cruru.process.domain.repository.ProcessRepository;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@DisplayName("대시보드 서비스 테스트")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class DashboardServiceTest {

@Autowired
DashboardService dashboardService;

@Autowired
DashboardRepository dashboardRepository;

@Autowired
ProcessRepository processRepository;

@Autowired
ClubRepository clubRepository;

@Autowired
ApplicantRepository applicantRepository;

@BeforeEach
void setUp() {
applicantRepository.deleteAll();
processRepository.deleteAll();
dashboardRepository.deleteAll();
clubRepository.deleteAll();
}

@DisplayName("새로운 대시보드를 생성시, 기본 프로세스 2개가 생성된다.")
@Test
void createDefaultProcessWhenCreateDashBoard() {
// given
Club club = new Club("크루루동아리", null);
clubRepository.save(club);

// when
DashboardCreateDto request = new DashboardCreateDto("크루루대시보드");
Long dashboardId = dashboardService.create(club.getId(), request);

// then
List<Process> processes = processRepository.findAllByDashboardId(dashboardId);
assertThat(processes).hasSize(2);
}
}

0 comments on commit 9748b39

Please sign in to comment.