mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
messages!
This commit is contained in:
@@ -37,6 +37,9 @@ public class ChatController {
|
||||
if (Arrays.stream(chatCreateTo.memberUuids()).noneMatch(n -> Objects.equals(n, principal.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, principal.getName()));
|
||||
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
|
||||
p -> entityManager.getReference(Person.class, p)
|
||||
|
||||
@@ -1,10 +1,55 @@
|
||||
package com.usatiuk.tjv.y.server.controller;
|
||||
|
||||
import com.usatiuk.tjv.y.server.dto.MessageCreateTo;
|
||||
import com.usatiuk.tjv.y.server.dto.MessageTo;
|
||||
import com.usatiuk.tjv.y.server.dto.converters.MessageMapper;
|
||||
import com.usatiuk.tjv.y.server.entity.Message;
|
||||
import com.usatiuk.tjv.y.server.entity.Person;
|
||||
import com.usatiuk.tjv.y.server.service.ChatService;
|
||||
import com.usatiuk.tjv.y.server.service.MessageService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/message", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public class MessageController {
|
||||
private final ChatService chatService;
|
||||
private final EntityManager entityManager;
|
||||
private final MessageMapper messageMapper;
|
||||
private final MessageService messageService;
|
||||
|
||||
public MessageController(ChatService chatService, EntityManager entityManager, MessageMapper messageMapper, MessageService messageService) {
|
||||
this.chatService = chatService;
|
||||
this.entityManager = entityManager;
|
||||
this.messageMapper = messageMapper;
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
@GetMapping(path = "/by-chat/{chatTd}")
|
||||
public Stream<MessageTo> get(Principal principal, @PathVariable Long chatTd) {
|
||||
var chat = chatService.readById(chatTd).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||
var userRef = entityManager.getReference(Person.class, principal.getName());
|
||||
if (!chat.getMembers().contains(userRef))
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
||||
|
||||
return chat.getMessages().stream().map(messageMapper::makeDto);
|
||||
}
|
||||
|
||||
@PostMapping(path = "/by-chat/{chatId}")
|
||||
public MessageTo post(Principal principal, @PathVariable Long chatId, @RequestBody MessageCreateTo messageCreateTo) {
|
||||
var chat = chatService.readById(chatId).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||
var userRef = entityManager.getReference(Person.class, principal.getName());
|
||||
if (!chat.getMembers().contains(userRef))
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't member of the chat");
|
||||
|
||||
Message message = new Message().setChat(chat).setAuthor(userRef).setContents(messageCreateTo.contents());
|
||||
messageService.create(message);
|
||||
return messageMapper.makeDto(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.usatiuk.tjv.y.server.dto;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public record ChatTo(Long id, String name, String creatorUuid, Collection<PersonTo> members,
|
||||
Collection<MessageTo> messages) {
|
||||
public record ChatTo(Long id, String name, String creatorUuid, Long memberCount) {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.usatiuk.tjv.y.server.dto;
|
||||
|
||||
public record MessageCreateTo(String contents) {
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
package com.usatiuk.tjv.y.server.dto;
|
||||
|
||||
public record MessageTo(Long id, Long chatId, String authorUuid, String contents) {
|
||||
public record MessageTo(Long id, Long chatId, String authorUuid, String authorUsername, String contents,
|
||||
Long createdAt) {
|
||||
}
|
||||
|
||||
@@ -6,18 +6,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ChatMapper {
|
||||
|
||||
private final PersonMapper personMapper;
|
||||
private final MessageMapper messageMapper;
|
||||
|
||||
public ChatMapper(PersonMapper personMapper, MessageMapper messageMapper) {
|
||||
this.personMapper = personMapper;
|
||||
this.messageMapper = messageMapper;
|
||||
}
|
||||
|
||||
public ChatTo makeDto(Chat chat) {
|
||||
return new ChatTo(chat.getId(), chat.getName(), chat.getCreator().getUuid(),
|
||||
chat.getMembers().stream().map(personMapper::makeDto).toList(),
|
||||
chat.getMessages().stream().map(messageMapper::makeDto).toList());
|
||||
return new ChatTo(chat.getId(), chat.getName(), chat.getCreator().getUuid(), (long) chat.getMembers().size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ import org.springframework.stereotype.Component;
|
||||
public class MessageMapper {
|
||||
public MessageTo makeDto(Message message) {
|
||||
return new MessageTo(message.getId(), message.getChat().getId(),
|
||||
message.getAuthor().getUuid(), message.getContents());
|
||||
message.getAuthor().getUuid(), message.getAuthor().getUsername(), message.getContents(), message.getCreatedAt().getEpochSecond());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@@ -21,10 +24,13 @@ public class Message implements EntityWithId<Long> {
|
||||
|
||||
@ManyToOne
|
||||
private Chat chat;
|
||||
|
||||
|
||||
@ManyToOne
|
||||
private Person author;
|
||||
|
||||
@CreationTimestamp
|
||||
private Instant createdAt;
|
||||
|
||||
@Lob
|
||||
@NotBlank
|
||||
private String contents;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
jwt.secret=JKLASJKLASJKLJHKLDFAHJKFDSHJKFJHKDSHJKFHJKSDFJHKSDJHKFJHKS98346783467899782345jkhgsdoigh938g
|
||||
logging.level.root=DEBUG
|
||||
logging.level.org.springframework.security=DEBUG
|
||||
logging.level.org.springframework.security=DEBUG
|
||||
spring.datasource.url=jdbc:h2:file:~/tjvserver.h2
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
Reference in New Issue
Block a user