diff --git a/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java new file mode 100644 index 0000000..14827f0 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java @@ -0,0 +1,58 @@ +package com.example.demo.schedule.infrastructure.persistence; + +import com.example.demo.schedule.domain.ScheduleRepository; +import jakarta.persistence.EntityManager; +import org.springframework.stereotype.Repository; + +import java.util.Optional; +import java.util.stream.Stream; + +@Repository +public class ScheduleJpaRepository implements ScheduleRepository { + + private final EntityManager em; + + public ScheduleJpaRepository(EntityManager em) { + this.em = em; + } + + @Override + public ScheduleEntity save(ScheduleEntity entity) { + if (entity.getEventId() == null) { + em.persist(entity); + return entity; + } else { + return em.merge(entity); + } + } + + + public Stream streamAllPublic() { + Stream entity = em.createQuery("SELECT s FROM ScheduleEntity s WHERE s.scheduleType = 'PUBLIC'", ScheduleEntity.class).getResultStream(); + return entity; + } + + + public Stream streamAllPrivate() { + Stream entities = em.createQuery("SELECT s FROM ScheduleEntity s WHERE s.scheduleType = 'PRIVATE'", ScheduleEntity.class) + .getResultStream(); + return entities; + } + + @Override + public Optional findById(Long eventId) { + Optional schedule = em.createQuery("SELECT s FROM ScheduleEntity s WHERE s.eventId = eventId", ScheduleEntity.class) + .setParameter("evnetId", eventId) + .getResultStream() + .findFirst(); + return schedule; + } + + @Override + public void deleteById(Long eventId) { + ScheduleEntity entity = em.find(ScheduleEntity.class, eventId); + if (entity != null) { + em.remove(entity); + } + } +}