From 37bf27682df000a58412a58ef9b605d71916b1f6 Mon Sep 17 00:00:00 2001 From: Urio999 Date: Fri, 6 Dec 2024 00:53:07 +0200 Subject: [PATCH] initial commit --- .../ManagementHabitController.java | 4 +- core/src/main/resources/messages.properties | 4 ++ .../core/management_user_habits.html | 58 +++++++++++++++++++ .../ManagementHabitControllerTest.java | 8 ++- .../dto/habit/HabitManagementDto.java | 2 + .../service/ManagementHabitService.java | 12 ++-- .../mapping/HabitManagementDtoMapper.java | 2 + .../service/ManagementHabitServiceImpl.java | 8 +-- .../ManagementHabitServiceImplTest.java | 11 ++-- 9 files changed, 90 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/greencity/webcontroller/ManagementHabitController.java b/core/src/main/java/greencity/webcontroller/ManagementHabitController.java index 03a5eca771..5a6c0caf95 100644 --- a/core/src/main/java/greencity/webcontroller/ManagementHabitController.java +++ b/core/src/main/java/greencity/webcontroller/ManagementHabitController.java @@ -222,8 +222,8 @@ public ResponseEntity> deleteAll(@RequestBody List listId) { @ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN) }) @PatchMapping("/switch-deleted-status/{id}") - public ResponseEntity switchIsDeletedStatus(@PathVariable("id") Long id) { - managementHabitService.switchIsDeletedStatus(id); + public ResponseEntity switchIsDeletedStatus(@PathVariable("id") Long id, @RequestBody Boolean newStatus) { + managementHabitService.switchIsDeletedStatus(id, newStatus); return ResponseEntity.status(HttpStatus.OK).body(id); } } diff --git a/core/src/main/resources/messages.properties b/core/src/main/resources/messages.properties index ed244d0535..8262fcce3e 100644 --- a/core/src/main/resources/messages.properties +++ b/core/src/main/resources/messages.properties @@ -156,6 +156,10 @@ greenCity.pages.table.startDate=Start date greenCity.pages.table.endDate=End date greenCity.pages.table.ready=Ready greenCity.pages.table.location=Location +greenCity.pages.table.isCustomHabit=Custom Habit +greenCity.pages.table.isDeleted=Deleted (Hidden) +greenCity.pages.yes=true +greenCity.pages.no=false greenCity.pages.popup.delete.all.h=Delete all selected greenCity.pages.popup.delete.all.h1=Are you sure you want to delete these Records? greenCity.pages.popup.delete.all.h2=This action cannot be undone. diff --git a/core/src/main/resources/templates/core/management_user_habits.html b/core/src/main/resources/templates/core/management_user_habits.html index 6f4e1013c0..778857637b 100644 --- a/core/src/main/resources/templates/core/management_user_habits.html +++ b/core/src/main/resources/templates/core/management_user_habits.html @@ -208,6 +208,8 @@

[[#{greenCity.habit.page.h}]]

[[#{greenCity.pages.table.description}]] + [[#{greenCity.pages.table.isCustomHabit}]] + [[#{greenCity.pages.table.isDeleted}]] [[#{greenCity.pages.table.actions}]] @@ -242,6 +244,15 @@

[[#{greenCity.habit.page.h}]]

[[${translation.description}]]
+ + + + + @@ -853,6 +864,53 @@ }); }); + diff --git a/core/src/test/java/greencity/webcontroller/ManagementHabitControllerTest.java b/core/src/test/java/greencity/webcontroller/ManagementHabitControllerTest.java index 9424b93f49..f2fb426816 100644 --- a/core/src/test/java/greencity/webcontroller/ManagementHabitControllerTest.java +++ b/core/src/test/java/greencity/webcontroller/ManagementHabitControllerTest.java @@ -175,9 +175,13 @@ void deleteAll() throws Exception { @Test void switchIsDeletedStatusTest() throws Exception { Long habitId = 1L; - this.mockMvc.perform(MockMvcRequestBuilders.patch(habitManagementLink + "/switch-deleted-status/" + habitId)) + Boolean newStatus = true; + + this.mockMvc.perform(MockMvcRequestBuilders.patch(habitManagementLink + "/switch-deleted-status/" + habitId) + .contentType(MediaType.APPLICATION_JSON) + .content(newStatus.toString())) .andExpect(status().isOk()); - verify(managementHabitService).switchIsDeletedStatus(habitId); + verify(managementHabitService).switchIsDeletedStatus(habitId, newStatus); } } diff --git a/service-api/src/main/java/greencity/dto/habit/HabitManagementDto.java b/service-api/src/main/java/greencity/dto/habit/HabitManagementDto.java index be11d1f377..5780f40998 100644 --- a/service-api/src/main/java/greencity/dto/habit/HabitManagementDto.java +++ b/service-api/src/main/java/greencity/dto/habit/HabitManagementDto.java @@ -29,4 +29,6 @@ public class HabitManagementDto implements Serializable { @Min(value = 7, message = ServiceValidationConstants.HABIT_DEFAULT_DURATION) @Max(value = 56, message = ServiceValidationConstants.HABIT_DEFAULT_DURATION) private Integer defaultDuration; + private Boolean isCustomHabit; + private Boolean isDeleted; } diff --git a/service-api/src/main/java/greencity/service/ManagementHabitService.java b/service-api/src/main/java/greencity/service/ManagementHabitService.java index 7322fe4c5c..f5677a98ff 100644 --- a/service-api/src/main/java/greencity/service/ManagementHabitService.java +++ b/service-api/src/main/java/greencity/service/ManagementHabitService.java @@ -60,12 +60,12 @@ PageableHabitManagementDto getAllHabitsDto(String searchReg, void deleteAll(List listId); /** - * Method switches the `isDeleted` status of a {@code Habit} instance. + * Updates the `isDeleted` status of a {@code Habit}. If `isDeleted` is + * {@code null}, it is set to the provided {@code newStatus}. Otherwise, it is + * updated directly to {@code newStatus}. * - * @param id {@code Habit} id. - If the current value of `isDeleted` is - * {@code false}, it will be updated to {@code true}. - If the current - * value of `isDeleted` is {@code true}, it will be updated to - * {@code false}. + * @param id the ID of the {@code Habit} to update. + * @param newStatus the new `isDeleted` status, {@code true} or {@code false}. */ - void switchIsDeletedStatus(Long id); + void switchIsDeletedStatus(Long id, Boolean newStatus); } diff --git a/service/src/main/java/greencity/mapping/HabitManagementDtoMapper.java b/service/src/main/java/greencity/mapping/HabitManagementDtoMapper.java index 15ed953b18..d166c845bc 100644 --- a/service/src/main/java/greencity/mapping/HabitManagementDtoMapper.java +++ b/service/src/main/java/greencity/mapping/HabitManagementDtoMapper.java @@ -26,6 +26,8 @@ protected HabitManagementDto convert(Habit habit) { .image(habit.getImage()) .complexity(habit.getComplexity()) .defaultDuration(habit.getDefaultDuration()) + .isCustomHabit(habit.getIsCustomHabit()) + .isDeleted(habit.getIsDeleted()) .habitTranslations(habit.getHabitTranslations() .stream().map(habitTranslation -> HabitTranslationManagementDto.builder() .id(habitTranslation.getId()) diff --git a/service/src/main/java/greencity/service/ManagementHabitServiceImpl.java b/service/src/main/java/greencity/service/ManagementHabitServiceImpl.java index 6e6544eca8..a232e25582 100644 --- a/service/src/main/java/greencity/service/ManagementHabitServiceImpl.java +++ b/service/src/main/java/greencity/service/ManagementHabitServiceImpl.java @@ -219,16 +219,14 @@ public void deleteAll(List listId) { } /** - * Method toggles the status of a Habit from "isDeleted" to true or false. - * - * @param id {@link HabitManagementDto}'s id. + * {@inheritDoc} */ @Override @Transactional - public void switchIsDeletedStatus(Long id) { + public void switchIsDeletedStatus(Long id, Boolean newStatus) { Habit habit = habitRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.HABIT_NOT_FOUND_BY_ID + id)); - habit.setIsDeleted(!habit.getIsDeleted()); + habit.setIsDeleted(newStatus); habitRepo.save(habit); } diff --git a/service/src/test/java/greencity/service/ManagementHabitServiceImplTest.java b/service/src/test/java/greencity/service/ManagementHabitServiceImplTest.java index 06feec24ee..39f223bc28 100644 --- a/service/src/test/java/greencity/service/ManagementHabitServiceImplTest.java +++ b/service/src/test/java/greencity/service/ManagementHabitServiceImplTest.java @@ -280,10 +280,11 @@ void saveHabitAndTranslationsWhenImageEmptyTest() { void switchIsDeletedStatusTest() { Habit habit = getHabit(); habit.setIsDeleted(false); + Boolean newStatus = true; when(habitRepo.findById(anyLong())).thenReturn(Optional.of(habit)); - managementHabitService.switchIsDeletedStatus(1L); + managementHabitService.switchIsDeletedStatus(1L, newStatus); assertTrue(habit.getIsDeleted()); verify(habitRepo).save(habit); @@ -292,23 +293,25 @@ void switchIsDeletedStatusTest() { @Test void switchIsDeletedStatusToFalseTest() { Habit habit = getHabit(); + Boolean newStatus = false; habit.setIsDeleted(true); when(habitRepo.findById(anyLong())).thenReturn(Optional.of(habit)); - managementHabitService.switchIsDeletedStatus(1L); + managementHabitService.switchIsDeletedStatus(1L, newStatus); assertFalse(habit.getIsDeleted()); verify(habitRepo).save(habit); } @Test - void switchIsDeletedStatusTestWhenHabitIsNotFoundTest() { + void switchIsDeletedStatusWhenHabitIsNotFoundTest() { Long habitId = 1L; + Boolean newStatus = false; when(habitRepo.findById(habitId)).thenReturn(Optional.empty()); - assertThrows(NotFoundException.class, () -> managementHabitService.switchIsDeletedStatus(habitId)); + assertThrows(NotFoundException.class, () -> managementHabitService.switchIsDeletedStatus(habitId, newStatus)); verify(habitRepo).findById(habitId); } }