package org.example.demo.praktikum6;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Repository
public class LogEntryDAO {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void createLogEntry(String message) {
        LogEntry entry = new LogEntry();
        entry.date = new Date();
        entry.time = LocalDateTime.now();
        entry.message = message;
        em.persist(entry);
    }

    public List<LogEntry> getAllLogEntries() {
        return em.createQuery("SELECT l FROM LogEntry l", LogEntry.class).getResultList();
    }
}
