Skip to content

Commit

Permalink
Update DTOs, objects replaced by ID, UserMeDTO merged into UserDTO, a…
Browse files Browse the repository at this point in the history
…dd author relationship in Message
  • Loading branch information
krabiworld committed Dec 4, 2024
1 parent fd4b4fb commit 240b78c
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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> message = List.of(messagesService.editMessage(id, channel, member, body));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ public ChannelDTO(Channel channel) {
this.ownerId = channel.getOwnerId();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> attachments;

public MessageDTO(String id, String content, String authorId, String channelId, List<String> attachments) {
this.id = id;
this.content = content;
this.authorId = authorId;
this.channelId = channelId;
this.attachments = attachments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
@Getter
@Setter
public class MessagesDTO {
private List<Message> messages;
private List<MessageDTO> messages;

public MessagesDTO(List<Message> 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()));
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class Channel {
private List<Message> messages;

public Channel() {

}

public Channel(String id, String name, int type, String ownerId) {
Expand Down
3 changes: 1 addition & 2 deletions foxogram-common/src/main/java/su/foxogram/models/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Code {
@Column()
public String type;

@Column(unique = true)
@Column()
public String value;

@Column()
Expand All @@ -28,7 +28,6 @@ public class Code {
public long expiresAt;

public Code() {

}

public Code(String userId, String type, String value, long issuedAt, long expiresAt) {
Expand Down
4 changes: 4 additions & 0 deletions foxogram-common/src/main/java/su/foxogram/models/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions foxogram-common/src/main/java/su/foxogram/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> attachments) {
this.id = id;
this.channel = channel;
this.authorId = authorId;
this.author = new Member(authorId);
this.content = content;
this.timestamp = timestamp;
this.attachments = attachments;
}

}
4 changes: 2 additions & 2 deletions foxogram-common/src/main/java/su/foxogram/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class User {
@Column()
public String displayName;

@Column(unique = true)
@Column()
public String username;

@Column()
Expand All @@ -32,7 +32,7 @@ public class User {
@Column()
public int type;

@Column(unique = true)
@Column()
private String email;

@Column()
Expand Down

0 comments on commit 240b78c

Please sign in to comment.