From 7b4940823bb50fc8b9679345bbe581fca6f2fd86 Mon Sep 17 00:00:00 2001 From: capDoYeonLee Date: Fri, 4 Oct 2024 17:39:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20scheduleRepo=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=EC=B2=B4=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistence/ScheduleJpaRepository.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 BE/error/src/main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java 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); + } + } +}