Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
urio999 committed Dec 5, 2024
1 parent 5dff090 commit 37bf276
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ public ResponseEntity<List<Long>> deleteAll(@RequestBody List<Long> listId) {
@ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN)
})
@PatchMapping("/switch-deleted-status/{id}")
public ResponseEntity<Long> switchIsDeletedStatus(@PathVariable("id") Long id) {
managementHabitService.switchIsDeletedStatus(id);
public ResponseEntity<Long> switchIsDeletedStatus(@PathVariable("id") Long id, @RequestBody Boolean newStatus) {
managementHabitService.switchIsDeletedStatus(id, newStatus);
return ResponseEntity.status(HttpStatus.OK).body(id);
}
}
4 changes: 4 additions & 0 deletions core/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions core/src/main/resources/templates/core/management_user_habits.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ <h2 class="text-describe">[[#{greenCity.habit.page.h}]]</h2>
</th>
<th>[[#{greenCity.pages.table.description}]]
</th>
<th>[[#{greenCity.pages.table.isCustomHabit}]]</th>
<th>[[#{greenCity.pages.table.isDeleted}]]</th>
<th>
[[#{greenCity.pages.table.actions}]]
</th>
Expand Down Expand Up @@ -242,6 +244,15 @@ <h2 class="text-describe">[[#{greenCity.habit.page.h}]]</h2>
<td>
<div class="pr" style="width: 27em">[[${translation.description}]]</div>
</td>
<td class="habit_isCustomHabit" th:text="${habit.isCustomHabit}"></td>
<td class="habit_isDeleted">
<select th:id="'status-'+${habit.id}" th:onchange="'toggleStatus('+${habit.id}+');'" class="form-control">
<option th:if="${habit.isDeleted == null}" th:value="null" th:selected="true">Not Set</option>
<option th:value="true" th:selected="${habit.isDeleted == true}">True</option>
<option th:value="false" th:selected="${habit.isDeleted == false}">False</option>
</select>
</td>

<td>
<a href="#editHabitModal" class="restore edit eBtn" th:data-id="${habit.id}" data-toggle="modal">
<i class="material-icons" data-toggle="tooltip" th:title="#{greenCity.pages.edit}">&#xE254;</i>
Expand Down Expand Up @@ -853,6 +864,53 @@ <h4 class="modal-title">[[#{greenCity.pages.popup.delete.all.h}]]</h4>
});
});
</script>
<script th:inline="javascript">
function toggleStatus(habitId) {
const selectElement = document.getElementById('status-' + habitId);
const selectedValue = selectElement.value;

if (selectedValue === "null") {
alert('You must select a valid status (True or False)');
return;
}

const newStatus = selectedValue === 'true';

fetch(`/management/habits/switch-deleted-status/${habitId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newStatus)
})
.then(response => {
if (response.ok) {
updateDropdown(habitId, newStatus);
alert('Status updated successfully');
} else {
alert('Failed to update status');
}
})
.catch(error => {
console.error('Error updating status:', error);
alert('An error occurred while updating the status');
});
}

function updateDropdown(habitId, newStatus) {
const selectElement = document.getElementById('status-' + habitId);

selectElement.innerHTML = '';

if (newStatus) {
selectElement.innerHTML += `<option value="true" selected>True</option>`;
selectElement.innerHTML += `<option value="false">False</option>`;
} else {
selectElement.innerHTML += `<option value="false" selected>False</option>`;
selectElement.innerHTML += `<option value="true">True</option>`;
}
}
</script>
</body>

</html>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ PageableHabitManagementDto<HabitManagementDto> getAllHabitsDto(String searchReg,
void deleteAll(List<Long> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,14 @@ public void deleteAll(List<Long> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

0 comments on commit 37bf276

Please sign in to comment.