delete edit users

This commit is contained in:
Stepan Usatiuk
2023-12-27 15:55:21 +01:00
parent 96dd1065da
commit 335f761eac
7 changed files with 147 additions and 10 deletions

View File

@@ -3,13 +3,17 @@ package com.usatiuk.tjv.y.server.controller;
import com.usatiuk.tjv.y.server.dto.PersonSignupTo;
import com.usatiuk.tjv.y.server.dto.PersonTo;
import com.usatiuk.tjv.y.server.dto.converters.PersonMapper;
import com.usatiuk.tjv.y.server.entity.Chat;
import com.usatiuk.tjv.y.server.entity.Person;
import com.usatiuk.tjv.y.server.service.ChatService;
import com.usatiuk.tjv.y.server.service.PersonService;
import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException;
import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.security.Principal;
import java.util.Optional;
@@ -20,11 +24,15 @@ import java.util.stream.StreamSupport;
@RequestMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE)
public class PersonController {
private final PersonService personService;
private final ChatService chatService;
private final PersonMapper personMapper;
private final PasswordEncoder passwordEncoder;
public PersonController(PersonService personService, PersonMapper personMapper) {
public PersonController(PersonService personService, ChatService chatService, PersonMapper personMapper, PasswordEncoder passwordEncoder) {
this.personService = personService;
this.chatService = chatService;
this.personMapper = personMapper;
this.passwordEncoder = passwordEncoder;
}
@PostMapping
@@ -67,6 +75,27 @@ public class PersonController {
return personMapper.makeDto(found.get());
}
@PatchMapping(path = "/self")
public PersonTo update(Principal principal, @RequestBody PersonSignupTo personSignupTo) {
var person = personService.readById(principal.getName()).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
person.setUsername(personSignupTo.username())
.setFullName(personSignupTo.fullName());
if (!personSignupTo.password().isEmpty()) person.setPassword(passwordEncoder.encode(personSignupTo.password()));
personService.update(person);
return personMapper.makeDto(person);
}
@DeleteMapping(path = "/self")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(Principal principal) {
var person = personService.readById(principal.getName()).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
for (Chat c : person.getChats()) {
c.getMembers().remove(person);
chatService.update(c);
}
personService.deleteById(principal.getName());
}
@GetMapping
public Stream<PersonTo> getAll() throws UserNotFoundException {
return StreamSupport.stream(personService.readAll().spliterator(), false).map(personMapper::makeDto);

View File

@@ -25,7 +25,7 @@ public class Chat implements EntityWithId<Long> {
@NotBlank
private String name;
@OneToMany(mappedBy = "chat")
@OneToMany(mappedBy = "chat", orphanRemoval = true)
private Collection<Message> messages = new ArrayList<>();
@ManyToMany

View File

@@ -34,13 +34,13 @@ public class Person implements EntityWithId<String> {
@NotBlank(message = "Password can't be empty")
private String password;
@OneToMany(mappedBy = "author")
@OneToMany(mappedBy = "author", orphanRemoval = true)
private Collection<Post> posts = new ArrayList<>();
@OneToMany(mappedBy = "creator")
@OneToMany(mappedBy = "creator", orphanRemoval = true)
private Collection<Chat> createdChats = new ArrayList<>();
@OneToMany(mappedBy = "author")
@OneToMany(mappedBy = "author", orphanRemoval = true)
private Collection<Message> messages = new ArrayList<>();
@ManyToMany