-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf192b7
commit 7b49408
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...main/java/com/example/demo/schedule/infrastructure/persistence/ScheduleJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ScheduleEntity> streamAllPublic() { | ||
Stream<ScheduleEntity> entity = em.createQuery("SELECT s FROM ScheduleEntity s WHERE s.scheduleType = 'PUBLIC'", ScheduleEntity.class).getResultStream(); | ||
return entity; | ||
} | ||
|
||
|
||
public Stream<ScheduleEntity> streamAllPrivate() { | ||
Stream<ScheduleEntity> entities = em.createQuery("SELECT s FROM ScheduleEntity s WHERE s.scheduleType = 'PRIVATE'", ScheduleEntity.class) | ||
.getResultStream(); | ||
return entities; | ||
} | ||
|
||
@Override | ||
public Optional<ScheduleEntity> findById(Long eventId) { | ||
Optional<ScheduleEntity> 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); | ||
} | ||
} | ||
} |