Skip to content

Commit

Permalink
Fix database bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
milesha committed Nov 23, 2024
1 parent 042da81 commit 25941cb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

Expand All @@ -32,21 +32,21 @@ public List<MessageDTO> 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> 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);
// messageRepository.saveAndFlush(systemMessage);

// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Message> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,12 +28,14 @@ public Optional<SessionDTO> 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));
}
}

0 comments on commit 25941cb

Please sign in to comment.