Skip to content

Commit

Permalink
Merge pull request #1 from LiveNathan/copy-from-spring
Browse files Browse the repository at this point in the history
copy-from-spring
  • Loading branch information
LiveNathan authored Aug 16, 2024
2 parents f15fa42 + 22d16d8 commit 67dc842
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ drivers/
# Error screenshots generated by TestBench for failed integration tests
error-screenshots/
webpack.generated.js

storage
1 change: 1 addition & 0 deletions .run/ALL TESTS.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<option name="VM_PARAMETERS" value="-ea --add-opens=java.base/java.util=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED" />
<tag value="!ui" />
<method v="2">
<option name="Make" enabled="true" />
Expand Down
1 change: 1 addition & 0 deletions .run/Application.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<option name="FRAME_DEACTIVATION_UPDATE_POLICY" value="UpdateClassesAndResources" />
<module name="clocker-vaadin" />
<option name="SPRING_BOOT_MAIN_CLASS" value="dev.nathanlively.Application" />
<option name="VM_PARAMETERS" value="--add-opens=java.base/java.util=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED" />
<extension name="com.fapiko.jetbrains.plugins.better_direnv.runconfigs.IdeaRunConfigurationExtension">
<option name="DIRENV_ENABLED" value="false" />
<option name="DIRENV_TRUSTED" value="false" />
Expand Down
1 change: 1 addition & 0 deletions .run/UI.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<option name="MAIN_CLASS_NAME" value="dev.nathanlively.views.clock.HomeViewTest" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="tags" />
<option name="VM_PARAMETERS" value="-ea --add-opens=java.base/java.util=ALL-UNNAMED --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED" />
<tag value="ui" />
<method v="2">
<option name="Make" enabled="true" />
Expand Down
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@
<artifactId>vaadin-testbench-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.github.mvysny.kaributesting</groupId>
<artifactId>karibu-testing-v10-spring</artifactId>
<version>2.1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.xdev</groupId>
<artifactId>spring-data-eclipse-store</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -95,6 +96,14 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
--add-opens=java.base/java.util=ALL-UNNAMED
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.time=ALL-UNNAMED
</jvmArguments>
</configuration>
</plugin>

<plugin>
Expand Down Expand Up @@ -155,6 +164,14 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
--add-opens=java.base/java.util=ALL-UNNAMED
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.time=ALL-UNNAMED
</jvmArguments>
</configuration>
<executions>
<execution>
<id>start-spring-boot</id>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/nathanlively/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.vaadin.flow.theme.lumo.Lumo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;

/**
* The entry point of the Spring Boot application.
Expand All @@ -15,6 +16,7 @@
*
*/
@SpringBootApplication
@EnableEclipseStoreRepositories
@NpmPackage(value = "@fontsource/anton", version = "4.5.0")
@Theme(value = "clocker-vaadin", variant = Lumo.DARK)
public class Application implements AppShellConfigurator {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/dev/nathanlively/ClockEclipseAdapter.java
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);
}
}
15 changes: 15 additions & 0 deletions src/main/java/dev/nathanlively/ClockEvent.java
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");
}
}

}
54 changes: 54 additions & 0 deletions src/main/java/dev/nathanlively/ClockEventService.java
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! 🙈");
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/dev/nathanlively/ClockEventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.nathanlively;

public enum ClockEventType {
IN,
OUT
}
7 changes: 7 additions & 0 deletions src/main/java/dev/nathanlively/ClockEventView.java
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());
}
}
19 changes: 19 additions & 0 deletions src/main/java/dev/nathanlively/ClockInException.java
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);
}
}
19 changes: 19 additions & 0 deletions src/main/java/dev/nathanlively/ClockOutException.java
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);
}
}
11 changes: 11 additions & 0 deletions src/main/java/dev/nathanlively/ClockRepository.java
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();
}
41 changes: 41 additions & 0 deletions src/main/java/dev/nathanlively/ClockService.java
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);
}
}
6 changes: 6 additions & 0 deletions src/main/java/dev/nathanlively/EclipseClockRepository.java
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> {
}
Loading

0 comments on commit 67dc842

Please sign in to comment.