Skip to content

Commit

Permalink
fix: Update delete query and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielPalafox committed Jan 4, 2024
1 parent f96c147 commit a6db989
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface EventDao {
void createEvent(EventEntity eventEntity);

EventEntity find(long id);

void delete(long id);

List<EventEntity> findAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public EventEntity find(long id) {
return entityManager.find(EventEntity.class, id);
}

@Override
@Transactional
public void delete(long id) {
entityManager.remove(find(id));
}

@Override
public List<EventEntity> findAll() {
TypedQuery<EventEntity> query = entityManager.createQuery("from EventEntity", EventEntity.class);
Expand All @@ -43,7 +49,7 @@ public List<EventEntity> findAll() {
@Override
@Transactional
public void deleteEventsByDate(Integer numberOfDays) {
String query = "DELETE FROM event where CAST(date_created as date) < CAST(now() - (CAST('1' AS INTERVAL DAY) * 1) as date) * :numberOfDays)";
String query = "DELETE FROM event where CAST(date_created as date) < CAST(now() - (CAST('1' AS INTERVAL DAY) * :numberOfDays) as date)";
Query queryDelete = entityManager.createNativeQuery(query);
queryDelete.setParameter("numberOfDays", numberOfDays);
queryDelete.executeUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.AfterClass;
Expand Down Expand Up @@ -77,5 +78,7 @@ public void testWriteEvent() throws IllegalAccessException {
assertEquals(eventEntity.getId(), fromDb.getId());
assertEquals(eventEntity.getLabel(), fromDb.getLabel());
assertNotNull(fromDb.getDateCreated());

eventDao.delete(eventEntity.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.springframework.test.context.ContextConfiguration;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -42,7 +45,9 @@ private void createEvents() {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType("Sign-In");
eventEntity.setClientId("Client " + 1);
eventEntity.setDateCreated(new Date());
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
eventEntity.setDateCreated(Date.from(instant));
eventDao.createEvent(eventEntity);
}
}
Expand Down

0 comments on commit a6db989

Please sign in to comment.