diff options
author | Sebastian Baltes <Sebastian.Baltes@uni-bayreuth.de> | 2025-05-13 21:54:23 +0200 |
---|---|---|
committer | Sebastian Baltes <Sebastian.Baltes@uni-bayreuth.de> | 2025-05-13 21:54:23 +0200 |
commit | 1f715d85e17d2bf6c8d73df23e12252b993f74ff (patch) | |
tree | 3f430f6588e91c3e47793d7cd4f9957eb5bf4619 /domain/src/main/java/de/unibayreuth/se/campuscoffee | |
parent | 21fc676c1b5a1d026e1489e9c8fabb80e652a50c (diff) | |
download | se25-assignment03-main.tar.gz se25-assignment03-main.zip |
Diffstat (limited to 'domain/src/main/java/de/unibayreuth/se/campuscoffee')
9 files changed, 198 insertions, 0 deletions
diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/CampusType.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/CampusType.java new file mode 100644 index 0000000..32c326d --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/CampusType.java @@ -0,0 +1,12 @@ +package de.unibayreuth.se.campuscoffee.domain; + +/** + * Enum for different campus locations. + */ +public enum CampusType { + MAIN, + ZAPF, + WITTELSBACHERRING, + KULMBACH + // ... (list incomplete) +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/Pos.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/Pos.java new file mode 100644 index 0000000..f873cd4 --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/Pos.java @@ -0,0 +1,42 @@ +package de.unibayreuth.se.campuscoffee.domain; + +import lombok.*; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +import java.io.Serial; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * Domain class that stores the POS metadata. + */ +@Data +public class Pos implements Serializable { + @Serial + private static final long serialVersionUID = 1L; + + @Nullable + private Long id; // null when POS has not been created yet + @Nullable + private LocalDateTime createdAt = LocalDateTime.now(ZoneId.of("UTC")); // set on POS creation + @Nullable + private LocalDateTime updatedAt = createdAt; // set on POS creation and update + @NonNull + private String name; + @NonNull + private String description; + @NonNull + private PosType type; + @NonNull + private CampusType campus; + @NonNull + private String street; + @NonNull + private String houseNumber; + @NonNull + private Integer postalCode; + @NonNull + private String city; +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/PosType.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/PosType.java new file mode 100644 index 0000000..a7fc04d --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/PosType.java @@ -0,0 +1,10 @@ +package de.unibayreuth.se.campuscoffee.domain; + +/** + * Enum for the different POS types. + */ +public enum PosType { + CAFE, // e.g., Cafeteria, CrazySheep + VENDING_MACHINE, // e.g., vending machines operated by the Studierendenwerk or in Lidl + BAKERY, // e.g., bakeries in grocery stores selling coffee +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/TestFixtures.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/TestFixtures.java new file mode 100644 index 0000000..ae81303 --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/TestFixtures.java @@ -0,0 +1,27 @@ +package de.unibayreuth.se.campuscoffee.domain; + +import de.unibayreuth.se.campuscoffee.domain.ports.PosService; +import org.apache.commons.lang3.SerializationUtils; + +import java.util.List; +import java.util.stream.Collectors; + +public class TestFixtures { + private static final List<Pos> POS_LIST = List.of( + new Pos("CrazySheep (RWI)", "", PosType.CAFE, CampusType.MAIN, "Andreas-Maisel-Weg", "2", 95445, "Bayreuth"), + new Pos("Cafeteria (Mensa)", "", PosType.CAFE, CampusType.MAIN, "Universitätsstraße", "30", 95447, "Bayreuth"), + new Pos("Lidl (Nürnberger Str.)", "", PosType.VENDING_MACHINE, CampusType.ZAPF, "Nürnberger Str.", "3a", 95448, "Bayreuth") + ); + + public static List<Pos> getPosList() { + return POS_LIST.stream() + .map(SerializationUtils::clone) // prevent issues when tests modify the fixture objects + .toList(); + } + + public static List<Pos> createPos(PosService posService) { + return getPosList().stream() + .map(posService::upsert) + .collect(Collectors.toList()); + } +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/MalformedRequestException.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/MalformedRequestException.java new file mode 100644 index 0000000..a42b994 --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/MalformedRequestException.java @@ -0,0 +1,7 @@ +package de.unibayreuth.se.campuscoffee.domain.exceptions; + +public class MalformedRequestException extends RuntimeException { + public MalformedRequestException(String message) { + super(message); + } +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/PosNotFoundException.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/PosNotFoundException.java new file mode 100644 index 0000000..591f35a --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/exceptions/PosNotFoundException.java @@ -0,0 +1,7 @@ +package de.unibayreuth.se.campuscoffee.domain.exceptions; + +public class PosNotFoundException extends RuntimeException { + public PosNotFoundException(String message) { + super(message); + } +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/impl/PosServiceImpl.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/impl/PosServiceImpl.java new file mode 100644 index 0000000..ee4ba8a --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/impl/PosServiceImpl.java @@ -0,0 +1,53 @@ +package de.unibayreuth.se.campuscoffee.domain.impl; + +import de.unibayreuth.se.campuscoffee.domain.Pos; +import de.unibayreuth.se.campuscoffee.domain.exceptions.PosNotFoundException; +import de.unibayreuth.se.campuscoffee.domain.ports.PosDataService; +import de.unibayreuth.se.campuscoffee.domain.ports.PosService; +import lombok.RequiredArgsConstructor; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class PosServiceImpl implements PosService { + private final PosDataService posDataService; + + @Override + public void clear() { + posDataService.clear(); + } + + @Override + @NonNull + public List<Pos> getAll() { + return posDataService.getAll(); + } + + @Override + @NonNull + public Pos getById(@NonNull Long id) throws PosNotFoundException { + return verifyPosExists(id); + } + + @Override + @NonNull + public Pos upsert(@NonNull Pos pos) throws PosNotFoundException { + if (pos.getId() == null) { + pos.setCreatedAt(LocalDateTime.now(ZoneId.of("UTC"))); + } else { + verifyPosExists(pos.getId()); + } + pos.setUpdatedAt(LocalDateTime.now(ZoneId.of("UTC"))); + return posDataService.upsert(pos); + } + + private Pos verifyPosExists(@NonNull Long id) throws PosNotFoundException { + return posDataService.getById(id) + .orElseThrow(() -> new PosNotFoundException("POS with ID " + id + " does not exist.")); + } +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosDataService.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosDataService.java new file mode 100644 index 0000000..7ac626b --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosDataService.java @@ -0,0 +1,20 @@ +package de.unibayreuth.se.campuscoffee.domain.ports; + +import de.unibayreuth.se.campuscoffee.domain.Pos; +import de.unibayreuth.se.campuscoffee.domain.exceptions.PosNotFoundException; +import org.springframework.lang.NonNull; + +import java.util.List; +import java.util.Optional; + +/** + * Interface for the implementation of the POS data service that the domain layer provides as a port. + */ +public interface PosDataService { + void clear(); + @NonNull + List<Pos> getAll(); + Optional<Pos> getById(@NonNull Long id) throws PosNotFoundException; + @NonNull + Pos upsert(@NonNull Pos pos) throws PosNotFoundException; +} diff --git a/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosService.java b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosService.java new file mode 100644 index 0000000..503f3aa --- /dev/null +++ b/domain/src/main/java/de/unibayreuth/se/campuscoffee/domain/ports/PosService.java @@ -0,0 +1,20 @@ +package de.unibayreuth.se.campuscoffee.domain.ports; + +import de.unibayreuth.se.campuscoffee.domain.Pos; +import de.unibayreuth.se.campuscoffee.domain.exceptions.PosNotFoundException; +import org.springframework.lang.NonNull; + +import java.util.List; + +/** + * Interface for the implementation of the POS service that the data layer provides as a port. + */ +public interface PosService { + void clear(); + @NonNull + List<Pos> getAll(); + @NonNull + Pos getById(@NonNull Long id) throws PosNotFoundException; + @NonNull + Pos upsert(@NonNull Pos pos) throws PosNotFoundException; +} |