Skip to content

Commit

Permalink
Fix repository bug
Browse files Browse the repository at this point in the history
  • Loading branch information
milesha committed Nov 23, 2024
1 parent cb11c80 commit 042da81
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Session {
private List<Message> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionDTO> findAllSessionsByUser(String login) {
Optional<List<Session>> sessions = sessionRepository.findByUserLogin(login);
Expand All @@ -32,11 +29,11 @@ public Optional<SessionDTO> 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));
}
Expand Down

0 comments on commit 042da81

Please sign in to comment.