Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 체크리스트 업데이트 시 지하철 역도 변경되도록 수정한다 #882

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public ResponseEntity<Void> updateChecklistById(
return ResponseEntity.noContent().build();
}

@PutMapping("/v1/checklists/{id}")
public ResponseEntity<Void> updateChecklistByIdV1(
@AuthRequiredPrincipal User user,
@PathVariable("id") long id,
@Valid @RequestBody ChecklistRequestV1 checklistRequestV1) {
checklistManageService.updateChecklistByIdV1(user, id, checklistRequestV1);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/checklists/{id}")
public ResponseEntity<Void> deleteChecklistById(@AuthRequiredPrincipal User user, @PathVariable("id") long id) {
checklistManageService.deleteChecklistById(user, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ public void updateChecklistById(User user, Long checklistId, ChecklistRequest ch
updateChecklistMaintenances(checklistRequest, checklist);
}

@Transactional
public void updateChecklistByIdV1(User user, Long checklistId, ChecklistRequestV1 checklistRequestV1) {
Checklist checklist = checklistService.readChecklist(user, checklistId);

ChecklistRequest checklistRequest = checklistRequestV1.toChecklistRequest();
roomService.updateRoom(checklist.getRoom(), checklistRequest.toRoomEntity());
checklistService.updateChecklist(checklist, checklistRequest.toChecklistEntity(checklist.getRoom(), user));

updateChecklistOptions(checklistRequest, checklist);
updateChecklistQuestions(checklistRequest, checklist);
updateChecklistMaintenances(checklistRequest, checklist);
updateChecklistStations(checklistRequestV1, checklist);
}

private void updateChecklistOptions(ChecklistRequest checklistRequest, Checklist checklist) {
List<ChecklistOption> checklistOptions = checklistRequest.options().stream()
.map(option -> new ChecklistOption(checklist, option))
Expand Down Expand Up @@ -252,4 +266,10 @@ private void updateChecklistMaintenances(ChecklistRequest checklistRequest, Chec
.toList();
checklistMaintenanceService.updateMaintenances(checklist.getId(), checklistMaintenances);
}

private void updateChecklistStations(ChecklistRequestV1 checklistRequestV1, Checklist checklist) {
double latitude = checklistRequestV1.geolocation().latitude();
double longitude = checklistRequestV1.geolocation().longitude();
checklistStationService.updateChecklistStation(checklist, latitude, longitude);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.station.domain.ChecklistStation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

public interface ChecklistStationRepository extends JpaRepository<ChecklistStation, Long> {
Expand All @@ -13,4 +15,11 @@ public interface ChecklistStationRepository extends JpaRepository<ChecklistStati
"where cs.checklist = :checklist " +
"and cs.deleted = false")
List<ChecklistStation> findByChecklist(@Param("checklist") Checklist checklist);

@Transactional
@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("UPDATE ChecklistStation cs " +
"SET cs.deleted = true " +
"WHERE cs.checklist.id = :checklistId")
void deleteAllByChecklistId(Long checklistId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param 붙여주세용 ㅠㅠ 자꾸 터져요 ..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다!

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public class ChecklistStationService {

@Transactional
public void createChecklistStations(Checklist checklist, double latitude, double longitude) {
saveChecklistStations(checklist, latitude, longitude);
}

@Transactional(readOnly = true)
public List<ChecklistStation> readChecklistStationsByChecklist(Checklist checklist) {
return checklistStationRepository.findByChecklist(checklist);
}

@Transactional
public void updateChecklistStation(Checklist checklist, double latitude, double longitude) {
checklistStationRepository.deleteAllByChecklistId(checklist.getId());
saveChecklistStations(checklist, latitude, longitude);
}

private void saveChecklistStations(Checklist checklist, double latitude, double longitude) {
List<SubwayStationResponse> responses = subwayStationService.readNearestStation(latitude, longitude)
.getStations();
List<ChecklistStation> checklistStations = new ArrayList<>();
Expand All @@ -32,9 +47,4 @@ public void createChecklistStations(Checklist checklist, double latitude, double

checklistStationRepository.saveAll(checklistStations);
}

@Transactional(readOnly = true)
public List<ChecklistStation> readChecklistStationsByChecklist(Checklist checklist) {
return checklistStationRepository.findByChecklist(checklist);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ public static ChecklistRequest CHECKLIST_UPDATE_REQUEST() {
);
}

public static ChecklistRequestV1 CHECKLIST_UPDATE_REQUEST_V1() {
return new ChecklistRequestV1(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 3, 4),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 3, 4),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.BED.getId(), Option.INDUCTION.getId() ... ),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영했습니다!

List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_UPDATE_REQUEST()),
ChecklistStationRequest.of(37.5, 127.1)
);
}

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_NO_ROOM_NAME() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST_NO_ROOM_NAME(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ void updateChecklist() {
.statusCode(204);
}

@DisplayName("체크리스트 수정 v1 성공")
@Test
void updateChecklistV1() {
long checklistId = checklistManageService.createChecklistV1(this.getAuthenticatedUser(),
ChecklistFixture.CHECKLIST_CREATE_REQUEST_V1());

RestAssured.given().log().all()
.contentType(ContentType.JSON)
.headers(this.headers)
.body(ChecklistFixture.CHECKLIST_UPDATE_REQUEST_V1())
.when().put("/v1/checklists/" + checklistId)
.then().log().all()
.statusCode(204);
}

@DisplayName("체크리스트 수정 실패: 방 이름을 넣지 않은 경우")
@Test
void updateChecklist_noRoomName_exception() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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

public class ChecklistRepositoryTest extends IntegrationTestSupport {
public class ChecklistStationRepositoryTest extends IntegrationTestSupport {

@Autowired
RoomRepository roomRepository;
Expand All @@ -30,11 +30,12 @@ public class ChecklistRepositoryTest extends IntegrationTestSupport {
@Autowired
UserRepository userRepository;

@Autowired
ChecklistStationRepository checklistStationRepository;

Room room;
User user;
Checklist checklist;
@Autowired
ChecklistStationRepository checklistStationRepository;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -63,4 +64,19 @@ void findByChecklistId_noData_exception() {
assertThat(checklistStationRepository.findByChecklist(checklist))
.isEmpty();
}

@DisplayName("체크리스트 아이디를 통한 삭제 성공")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DisplayName에 지하철에 대한 내용이 포함되면 좋겠어요!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChecklistStationRepositoryTest라 저는 포함하지 않아도 괜찮을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일단 안붙이는 방향으로 가보겠습니다!

@Test
void deleteAllByChecklistId_noData_exception() {
// given
ChecklistStation checklistStation1 = new ChecklistStation(checklist, "잠실", "2호선", 5);
ChecklistStation checklistStation2 = new ChecklistStation(checklist, "잠실", "8호선", 6);
checklistStationRepository.saveAll(List.of(checklistStation1, checklistStation2));

// when
checklistStationRepository.deleteAllByChecklistId(checklist.getId());

// then
assertThat(checklistStationRepository.findByChecklist(checklist)).isEmpty();
}
}
Loading