From 240b78cb32b6c6eb2919317a2d3812ece5331187 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 5 Dec 2024 00:05:44 +0300 Subject: [PATCH] Update DTOs, objects replaced by ID, UserMeDTO merged into UserDTO, add author relationship in Message --- .../controllers/MessagesController.java | 6 ++-- .../foxogram/controllers/UsersController.java | 9 +++-- .../su/foxogram/services/MessagesService.java | 10 +++--- ...{MessageDTO.java => MessageCreateDTO.java} | 2 +- .../su/foxogram/dtos/response/ChannelDTO.java | 1 - .../su/foxogram/dtos/response/MemberDTO.java | 16 ++------- .../su/foxogram/dtos/response/MessageDTO.java | 28 ++++++++++++++++ .../foxogram/dtos/response/MessagesDTO.java | 6 ++-- .../su/foxogram/dtos/response/UserDTO.java | 9 ++++- .../su/foxogram/dtos/response/UserMeDTO.java | 33 ------------------- .../main/java/su/foxogram/models/Channel.java | 1 - .../main/java/su/foxogram/models/Code.java | 3 +- .../main/java/su/foxogram/models/Member.java | 4 +++ .../main/java/su/foxogram/models/Message.java | 9 +++-- .../main/java/su/foxogram/models/User.java | 4 +-- 15 files changed, 66 insertions(+), 75 deletions(-) rename foxogram-common/src/main/java/su/foxogram/dtos/request/{MessageDTO.java => MessageCreateDTO.java} (95%) create mode 100644 foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java delete mode 100644 foxogram-common/src/main/java/su/foxogram/dtos/response/UserMeDTO.java diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java index f7b7b03..40da921 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/MessagesController.java @@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import su.foxogram.constants.APIConstants; -import su.foxogram.dtos.request.MessageDTO; +import su.foxogram.dtos.request.MessageCreateDTO; import su.foxogram.dtos.response.MessagesDTO; import su.foxogram.dtos.response.OkDTO; import su.foxogram.exceptions.MessageNotFoundException; @@ -50,7 +50,7 @@ public MessagesDTO getMessage(@RequestAttribute(value = "user") User user, @Requ } @PostMapping("/channel/{channelId}") - public OkDTO createMessage(@RequestAttribute(value = "user") User user, @RequestAttribute(value = "channel") Channel channel, @Valid @RequestBody MessageDTO body) { + public OkDTO createMessage(@RequestAttribute(value = "user") User user, @RequestAttribute(value = "channel") Channel channel, @Valid @RequestBody MessageCreateDTO body) { log.info("MESSAGE post to CHANNEL ({}) by USER ({}, {}) requested", channel.getId(), user.getId(), user.getEmail()); messagesService.addMessage(channel, user, body); @@ -68,7 +68,7 @@ public OkDTO deleteMessage(@RequestAttribute(value = "user") User user, @Request } @PatchMapping("/channel/{channelId}/{id}") - public MessagesDTO editMessage(@RequestAttribute(value = "user") User user, @RequestAttribute(value = "member") Member member, @RequestAttribute(value = "channel") Channel channel, @Valid @RequestBody MessageDTO body, @PathVariable String id, HttpServletRequest request) throws MessageNotFoundException, MissingPermissionsException { + public MessagesDTO editMessage(@RequestAttribute(value = "user") User user, @RequestAttribute(value = "member") Member member, @RequestAttribute(value = "channel") Channel channel, @Valid @RequestBody MessageCreateDTO body, @PathVariable String id, HttpServletRequest request) throws MessageNotFoundException, MissingPermissionsException { log.info("MESSAGE ({}) patch in CHANNEL ({}) by USER ({}, {}) requested", id, channel.getId(), user.getId(), user.getEmail()); List message = List.of(messagesService.editMessage(id, channel, member, body)); diff --git a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java index a93325d..b74b3f5 100644 --- a/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java +++ b/foxogram-api/src/main/java/su/foxogram/controllers/UsersController.java @@ -6,7 +6,6 @@ import su.foxogram.constants.APIConstants; import su.foxogram.dtos.request.UserEditDTO; import su.foxogram.dtos.response.UserDTO; -import su.foxogram.dtos.response.UserMeDTO; import su.foxogram.exceptions.UserCredentialsDuplicateException; import su.foxogram.exceptions.UserNotFoundException; import su.foxogram.models.User; @@ -24,21 +23,21 @@ public UsersController(UsersService usersService) { } @GetMapping("/@me") - public UserMeDTO getYourself(@RequestAttribute(value = "user") User authenticatedUser) { - return new UserMeDTO(authenticatedUser); + public UserDTO getYourself(@RequestAttribute(value = "user") User authenticatedUser) { + return new UserDTO(authenticatedUser, true); } @GetMapping("/{userKey}") public UserDTO getUser(@PathVariable String userKey) throws UserNotFoundException { User fetchedUser = usersService.getUser(userKey); - return new UserDTO(fetchedUser); + return new UserDTO(fetchedUser, false); } @PatchMapping("/@me") public UserDTO editUser(@RequestAttribute(value = "user") User authenticatedUser, @Valid @RequestBody UserEditDTO userEditRequest) throws UserCredentialsDuplicateException { authenticatedUser = usersService.editUser(authenticatedUser, userEditRequest); - return new UserDTO(authenticatedUser); + return new UserDTO(authenticatedUser, false); } } diff --git a/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java b/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java index 992bab8..5acbdf1 100644 --- a/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java +++ b/foxogram-api/src/main/java/su/foxogram/services/MessagesService.java @@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import su.foxogram.constants.MemberConstants; -import su.foxogram.dtos.request.MessageDTO; +import su.foxogram.dtos.request.MessageCreateDTO; import su.foxogram.exceptions.MessageNotFoundException; import su.foxogram.exceptions.MissingPermissionsException; import su.foxogram.models.Channel; @@ -48,7 +48,7 @@ public Message getMessage(String id, Channel channel) throws MessageNotFoundExce return message; } - public void addMessage(Channel channel, User user, MessageDTO body) { + public void addMessage(Channel channel, User user, MessageCreateDTO body) { String id = Snowflake.create(); String authorId = user.getId(); long timestamp = System.currentTimeMillis(); @@ -65,19 +65,19 @@ public void deleteMessage(String id, Member member, Channel channel) throws Mess Message message = messageRepository.findByChannelAndId(channel, id); if (message == null) throw new MessageNotFoundException(); - if (!Objects.equals(message.getAuthorId(), member.getId()) || member.hasAnyPermission(MemberConstants.Permissions.ADMIN, MemberConstants.Permissions.MANAGE_MESSAGES)) + if (!Objects.equals(message.getAuthor().getId(), member.getId()) || member.hasAnyPermission(MemberConstants.Permissions.ADMIN, MemberConstants.Permissions.MANAGE_MESSAGES)) throw new MissingPermissionsException(); messageRepository.delete(message); log.info("MESSAGE ({}) in CHANNEL ({}) deleted successfully", id, channel.getId()); } - public Message editMessage(String id, Channel channel, Member member, MessageDTO body) throws MessageNotFoundException, MissingPermissionsException { + public Message editMessage(String id, Channel channel, Member member, MessageCreateDTO body) throws MessageNotFoundException, MissingPermissionsException { Message message = messageRepository.findByChannelAndId(channel, id); String content = body.getContent(); if (message == null) throw new MessageNotFoundException(); - if (!Objects.equals(message.getAuthorId(), member.getId())) throw new MissingPermissionsException(); + if (!Objects.equals(message.getAuthor().getId(), member.getId())) throw new MissingPermissionsException(); message.setContent(content); messageRepository.save(message); diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/request/MessageDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/request/MessageCreateDTO.java similarity index 95% rename from foxogram-common/src/main/java/su/foxogram/dtos/request/MessageDTO.java rename to foxogram-common/src/main/java/su/foxogram/dtos/request/MessageCreateDTO.java index 2ec86b4..6a12af0 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/request/MessageDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/request/MessageCreateDTO.java @@ -10,7 +10,7 @@ @Setter @Getter -public class MessageDTO { +public class MessageCreateDTO { @NotNull(message = "Content" + ValidationConstants.Messages.MUST_NOT_BE_NULL) @Size(min = 1, max = ValidationConstants.Lengths.MESSAGE_CONTENT, message = ValidationConstants.Messages.MESSAGE_WRONG_LENGTH) private String content; diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java index 0419ddd..df47381 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/ChannelDTO.java @@ -22,4 +22,3 @@ public ChannelDTO(Channel channel) { this.ownerId = channel.getOwnerId(); } } - diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java index 36d459c..ea2bd3b 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MemberDTO.java @@ -3,31 +3,19 @@ import lombok.Getter; import lombok.Setter; import su.foxogram.models.Member; -import su.foxogram.models.User; @Getter @Setter public class MemberDTO { private String id; - private String avatar; - - private String username; - - private long flags; - - private long type; + private String channelId; private long permissions; public MemberDTO(Member member) { - User user = member.getUser(); - this.id = member.getId(); - this.avatar = user.getAvatar(); - this.username = user.getUsername(); - this.flags = user.getFlags(); - this.type = user.getType(); + this.channelId = member.getChannel().getId(); this.permissions = member.getPermissions(); } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java new file mode 100644 index 0000000..39f49f9 --- /dev/null +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessageDTO.java @@ -0,0 +1,28 @@ +package su.foxogram.dtos.response; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class MessageDTO { + private String id; + + private String content; + + private String authorId; + + private String channelId; + + private List attachments; + + public MessageDTO(String id, String content, String authorId, String channelId, List attachments) { + this.id = id; + this.content = content; + this.authorId = authorId; + this.channelId = channelId; + this.attachments = attachments; + } +} diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java index 8955f14..a076a40 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/MessagesDTO.java @@ -9,9 +9,11 @@ @Getter @Setter public class MessagesDTO { - private List messages; + private List messages; public MessagesDTO(List messages) { - this.messages = messages; + for (Message message : messages) { + this.messages.add(new MessageDTO(message.getId(), message.getContent(), message.getAuthor().getId(), message.getChannel().getId(), message.getAttachments())); + } } } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java index 056e85b..20aa1b9 100644 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java +++ b/foxogram-common/src/main/java/su/foxogram/dtos/response/UserDTO.java @@ -1,5 +1,6 @@ package su.foxogram.dtos.response; +import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Getter; import lombok.Setter; import su.foxogram.models.User; @@ -15,15 +16,21 @@ public class UserDTO { private String username; + @JsonInclude(JsonInclude.Include.NON_NULL) + private String email; + private long flags; private long type; - public UserDTO(User user) { + public UserDTO(User user, boolean includeEmail) { this.id = user.getId(); this.avatar = user.getAvatar(); this.displayName = user.getDisplayName(); this.username = user.getUsername(); + if (includeEmail) { + this.email = user.getEmail(); + } this.flags = user.getFlags(); this.type = user.getType(); } diff --git a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserMeDTO.java b/foxogram-common/src/main/java/su/foxogram/dtos/response/UserMeDTO.java deleted file mode 100644 index 70cd0ef..0000000 --- a/foxogram-common/src/main/java/su/foxogram/dtos/response/UserMeDTO.java +++ /dev/null @@ -1,33 +0,0 @@ -package su.foxogram.dtos.response; - -import lombok.Getter; -import lombok.Setter; -import su.foxogram.models.User; - -@Getter -@Setter -public class UserMeDTO { - private String id; - - private String avatar; - - private String displayName; - - private String username; - - private String email; - - private long flags; - - private long type; - - public UserMeDTO(User user) { - this.id = user.getId(); - this.avatar = user.getAvatar(); - this.displayName = user.getDisplayName(); - this.username = user.getUsername(); - this.email = user.getEmail(); - this.flags = user.getFlags(); - this.type = user.getType(); - } -} diff --git a/foxogram-common/src/main/java/su/foxogram/models/Channel.java b/foxogram-common/src/main/java/su/foxogram/models/Channel.java index 47df744..b5f7ead 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Channel.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Channel.java @@ -33,7 +33,6 @@ public class Channel { private List messages; public Channel() { - } public Channel(String id, String name, int type, String ownerId) { diff --git a/foxogram-common/src/main/java/su/foxogram/models/Code.java b/foxogram-common/src/main/java/su/foxogram/models/Code.java index 0bbd07f..f4825b0 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Code.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Code.java @@ -18,7 +18,7 @@ public class Code { @Column() public String type; - @Column(unique = true) + @Column() public String value; @Column() @@ -28,7 +28,6 @@ public class Code { public long expiresAt; public Code() { - } public Code(String userId, String type, String value, long issuedAt, long expiresAt) { diff --git a/foxogram-common/src/main/java/su/foxogram/models/Member.java b/foxogram-common/src/main/java/su/foxogram/models/Member.java index 6de4153..2fd24f8 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Member.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Member.java @@ -31,6 +31,10 @@ public Member() { } + public Member(String id) { + this.id = id; + } + public Member(User user, Channel channel, long permissions) { this.id = user.getId(); this.user = user; diff --git a/foxogram-common/src/main/java/su/foxogram/models/Message.java b/foxogram-common/src/main/java/su/foxogram/models/Message.java index 39ccb8e..6b922c7 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/Message.java +++ b/foxogram-common/src/main/java/su/foxogram/models/Message.java @@ -21,8 +21,9 @@ public class Message { @Column() public String content; - @Column() - public String authorId; + @OneToOne + @JoinColumn(name = "author", nullable = false) + public Member author; @Column() public long timestamp; @@ -37,16 +38,14 @@ public class Message { private Channel channel; public Message() { - } public Message(String id, Channel channel, String content, String authorId, long timestamp, List attachments) { this.id = id; this.channel = channel; - this.authorId = authorId; + this.author = new Member(authorId); this.content = content; this.timestamp = timestamp; this.attachments = attachments; } - } diff --git a/foxogram-common/src/main/java/su/foxogram/models/User.java b/foxogram-common/src/main/java/su/foxogram/models/User.java index 4f1d60a..00154c7 100644 --- a/foxogram-common/src/main/java/su/foxogram/models/User.java +++ b/foxogram-common/src/main/java/su/foxogram/models/User.java @@ -20,7 +20,7 @@ public class User { @Column() public String displayName; - @Column(unique = true) + @Column() public String username; @Column() @@ -32,7 +32,7 @@ public class User { @Column() public int type; - @Column(unique = true) + @Column() private String email; @Column()