Skip to content

Commit

Permalink
Merge pull request #86 from MrSebastian/delete-person
Browse files Browse the repository at this point in the history
Delete person
  • Loading branch information
MrSebastian authored Mar 17, 2024
2 parents 1b178ae + c4a635a commit 8044bdf
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface PersonDataAccessService {

PersonDao createPerson(PersonCreateDao personCreateDao);

void deletePerson(UUID id);

boolean personExists(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public PersonDao createPerson(PersonCreateDao personCreateDao) {
return personDaoMapper.toDao(repository.save(personToCreate));
}

@Override
public void deletePerson(UUID id) {
repository.deleteById(id);
}

@Override
public boolean personExists(UUID id) {
return repository.existsById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public PersonDao createPerson(final PersonCreateDao personCreateDao) {
return sqlDaoMapper.toDao(personRepository.save(entityToSave));
}

@Override
public void deletePerson(UUID id) {
personRepository.deleteById(id);
}

@Override
public boolean personExists(UUID id) {
return personRepository.existsById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import de.mrsebastian.todoappdemo.backend.person.service.PersonService;
import jakarta.validation.Valid;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -28,4 +31,10 @@ public Iterable<PersonDTO> getPersonen() {
public PersonDTO createPerson(@Valid @RequestBody PersonCreateDTO personCreateDTO) {
return personService.createPerson(personCreateDTO);
}

@DeleteMapping("{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deletePerson(@PathVariable("id") UUID personId) {
personService.deletePerson(personId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import de.mrsebastian.todoappdemo.backend.person.dataaccess.entity.Person;
import de.mrsebastian.todoappdemo.backend.person.rest.PersonCreateDTO;
import de.mrsebastian.todoappdemo.backend.person.rest.PersonDTO;
import de.mrsebastian.todoappdemo.backend.person.service.events.PersonDeleteEvent;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -19,6 +21,8 @@ public class PersonService {

private final PersonDataAccessService personDAService;

private final ApplicationEventPublisher applicationEventPublisher;

public List<PersonDTO> getPersonen() {
return personDAService.getPersonen().stream().map(personMapper::toDto).toList();
}
Expand All @@ -28,6 +32,12 @@ public PersonDTO createPerson(final PersonCreateDTO personCreateDTO) {
return personMapper.toDto(personDAService.createPerson(personDaoForCreate));
}

public void deletePerson(final UUID personId) {
existsOrThrow(personId);
personDAService.deletePerson(personId);
applicationEventPublisher.publishEvent(new PersonDeleteEvent(personId));
}

public void existsOrThrow(final UUID personId) {
if (!personDAService.personExists(personId)) {
throw new NotFoundException(personId, Person.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.mrsebastian.todoappdemo.backend.person.service.events;

import jakarta.validation.constraints.NotNull;
import java.util.UUID;

public record PersonDeleteEvent(@NotNull UUID personId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void updateTaskAssignee(final UUID taskId, @Nullable final TaskAssigneeDT
}
}

@PreAuthorize("hasAnyRole(T(de.mrsebastian.todoappdemo.backend.security.AuthoritiesEnum).TASK_ADMIN.name())")
public void removePersonFromTasks(final UUID personId) {
getTaskWithAssignee(personId).forEach(task -> taskDAService.removeAssignee(task.id()));
}

private void checkIfExistsOrThrow(final UUID taskId) {
if (!taskDAService.taskExsits(taskId)) {
throw new NotFoundException(taskId, Task.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.mrsebastian.todoappdemo.backend.task.service.events;

import de.mrsebastian.todoappdemo.backend.person.service.events.PersonDeleteEvent;
import de.mrsebastian.todoappdemo.backend.task.service.TaskService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class PersonEventsListener {

private final TaskService taskService;

@EventListener
public void handlePersonDeletedEvent(final PersonDeleteEvent personDeleteEvent) {
taskService.removePersonFromTasks(personDeleteEvent.personId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
v-for="(person, index) in props.persons"
:key="index"
:person="person"
@delete="handleDeleteRequest"
/>
</v-list>
</template>
Expand All @@ -18,6 +19,13 @@ interface IProps {
}
const props = defineProps<IProps>();
const emit = defineEmits<{
(e: "delete", value: string): void;
}>();
function handleDeleteRequest(personIdToDelete: string) {
emit("delete", personIdToDelete);
}
</script>

<style scoped></style>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
:subtitle="props.person.id"
>
{{ props.person.email }}

<template #append>
<div>
<base-delete-icon-button @click="handleDeleteClicked" />
</div>
</template>
</v-list-item>
</template>

Expand All @@ -12,15 +18,24 @@ import type PersonPersisted from "@/features/person/types/PersonPersisted";
import { computed } from "vue";
import BaseDeleteIconButton from "@/components/common/buttons/icon/BaseDeleteIconButton.vue";
interface IProps {
person: PersonPersisted;
}
const props = defineProps<IProps>();
const emit = defineEmits<{
(e: "delete", value: string): void;
}>();
const fullname = computed(
() => `${props.person.firstname ?? ""} ${props.person.lastname ?? ""}`
);
function handleDeleteClicked() {
emit("delete", props.person.id);
}
</script>

<style scoped></style>
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export default class PersonService {
)
);
}

deletePerson(personId: string): Promise<void> {
return this.personClient.deletePerson(personId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import type PersonDTO from "@/features/person/services/api/model/PersonDTO";

export interface PersonClientInterface {
getPersonen(): Promise<PersonDTO[]>;

createPerson(person: PersonCreateDTO): Promise<PersonDTO>;

deletePerson(id: string): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export default class PersonClientLocalStorage implements PersonClientInterface {
);
}

deletePerson(id: string): Promise<void> {
const personsOfLocalStorage = this.getOrCreatePersonsOfLocalStorage();
const indexOfPersonToDelete = personsOfLocalStorage.findIndex(
(person) => person.id === id
);
if (indexOfPersonToDelete !== -1) {
personsOfLocalStorage.splice(indexOfPersonToDelete);
this.saveAllPersons(personsOfLocalStorage);
}

return Promise.resolve();
}

private getOrCreatePersonsOfLocalStorage(): Array<Person> {
let localStoragePersonsAsString = localStorage.getItem(
this.KEY_PERSON_ARRAY
Expand All @@ -46,9 +59,10 @@ export default class PersonClientLocalStorage implements PersonClientInterface {
private savePerson(person: Person): void {
const localPersons = this.getOrCreatePersonsOfLocalStorage();
localPersons.push(person);
localStorage.setItem(
this.KEY_PERSON_ARRAY,
JSON.stringify(localPersons)
);
this.saveAllPersons(localPersons);
}

private saveAllPersons(persons: Person[]): void {
localStorage.setItem(this.KEY_PERSON_ARRAY, JSON.stringify(persons));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class PersonRestClient implements PersonClientInterface {
private readonly PATH_PERSON = "/person";

private readonly fetchUtils = new FetchUtils();

createPerson(person: PersonCreateDTO): Promise<PersonDTO> {
return this.fetchUtils.doFetch<PersonDTO>(
`${API_BACKEND_BASE}${this.PATH_PERSON}`,
Expand All @@ -22,4 +23,11 @@ export default class PersonRestClient implements PersonClientInterface {
FetchUtils.getGETConfig()
);
}

deletePerson(id: string): Promise<void> {
return this.fetchUtils.doFetch<void>(
`${API_BACKEND_BASE}${this.PATH_PERSON}/${id}`,
FetchUtils.getDeletConfig()
);
}
}
16 changes: 12 additions & 4 deletions frontend/frontend/src/views/PersonView.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<v-container>
<v-row class="text-center">
<v-col cols="12"
><base-person-add-button @click="handlePersonAddClicked" />
<v-col cols="12">
<base-person-add-button @click="handlePersonAddClicked" />
</v-col>
</v-row>

Expand All @@ -13,11 +13,15 @@
icon
variant="flat"
@click="handleReloadClicked"
><v-icon>mdi-reload</v-icon></v-btn
>
<v-icon>mdi-reload</v-icon>
</v-btn>
</div>
<div>
<base-person-list :persons="persons" />
<base-person-list
:persons="persons"
@delete="handlePersonDeleteRequest"
/>
</div>
</v-col>
</v-row>
Expand Down Expand Up @@ -50,6 +54,10 @@ function handlePersonAdded() {
loadPersons();
}
function handlePersonDeleteRequest(personIdForDeletion: string): void {
personService.deletePerson(personIdForDeletion).then(() => loadPersons());
}
function handlePersonAddClicked() {
showPersonCreateDialog();
}
Expand Down

0 comments on commit 8044bdf

Please sign in to comment.