From 042da8137c9e9756a0af90b1249627bea4b4ff81 Mon Sep 17 00:00:00 2001 From: milenasrb Date: Sat, 23 Nov 2024 15:26:50 +0100 Subject: [PATCH] Fix repository bug --- .../chat/message/MessageService.java | 12 +++++++----- .../www1/hephaestus/chat/session/Session.java | 4 ++-- .../chat/session/SessionService.java | 19 ++++++++----------- 3 files changed, 17 insertions(+), 18 deletions(-) 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 9540d8a3..0739bed0 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 @@ -38,13 +38,15 @@ public MessageDTO sendMessage(String content, Long sessionId) { Message userMessage = new Message(OffsetDateTime.now(), MessageSender.USER, content, session); messageRepository.saveAndFlush(userMessage); - String systemResponse = generateResponse(sessionId, content); + // String systemResponse = generateResponse(sessionId, content); - Message systemMessage = new Message(OffsetDateTime.now(), MessageSender.SYSTEM, systemResponse, session); - messageRepository.saveAndFlush(systemMessage); + // Message systemMessage = new Message(OffsetDateTime.now(), MessageSender.SYSTEM, systemResponse, session); + // messageRepository.saveAndFlush(systemMessage); - return new MessageDTO(systemMessage.getId(), systemMessage.getSentAt(), systemMessage.getSender(), - systemMessage.getContent(), systemMessage.getSession().getId()); + // 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()); } 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 f5e46174..2900f52e 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 @@ -28,8 +28,8 @@ public class Session { private List messages = new ArrayList<>(); @NonNull - @Column(name = "created_at") - protected OffsetDateTime createdAt = OffsetDateTime.now(); + @Column(name = "created_at", nullable = false, updatable = false) + protected OffsetDateTime createdAt; @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 cebb20bd..e83ea3f2 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 @@ -4,20 +4,17 @@ import org.springframework.stereotype.Service; import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository; -import java.time.OffsetDateTime; import java.util.Optional; +import java.time.OffsetDateTime; import java.util.List; @Service public class SessionService { - - private final SessionRepository sessionRepository; + @Autowired + private SessionRepository sessionRepository; @Autowired private UserRepository userRepository; - public SessionService(SessionRepository sessionRepository) { - this.sessionRepository = sessionRepository; - } public List findAllSessionsByUser(String login) { Optional> sessions = sessionRepository.findByUserLogin(login); @@ -32,11 +29,11 @@ public Optional findSessionById(Long sessionId) { } public SessionDTO createSession(String login) { - Session session = new Session(); - var user = userRepository.findByLogin(login); - if (user.isPresent()){ - session.setUser(user.get()); - } + var user = userRepository.findByLogin(login) + .orElseThrow(() -> new IllegalArgumentException("User not found: " + login)); + + Session session = new Session(OffsetDateTime.now()); + session.setUser(user); return SessionDTO.fromSession(sessionRepository.saveAndFlush(session)); }