Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat 232 user nickname #77

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class MessageResponseDto {
private Integer id;
private LocalDateTime timestamp;
private String sentFrom;
private String sentFromNickname;
private String sendTo;
private String sendToNickname;
private String content;

}
5 changes: 5 additions & 0 deletions src/main/java/com/chat/yourway/mapper/MessageMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import com.chat.yourway.model.Message;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface MessageMapper {

@Mapping(source = "sender.nickname", target = "sentFromNickname")
@Mapping(source = "receiver.nickname", target = "sendToNickname",
defaultExpression = "java(message.getReceiver() != null ? message.getReceiver().getNickname() : null )"
)
MessageResponseDto toResponseDto(Message message);

List<MessageResponseDto> toListResponseDto(List<Message> messages);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/chat/yourway/model/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ public class Contact implements UserDetails {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contact_seq_gen")
@SequenceGenerator(name = "contact_seq_gen", sequenceName = "chat.contact_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "nickname", nullable = false, unique = true)
private String nickname;

@Column(name = "avatar_id", nullable = false)
private Byte avatarId;

@Column(name = "email", nullable = false, unique = true)
private String email;

@Column(name = "password", nullable = false, length = 2048)
private String password;

@Column(name = "is_active", nullable = false)
private Boolean isActive;

@Column(name = "is_private", nullable = false)
private Boolean isPrivate;

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/chat/yourway/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,36 @@
@Entity
@Table(schema = "chat", name = "message")
public class Message {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "message_seq_gen")
@SequenceGenerator(name = "message_seq_gen", sequenceName = "chat.message_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "sent_from", nullable = false)
private String sentFrom;

@Column(name = "send_to")
private String sendTo;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "sender_id")
private Contact sender;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "receiver_id")
private Contact receiver;

@Column(name = "content", nullable = false)
private String content;

@Column(name = "timestamp", nullable = false)
private LocalDateTime timestamp;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "topic_id", referencedColumnName = "id", nullable = false)
private Topic topic;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
schema = "chat",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.chat.yourway.mapper.TopicMapper;
import com.chat.yourway.model.Message;
import com.chat.yourway.repository.MessageRepository;
import com.chat.yourway.service.interfaces.ContactService;
import com.chat.yourway.service.interfaces.MessageService;
import com.chat.yourway.service.interfaces.TopicService;
import com.chat.yourway.service.interfaces.TopicSubscriberService;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class MessageServiceImpl implements MessageService {
private final MessageRepository messageRepository;
private final MessageMapper messageMapper;
private final TopicService topicService;
private final ContactService contactService;
private final TopicMapper topicMapper;
private final TopicSubscriberService topicSubscriberService;

Expand All @@ -55,6 +57,7 @@ public MessageResponseDto createPublic(int topicId, MessagePublicRequestDto mess
Message savedMessage = messageRepository.save(Message.builder()
.sentFrom(email)
.sendTo("Topic id=" + topic.getId())
.sender(contactService.findByEmail(email))
.content(message.getContent())
.timestamp(LocalDateTime.now())
.topic(topicMapper.toEntity(topic))
Expand All @@ -81,6 +84,8 @@ public MessageResponseDto createPrivate(MessagePrivateRequestDto message, String
Message savedMessage = messageRepository.save(Message.builder()
.sentFrom(email)
.sendTo(message.getSendTo())
.sender(contactService.findByEmail(email))
.receiver(contactService.findByEmail(message.getSendTo()))
.content(message.getContent())
.timestamp(LocalDateTime.now())
.topic(topicMapper.toEntity(topic))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ALTER TABLE chat.message
ADD sender_id int4,
ALTER COLUMN sender_id SET DEFAULT NULL,
ADD CONSTRAINT sender_fk_contact FOREIGN KEY (sender_id) REFERENCES chat.contact (id);
ALTER TABLE chat.message
ADD receiver_id int4,
ALTER COLUMN receiver_id SET DEFAULT NULL,
ADD CONSTRAINT receiver_fk_contact FOREIGN KEY (receiver_id) REFERENCES chat.contact (id);

UPDATE chat.message m
SET sender_id = (SELECT ID FROM chat.contact c WHERE c.email = m.sent_from),
receiver_id = (SELECT ID FROM chat.contact c WHERE c.email = m.send_to);
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import com.chat.yourway.exception.TopicSubscriberNotFoundException;
import com.chat.yourway.mapper.MessageMapperImpl;
import com.chat.yourway.mapper.TopicMapperImpl;
import com.chat.yourway.model.Contact;
import com.chat.yourway.model.Message;
import com.chat.yourway.model.Topic;
import com.chat.yourway.repository.MessageRepository;
import com.chat.yourway.service.MessageServiceImpl;
import com.chat.yourway.service.interfaces.ContactService;
import com.chat.yourway.service.interfaces.TopicService;
import com.chat.yourway.service.interfaces.TopicSubscriberService;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -57,6 +59,8 @@ public class MessageServiceImplTest {
@Mock
TopicService topicService;
@Mock
ContactService contactService;
@Mock
TopicSubscriberService topicSubscriberService;
@InjectMocks
MessageServiceImpl messageService;
Expand Down Expand Up @@ -159,6 +163,7 @@ public void createPublic_shouldCreatePublicMessage() {
when(topicService.findById(topicId)).thenReturn(topicResponseDto);
when(messageRepository.save(any(Message.class))).thenReturn(message);
when(topicSubscriberService.hasContactSubscribedToTopic(sentFrom, topicId)).thenReturn(true);
when(contactService.findByEmail(anyString())).thenReturn(null);

// When
MessageResponseDto messageDto = messageService.createPublic(topicId, messageRequest, sentFrom);
Expand Down Expand Up @@ -218,6 +223,7 @@ public void createPrivate_shouldCreatePrivateMessage() {
when(topicService.generatePrivateName(sendTo, sentFrom)).thenReturn(topicName);
when(topicService.findByName(topicName)).thenReturn(topicResponseDto);
when(messageRepository.save(any(Message.class))).thenReturn(message);
when(contactService.findByEmail(anyString())).thenReturn(null);

// When
MessageResponseDto messageDto = messageService.createPrivate(messageRequest, sentFrom);
Expand Down
Loading