Skip to content

Commit

Permalink
Add Statistics Provider Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Jun 26, 2024
1 parent d15260d commit 16a468a
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<modules>
<module>account-service-provider</module>
<module>statistics-service-provider</module>
</modules>

</project>
108 changes: 108 additions & 0 deletions statistics-service-provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.sample.schulung.spring</groupId>
<artifactId>statistics-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Statistics Service Provider</name>
<properties>
<java.version>17</java.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok-mapstruct-binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
<exclude>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.sample.schulung.statistics;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StatisticsServiceProviderApplication {

public static void main(String[] args) {
SpringApplication.run(StatisticsServiceProviderApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.sample.schulung.statistics.boundary;

import lombok.Data;

import java.time.LocalDate;

@Data
public class CustomerStatisticsDto {

private long count;
private LocalDate earliestBirthdate;
private LocalDate latestBirthdate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.sample.schulung.statistics.boundary;

import de.sample.schulung.statistics.domain.CustomerStatistics;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface CustomerStatisticsDtoMapper {

CustomerStatisticsDto map(CustomerStatistics source);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.sample.schulung.statistics.boundary;

import de.sample.schulung.statistics.domain.CustomerStatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/statistics")
@RequiredArgsConstructor
public class StatisticsController {

private final CustomerStatisticsService customerStatisticsService;
private final CustomerStatisticsDtoMapper customerStatisticsDtoMapper;

@GetMapping(
value = "/customers",
produces = MediaType.APPLICATION_JSON_VALUE
)
public CustomerStatisticsDto getStatistics() {
return this.customerStatisticsDtoMapper
.map(
this.customerStatisticsService
.getStatistics()
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package de.sample.schulung.statistics.domain;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@Builder
public class Customer {

@NotNull
private UUID uuid;
private LocalDate dateOfBirth;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.sample.schulung.statistics.domain;

import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;

@Data
@Builder
public class CustomerStatistics {

@Builder.Default
private long count = 0;
private LocalDate earliestBirthdate;
private LocalDate latestBirthdate;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.sample.schulung.statistics.domain;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CustomerStatisticsService {

private final CustomerStatisticsSink customerStatisticsSink;

public CustomerStatistics getStatistics() {
return customerStatisticsSink.getStatistics();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.sample.schulung.statistics.domain;

public interface CustomerStatisticsSink {

CustomerStatistics getStatistics();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.sample.schulung.statistics.domain;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.UUID;

@Component
@ConditionalOnProperty(
name = "application.init-sample-data.enabled",
havingValue = "true"
)
@RequiredArgsConstructor
public class CustomersInitializer {

private final CustomersService customersService;

@EventListener(ContextRefreshedEvent.class)
public void createSamples() {
if (customersService.count() < 1) {
customersService.saveCustomer(
Customer.builder()
.uuid(UUID.randomUUID())
.dateOfBirth(LocalDate.now().minusYears(20).minusDays(100))
.build()
);
customersService.saveCustomer(
Customer.builder()
.uuid(UUID.randomUUID())
.dateOfBirth(LocalDate.now().minusYears(30).minusDays(55))
.build()
);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.sample.schulung.statistics.domain;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.UUID;

@Validated
@Service
@RequiredArgsConstructor
public class CustomersService {

private final CustomersSink customersSink;

public void saveCustomer(Customer customer) {
customersSink.saveCustomer(customer);
}

public void deleteCustomer(UUID uuid) {
customersSink.deleteCustomer(uuid);
}

public long count() {
return customersSink.count();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.sample.schulung.statistics.domain;

import java.util.UUID;

public interface CustomersSink {

void saveCustomer(Customer customer);
void deleteCustomer(UUID uuid);
long count();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.sample.schulung.statistics.persistence;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@Entity(name = "Customer")
@Table(name = "CUSTOMERS")
public class CustomerEntity {

@NotNull
@Id
private UUID uuid;
private LocalDate dateOfBirth;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.sample.schulung.statistics.persistence;

import de.sample.schulung.statistics.domain.Customer;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface CustomerEntityMapper {

CustomerEntity map(Customer customer);

}
Loading

0 comments on commit 16a468a

Please sign in to comment.