mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
proper chat service
This commit is contained in:
@@ -3,110 +3,52 @@ package com.usatiuk.tjv.y.server.controller;
|
|||||||
import com.usatiuk.tjv.y.server.dto.ChatCreateTo;
|
import com.usatiuk.tjv.y.server.dto.ChatCreateTo;
|
||||||
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
||||||
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
||||||
import com.usatiuk.tjv.y.server.dto.converters.ChatMapper;
|
|
||||||
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.ChatService;
|
||||||
import jakarta.persistence.EntityManager;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Collection;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/chat", produces = MediaType.APPLICATION_JSON_VALUE)
|
@RequestMapping(value = "/chat", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
public class ChatController {
|
public class ChatController {
|
||||||
private final EntityManager entityManager;
|
|
||||||
private final ChatService chatService;
|
private final ChatService chatService;
|
||||||
private final ChatMapper chatMapper;
|
|
||||||
private final PersonMapper personMapper;
|
|
||||||
|
|
||||||
public ChatController(EntityManager entityManager, ChatService chatService, ChatMapper chatMapper, PersonMapper personMapper) {
|
public ChatController(ChatService chatService) {
|
||||||
this.entityManager = entityManager;
|
|
||||||
this.chatService = chatService;
|
this.chatService = chatService;
|
||||||
this.chatMapper = chatMapper;
|
|
||||||
this.personMapper = personMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ChatTo create(Authentication authentication, @RequestBody ChatCreateTo chatCreateTo) {
|
public ChatTo create(Authentication authentication, @RequestBody ChatCreateTo chatCreateTo) {
|
||||||
var chat = new Chat();
|
return chatService.create(authentication, chatCreateTo);
|
||||||
|
|
||||||
if (Arrays.stream(chatCreateTo.memberUuids()).noneMatch(n -> Objects.equals(n, authentication.getName())))
|
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Creator of chat must be its member");
|
|
||||||
|
|
||||||
if (chatCreateTo.memberUuids().length <= 1)
|
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Chat must have members other than its creator");
|
|
||||||
|
|
||||||
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
|
|
||||||
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
|
|
||||||
p -> entityManager.getReference(Person.class, p)
|
|
||||||
).toList());
|
|
||||||
chat.setName(chatCreateTo.name());
|
|
||||||
|
|
||||||
chatService.create(chat);
|
|
||||||
return chatMapper.makeDto(chat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/by-id/{id}")
|
@GetMapping(path = "/by-id/{id}")
|
||||||
public ChatTo get(Authentication authentication, @PathVariable Long id) {
|
public ChatTo get(Authentication authentication, @PathVariable Long id) {
|
||||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
return chatService.getById(authentication, id);
|
||||||
var userRef = entityManager.getReference(Person.class, authentication.getName());
|
|
||||||
if (!chat.getMembers().contains(userRef))
|
|
||||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
|
||||||
return chatMapper.makeDto(chat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping(path = "/by-id/{id}")
|
@DeleteMapping(path = "/by-id/{id}")
|
||||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||||
public void delete(Authentication authentication, @PathVariable Long id) {
|
public void delete(Authentication authentication, @PathVariable Long id) {
|
||||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
chatService.deleteById(authentication, id);
|
||||||
if (!Objects.equals(chat.getCreator().getUuid(), authentication.getName()))
|
|
||||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't creator of the chat");
|
|
||||||
chatService.deleteById(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping(path = "/by-id/{id}")
|
@PatchMapping(path = "/by-id/{id}")
|
||||||
public ChatTo update(Authentication authentication, @PathVariable Long id, @RequestBody ChatCreateTo chatCreateTo) {
|
public ChatTo update(Authentication authentication, @PathVariable Long id, @RequestBody ChatCreateTo chatCreateTo) {
|
||||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
return chatService.update(authentication, id, chatCreateTo);
|
||||||
if (!Objects.equals(chat.getCreator().getUuid(), authentication.getName()))
|
|
||||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't creator of the chat");
|
|
||||||
|
|
||||||
if (Arrays.stream(chatCreateTo.memberUuids()).noneMatch(n -> Objects.equals(n, authentication.getName())))
|
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Creator of chat must be its member");
|
|
||||||
|
|
||||||
if (chatCreateTo.memberUuids().length <= 1)
|
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Chat must have members other than its creator");
|
|
||||||
|
|
||||||
chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map(
|
|
||||||
p -> entityManager.getReference(Person.class, p)
|
|
||||||
).toList()));
|
|
||||||
chat.setName(chatCreateTo.name());
|
|
||||||
|
|
||||||
chatService.update(chat);
|
|
||||||
return chatMapper.makeDto(chat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping(path = "/my")
|
@GetMapping(path = "/my")
|
||||||
public Stream<ChatTo> getMy(Authentication authentication) {
|
public Collection<ChatTo> getMy(Authentication authentication) {
|
||||||
return chatService.readByMember(authentication.getName()).stream().map(chatMapper::makeDto);
|
return chatService.getMy(authentication);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/by-id/{id}/members")
|
@GetMapping(path = "/by-id/{id}/members")
|
||||||
public Stream<PersonTo> getMembers(Authentication authentication, @PathVariable Long id) {
|
public Collection<PersonTo> getMembers(Authentication authentication, @PathVariable Long id) {
|
||||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
return chatService.getMembers(authentication, id);
|
||||||
var userRef = entityManager.getReference(Person.class, authentication.getName());
|
|
||||||
if (!chat.getMembers().contains(userRef))
|
|
||||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
|
||||||
return chat.getMembers().stream().map(personMapper::makeDto);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,21 @@
|
|||||||
package com.usatiuk.tjv.y.server.service;
|
package com.usatiuk.tjv.y.server.service;
|
||||||
|
|
||||||
|
import com.usatiuk.tjv.y.server.dto.ChatCreateTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
||||||
import com.usatiuk.tjv.y.server.entity.Chat;
|
import com.usatiuk.tjv.y.server.entity.Chat;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
public interface ChatService extends CrudService<Chat, Long> {
|
public interface ChatService extends CrudService<Chat, Long> {
|
||||||
Collection<Chat> readByMember(String memberUuid);
|
ChatTo create(Authentication authentication, ChatCreateTo chatCreateTo);
|
||||||
|
ChatTo update(Authentication authentication, Long id, ChatCreateTo chatCreateTo);
|
||||||
|
|
||||||
|
Collection<ChatTo> getMy(Authentication authentication);
|
||||||
|
|
||||||
|
ChatTo getById(Authentication authentication, Long id);
|
||||||
|
void deleteById(Authentication authentication, Long id);
|
||||||
|
|
||||||
|
Collection<PersonTo> getMembers(Authentication authentication, Long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,38 @@
|
|||||||
package com.usatiuk.tjv.y.server.service;
|
package com.usatiuk.tjv.y.server.service;
|
||||||
|
|
||||||
|
import com.usatiuk.tjv.y.server.dto.ChatCreateTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.ChatTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.PersonTo;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.converters.ChatMapper;
|
||||||
|
import com.usatiuk.tjv.y.server.dto.converters.PersonMapper;
|
||||||
import com.usatiuk.tjv.y.server.entity.Chat;
|
import com.usatiuk.tjv.y.server.entity.Chat;
|
||||||
|
import com.usatiuk.tjv.y.server.entity.Person;
|
||||||
import com.usatiuk.tjv.y.server.repository.ChatRepository;
|
import com.usatiuk.tjv.y.server.repository.ChatRepository;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ChatServiceImpl extends CrudServiceImpl<Chat, Long> implements ChatService {
|
public class ChatServiceImpl extends CrudServiceImpl<Chat, Long> implements ChatService {
|
||||||
|
|
||||||
private final ChatRepository chatRepository;
|
private final ChatRepository chatRepository;
|
||||||
|
private final ChatMapper chatMapper;
|
||||||
|
private final PersonMapper personMapper;
|
||||||
|
private final EntityManager entityManager;
|
||||||
|
|
||||||
public ChatServiceImpl(ChatRepository chatRepository) {
|
public ChatServiceImpl(ChatRepository chatRepository, ChatMapper chatMapper, PersonMapper personMapper, EntityManager entityManager) {
|
||||||
this.chatRepository = chatRepository;
|
this.chatRepository = chatRepository;
|
||||||
|
this.chatMapper = chatMapper;
|
||||||
|
this.personMapper = personMapper;
|
||||||
|
this.entityManager = entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -21,8 +40,77 @@ public class ChatServiceImpl extends CrudServiceImpl<Chat, Long> implements Chat
|
|||||||
return chatRepository;
|
return chatRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Chat> readByMember(String memberUuid) {
|
public ChatTo create(Authentication authentication, ChatCreateTo chatCreateTo) {
|
||||||
return chatRepository.findByMembers_uuid(memberUuid);
|
var chat = new Chat();
|
||||||
|
|
||||||
|
if (Arrays.stream(chatCreateTo.memberUuids()).noneMatch(n -> Objects.equals(n, authentication.getName())))
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Creator of chat must be its member");
|
||||||
|
|
||||||
|
if (chatCreateTo.memberUuids().length == 1)
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Chat must have members other than its creator");
|
||||||
|
|
||||||
|
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
|
||||||
|
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
|
||||||
|
p -> entityManager.getReference(Person.class, p)
|
||||||
|
).toList());
|
||||||
|
chat.setName(chatCreateTo.name());
|
||||||
|
|
||||||
|
chatRepository.save(chat);
|
||||||
|
return chatMapper.makeDto(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatTo update(Authentication authentication, Long id, ChatCreateTo chatCreateTo) {
|
||||||
|
var chat = readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||||
|
if (!Objects.equals(chat.getCreator().getUuid(), authentication.getName()))
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't creator of the chat");
|
||||||
|
|
||||||
|
if (Arrays.stream(chatCreateTo.memberUuids()).noneMatch(n -> Objects.equals(n, authentication.getName())))
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Creator of chat must be its member");
|
||||||
|
|
||||||
|
if (chatCreateTo.memberUuids().length <= 1)
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Chat must have members other than its creator");
|
||||||
|
|
||||||
|
chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map(
|
||||||
|
p -> entityManager.getReference(Person.class, p)
|
||||||
|
).toList()));
|
||||||
|
chat.setName(chatCreateTo.name());
|
||||||
|
|
||||||
|
chatRepository.save(chat);
|
||||||
|
return chatMapper.makeDto(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ChatTo> getMy(Authentication authentication) {
|
||||||
|
return chatRepository.findByMembers_uuid(authentication.getName()).stream().map(chatMapper::makeDto).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ChatTo getById(Authentication authentication, Long id) {
|
||||||
|
var chat = readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||||
|
var userRef = entityManager.getReference(Person.class, authentication.getName());
|
||||||
|
if (!chat.getMembers().contains(userRef))
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
||||||
|
|
||||||
|
return chatMapper.makeDto(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteById(Authentication authentication, Long id) {
|
||||||
|
var chat = readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||||
|
if (!Objects.equals(chat.getCreator().getUuid(), authentication.getName()))
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't creator of the chat");
|
||||||
|
deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<PersonTo> getMembers(Authentication authentication, Long id) {
|
||||||
|
var chat = readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||||
|
var userRef = entityManager.getReference(Person.class, authentication.getName());
|
||||||
|
if (!chat.getMembers().contains(userRef))
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
||||||
|
return chat.getMembers().stream().map(personMapper::makeDto).toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class ChatControllerTest extends DemoDataDbTest {
|
|||||||
|
|
||||||
Assertions.assertEquals("chatnew", toResponse.name());
|
Assertions.assertEquals("chatnew", toResponse.name());
|
||||||
Assertions.assertEquals(person1.getUuid(), toResponse.creatorUuid());
|
Assertions.assertEquals(person1.getUuid(), toResponse.creatorUuid());
|
||||||
Assertions.assertIterableEquals(Stream.of(person1, person2).map(personMapper::makeDto).toList(), toResponse.members());
|
Assertions.assertEquals(2, toResponse.memberCount());
|
||||||
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
|
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user