-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clock event management classes and services
Introduced `ClockEvent`, `ClockEventType`, and associated exceptions for clock in/out management. Added repositories and services for managing clock events, including both in-memory and Eclipse storage implementations. Updated configuration files to support new dependencies and settings.
- Loading branch information
1 parent
f15fa42
commit 23a7c60
Showing
16 changed files
with
276 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
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,33 @@ | ||
package dev.nathanlively; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
@Component | ||
public class ClockEclipseAdapter implements ClockRepository { | ||
private final EclipseClockRepository repository; | ||
|
||
public ClockEclipseAdapter(EclipseClockRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@Override | ||
public void save(ClockEvent clockEvent) { | ||
repository.save(clockEvent); | ||
} | ||
|
||
@Override | ||
public List<ClockEvent> findAll() { | ||
return repository.findAll(); | ||
} | ||
|
||
@Override | ||
public ClockEventType findLast() { | ||
return repository.findAll().stream() | ||
.max(Comparator.comparing(ClockEvent::time)) | ||
.map(ClockEvent::type) | ||
.orElse(null); | ||
} | ||
} |
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,15 @@ | ||
package dev.nathanlively; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record ClockEvent(LocalDateTime time, ClockEventType type) { | ||
public ClockEvent { | ||
if (time == null) { | ||
throw new IllegalArgumentException("Event time is empty"); | ||
} | ||
if (type == null) { | ||
throw new IllegalArgumentException("Event type is empty"); | ||
} | ||
} | ||
|
||
} |
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,54 @@ | ||
package dev.nathanlively; | ||
|
||
import java.time.Clock; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ClockEventService { | ||
private final ClockRepository clockRepository; | ||
private final Clock clock; | ||
|
||
public ClockEventService(ClockRepository clockRepository, Clock clock) { | ||
this.clockRepository = clockRepository; | ||
this.clock = clock; | ||
} | ||
|
||
public List<ClockEventView> all() { | ||
return clockRepository.findAll().stream() | ||
.sorted((event1, event2) -> event2.time().compareTo(event1.time())) // Sorting in descending order by time | ||
.map(ClockEventView::from) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public ClockEventView clockIn() { | ||
validateClockIn(); | ||
ClockEvent clockEvent = new ClockEvent(new ClockService(clock).now(), ClockEventType.IN); | ||
clockRepository.save(clockEvent); | ||
return ClockEventView.from(clockEvent); | ||
} | ||
|
||
public ClockEventView clockOut() { | ||
validateClockOut(); | ||
ClockEvent clockEvent = new ClockEvent(new ClockService(clock).now(), ClockEventType.OUT); | ||
clockRepository.save(clockEvent); | ||
return ClockEventView.from(clockEvent); | ||
} | ||
|
||
private void validateClockIn() { | ||
ClockEventType previousEventType = getLastClockEventType(); | ||
if (previousEventType == ClockEventType.IN) { | ||
throw new ClockInException("You must clock out first! ⏰"); | ||
} | ||
} | ||
|
||
public ClockEventType getLastClockEventType() { | ||
return clockRepository.findLast(); | ||
} | ||
|
||
private void validateClockOut() { | ||
ClockEventType previousEventType = getLastClockEventType(); | ||
if (previousEventType == ClockEventType.OUT) { | ||
throw new ClockOutException("You must clock in first! 🙈"); | ||
} | ||
} | ||
} |
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,6 @@ | ||
package dev.nathanlively; | ||
|
||
public enum ClockEventType { | ||
IN, | ||
OUT | ||
} |
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,7 @@ | ||
package dev.nathanlively; | ||
|
||
public record ClockEventView(String timeStamp) { | ||
static ClockEventView from(ClockEvent event) { | ||
return new ClockEventView(event.time().toString() + " " + event.type().toString()); | ||
} | ||
} |
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,19 @@ | ||
package dev.nathanlively; | ||
|
||
public class ClockInException extends RuntimeException { | ||
public ClockInException(String message) { | ||
super(message); | ||
} | ||
|
||
public ClockInException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ClockInException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected ClockInException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
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,19 @@ | ||
package dev.nathanlively; | ||
|
||
public class ClockOutException extends RuntimeException { | ||
public ClockOutException(String message) { | ||
super(message); | ||
} | ||
|
||
public ClockOutException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ClockOutException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected ClockOutException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
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,11 @@ | ||
package dev.nathanlively; | ||
|
||
import java.util.List; | ||
|
||
public interface ClockRepository { | ||
void save(ClockEvent clockEvent); | ||
|
||
List<ClockEvent> findAll(); | ||
|
||
ClockEventType findLast(); | ||
} |
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,41 @@ | ||
package dev.nathanlively; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
|
||
public class ClockService { | ||
private final Clock clock; | ||
|
||
public ClockService(Clock clock) { | ||
this.clock = clock; | ||
} | ||
|
||
public static LocalDateTime aug7at8am() { | ||
Clock fixedClock = fixedAtAug7at8am(); | ||
return LocalDateTime.now(fixedClock); | ||
} | ||
|
||
public static Clock fixedAtAug7at8am() { | ||
return fixed("2024-08-07T08:00:00.00Z"); | ||
} | ||
|
||
private static Clock fixed(String text) { | ||
return Clock.fixed(Instant.parse(text), ZoneId.of("UTC")); | ||
} | ||
|
||
public static LocalDateTime aug7at5pm() { | ||
Clock fixedClock = fixed("2024-08-07T17:00:00.00Z"); | ||
return LocalDateTime.now(fixedClock); | ||
} | ||
|
||
public static LocalDateTime aug8() { | ||
Clock fixedClock = fixed("2024-08-08T08:56:30.00Z"); | ||
return LocalDateTime.now(fixedClock); | ||
} | ||
|
||
public LocalDateTime now() { | ||
return LocalDateTime.now(clock); | ||
} | ||
} |
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,6 @@ | ||
package dev.nathanlively; | ||
|
||
import org.springframework.data.repository.ListCrudRepository; | ||
|
||
public interface EclipseClockRepository extends ListCrudRepository<ClockEvent, Long> { | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/dev/nathanlively/InMemoryClockRepository.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,39 @@ | ||
package dev.nathanlively; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class InMemoryClockRepository implements ClockRepository { | ||
private final List<ClockEvent> clockEvents; | ||
|
||
public InMemoryClockRepository(List<ClockEvent> clockEvents) { | ||
this.clockEvents = clockEvents; | ||
} | ||
|
||
public static InMemoryClockRepository create(List<ClockEvent> clockEvents) { | ||
return new InMemoryClockRepository(clockEvents); | ||
} | ||
|
||
public static InMemoryClockRepository createEmpty() { | ||
return create(new ArrayList<>()); | ||
} | ||
|
||
@Override | ||
public void save(ClockEvent clockEvent) { | ||
clockEvents.add(clockEvent); | ||
} | ||
|
||
@Override | ||
public List<ClockEvent> findAll() { | ||
return new ArrayList<>(clockEvents); | ||
} | ||
|
||
@Override | ||
public ClockEventType findLast() { | ||
return clockEvents.stream() | ||
.max(Comparator.comparing(ClockEvent::time)) | ||
.map(ClockEvent::type) | ||
.orElse(null); | ||
} | ||
} |