From 25941cb704d15ec9ed58481d4f0b783ba374d38c Mon Sep 17 00:00:00 2001 From: milenasrb Date: Sat, 23 Nov 2024 17:01:21 +0100 Subject: [PATCH] Fix database bugs --- .../www1/hephaestus/chat/message/Message.java | 4 +-- .../chat/message/MessageService.java | 30 +++++++++---------- .../www1/hephaestus/chat/session/Session.java | 10 +++---- .../chat/session/SessionService.java | 13 ++++---- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/Message.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/Message.java index cfab9ee6..b983d4d5 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/Message.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/Message.java @@ -18,11 +18,11 @@ public class Message { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) - protected Long id; + private Long id; @NonNull @Column(name = "sent_at") - private OffsetDateTime sentAt; + private OffsetDateTime sentAt = OffsetDateTime.now(); @NonNull @Enumerated(EnumType.STRING) diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/MessageService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/MessageService.java index 0739bed0..bb1635cd 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/MessageService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/message/MessageService.java @@ -1,8 +1,8 @@ package de.tum.in.www1.hephaestus.chat.message; -import java.time.OffsetDateTime; import java.util.List; - +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import de.tum.in.www1.hephaestus.chat.message.Message.MessageSender; import de.tum.in.www1.hephaestus.chat.session.Session; @@ -14,13 +14,13 @@ @Service public class MessageService { - private final SessionRepository sessionRepository; - private final MessageRepository messageRepository; - private final DefaultApi sessionApiClient; + private DefaultApi sessionApiClient; + @Autowired + private SessionRepository sessionRepository; + @Autowired + private MessageRepository messageRepository; - public MessageService(SessionRepository sessionRepository, MessageRepository messageRepository) { - this.sessionRepository = sessionRepository; - this.messageRepository = messageRepository; + public MessageService() { this.sessionApiClient = new DefaultApi(); } @@ -32,12 +32,13 @@ public List getMessagesBySessionId(Long sessionId) { } public MessageDTO sendMessage(String content, Long sessionId) { - Session session = sessionRepository.findById(sessionId) - .orElseThrow(() -> new IllegalArgumentException("Session not found")); - - Message userMessage = new Message(OffsetDateTime.now(), MessageSender.USER, content, session); - messageRepository.saveAndFlush(userMessage); + Optional session = sessionRepository.findById(sessionId); + if (session.isEmpty()) { + return null; + } + Message userMessage = new Message(MessageSender.USER, content, session.get()); + // String systemResponse = generateResponse(sessionId, content); // Message systemMessage = new Message(OffsetDateTime.now(), MessageSender.SYSTEM, systemResponse, session); @@ -45,8 +46,7 @@ public MessageDTO sendMessage(String content, Long sessionId) { // return new MessageDTO(systemMessage.getId(), systemMessage.getSentAt(), systemMessage.getSender(), // systemMessage.getContent(), systemMessage.getSession().getId()); - return new MessageDTO(userMessage.getId(), userMessage.getSentAt(), userMessage.getSender(), - userMessage.getContent(), userMessage.getSession().getId()); + return MessageDTO.fromMessage(messageRepository.save(userMessage)); } private String generateResponse(Long session_id, String messageContent) { diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/Session.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/Session.java index 2900f52e..7648b383 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/Session.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/Session.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; import lombok.Getter; -import lombok.RequiredArgsConstructor; +import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.springframework.lang.NonNull; @@ -17,19 +17,19 @@ @Getter @Setter @ToString(callSuper = true) -@RequiredArgsConstructor +@NoArgsConstructor public class Session { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - protected Long id; + private Long id; @OrderColumn(name = "message_order") @OneToMany(mappedBy = "session") private List messages = new ArrayList<>(); @NonNull - @Column(name = "created_at", nullable = false, updatable = false) - protected OffsetDateTime createdAt; + @Column(name = "created_at") + private OffsetDateTime createdAt = OffsetDateTime.now(); @ManyToOne @JoinColumn(name = "user_id") diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/SessionService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/SessionService.java index e83ea3f2..5f4bf8c4 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/SessionService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/chat/session/SessionService.java @@ -5,7 +5,6 @@ import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository; import java.util.Optional; -import java.time.OffsetDateTime; import java.util.List; @Service @@ -29,12 +28,14 @@ public Optional findSessionById(Long sessionId) { } public SessionDTO createSession(String login) { - var user = userRepository.findByLogin(login) - .orElseThrow(() -> new IllegalArgumentException("User not found: " + login)); + var user = userRepository.findByLogin(login); + if (user.isEmpty()) { + return null; + } - Session session = new Session(OffsetDateTime.now()); - session.setUser(user); + Session session = new Session(); + session.setUser(user.get()); - return SessionDTO.fromSession(sessionRepository.saveAndFlush(session)); + return SessionDTO.fromSession(sessionRepository.save(session)); } } \ No newline at end of file