From 0b89e889054bc401c395182be2936a97b9f73c36 Mon Sep 17 00:00:00 2001 From: "julius.lautz" Date: Sat, 4 Dec 2021 15:16:29 +0100 Subject: [PATCH] added the necessary classes for the discovery web controller --- ...ctionHouseDiscoveryJsonRepresentation.java | 45 ++++++++++++++ .../AuctionHouseDiscoveryWebController.java | 56 +++++++++++++++++ .../port/in/AuctionHouseDiscoveryCommand.java | 40 ++++++++++++ .../port/in/AuctionHouseDiscoveryUseCase.java | 8 +++ .../AuctionHouseDiscoveryInformation.java | 62 +++++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionHouseDiscoveryJsonRepresentation.java create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/web/AuctionHouseDiscoveryWebController.java create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryCommand.java create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryUseCase.java create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscoveryInformation.java diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionHouseDiscoveryJsonRepresentation.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionHouseDiscoveryJsonRepresentation.java new file mode 100644 index 0000000..e417246 --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/common/formats/AuctionHouseDiscoveryJsonRepresentation.java @@ -0,0 +1,45 @@ +package ch.unisg.tapas.auctionhouse.adapter.common.formats; + +import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Getter; +import lombok.Setter; + +public class AuctionHouseDiscoveryJsonRepresentation { + public static final String MEDIA_TYPE = "application/auctionhousediscovery + json"; + + @Getter @Setter + private String auctionHouseUri; + + @Getter @Setter + private String webSubUri; + + @Getter @Setter + private String taskTypes; + + @Getter @Setter + private String timeStamp; + + @Getter @Setter + private String groupName; + + public AuctionHouseDiscoveryJsonRepresentation() {} + + public AuctionHouseDiscoveryJsonRepresentation(AuctionHouseDiscoveryInformation auctionHouseDiscoveryInformation){ + this.auctionHouseUri = auctionHouseDiscoveryInformation.getAuctionHouseUri().getValue(); + this.webSubUri = auctionHouseDiscoveryInformation.getWebSubUri().getValue(); + this.taskTypes = auctionHouseDiscoveryInformation.getTaskTypes().toString(); + this.timeStamp = auctionHouseDiscoveryInformation.getTimeStamp().getValue(); + this.groupName = auctionHouseDiscoveryInformation.getGroupName().getValue(); + } + + public static String serialize(AuctionHouseDiscoveryInformation auctionHouseDiscoveryInformation) throws JsonProcessingException { + AuctionHouseDiscoveryJsonRepresentation representation = new AuctionHouseDiscoveryJsonRepresentation(auctionHouseDiscoveryInformation); + + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + return mapper.writeValueAsString(representation); + } +} diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/web/AuctionHouseDiscoveryWebController.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/web/AuctionHouseDiscoveryWebController.java new file mode 100644 index 0000000..cd766d1 --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/web/AuctionHouseDiscoveryWebController.java @@ -0,0 +1,56 @@ +package ch.unisg.tapas.auctionhouse.adapter.in.web; + +import ch.unisg.tapas.auctionhouse.adapter.common.formats.AuctionHouseDiscoveryJsonRepresentation; +import ch.unisg.tapas.auctionhouse.application.port.in.AuctionHouseDiscoveryUseCase; +import ch.unisg.tapas.auctionhouse.application.port.in.AuctionHouseDiscoveryCommand; +import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + + +/** + * Controller that handles HTTP requests for the auction house discovery. This controller implements + * the {@link AuctionHouseDiscoveryUseCase} use case using the {@link AuctionHouseDiscoveryCommand} command. + */ +@RestController +public class AuctionHouseDiscoveryWebController { + private final AuctionHouseDiscoveryUseCase auctionHouseDiscoveryUseCase; + + public AuctionHouseDiscoveryWebController(AuctionHouseDiscoveryUseCase auctionHouseDiscoveryUseCase) { + this.auctionHouseDiscoveryUseCase = auctionHouseDiscoveryUseCase; + } + + @GetMapping(path="/discovery/", consumes = AuctionHouseDiscoveryJsonRepresentation.MEDIA_TYPE) + public ResponseEntity auctionHouseDiscovery(@RequestBody AuctionHouseDiscoveryJsonRepresentation payload) { + AuctionHouseDiscoveryInformation.GroupName groupName = (payload.getGroupName() == null) ? + null : new AuctionHouseDiscoveryInformation.GroupName(payload.getGroupName()); + + AuctionHouseDiscoveryCommand command = new AuctionHouseDiscoveryCommand( + new AuctionHouseDiscoveryInformation.AuctionHouseUri(payload.getAuctionHouseUri()), + new AuctionHouseDiscoveryInformation.WebSubUri(payload.getAuctionHouseUri()), + new AuctionHouseDiscoveryInformation.TaskTypes(payload.getTaskTypes()), + new AuctionHouseDiscoveryInformation.TimeStamp(payload.getTimeStamp()), + groupName + ); + + AuctionHouseDiscoveryInformation auctionHouseDiscoveryInformation = auctionHouseDiscoveryUseCase.auctionHouseDiscovery(command); + + try { + AuctionHouseDiscoveryJsonRepresentation representation = new AuctionHouseDiscoveryJsonRepresentation(auctionHouseDiscoveryInformation); + String auctionHouseDiscoveryJson = AuctionHouseDiscoveryJsonRepresentation.serialize(auctionHouseDiscoveryInformation); + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, AuctionHouseDiscoveryJsonRepresentation.MEDIA_TYPE); + + return new ResponseEntity<>(auctionHouseDiscoveryJson, responseHeaders, HttpStatus.OK); + } catch (JsonProcessingException e) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryCommand.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryCommand.java new file mode 100644 index 0000000..a2cd62b --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryCommand.java @@ -0,0 +1,40 @@ +package ch.unisg.tapas.auctionhouse.application.port.in; + +import ch.unisg.tapas.auctionhouse.domain.Auction; +import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; +import ch.unisg.tapas.common.SelfValidating; +import lombok.Value; + +import javax.validation.constraints.NotNull; + +@Value +public class AuctionHouseDiscoveryCommand extends SelfValidating { + @NotNull + private final AuctionHouseDiscoveryInformation.AuctionHouseUri auctionHouseUri; + + @NotNull + private final AuctionHouseDiscoveryInformation.WebSubUri webSubUri; + + @NotNull + private final AuctionHouseDiscoveryInformation.TaskTypes taskTypes; + + @NotNull + private final AuctionHouseDiscoveryInformation.TimeStamp timeStamp; + + //Didn't put @NotNull here since the GroupName is not required + private final AuctionHouseDiscoveryInformation.GroupName groupName; + + public AuctionHouseDiscoveryCommand(AuctionHouseDiscoveryInformation.AuctionHouseUri auctionHouseUri, + AuctionHouseDiscoveryInformation.WebSubUri webSubUri, + AuctionHouseDiscoveryInformation.TaskTypes taskTypes, + AuctionHouseDiscoveryInformation.TimeStamp timeStamp, + AuctionHouseDiscoveryInformation.GroupName groupName) { + this.auctionHouseUri = auctionHouseUri; + this.webSubUri = webSubUri; + this.taskTypes = taskTypes; + this.timeStamp = timeStamp; + this.groupName = groupName; + + this.validateSelf(); + } +} diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryUseCase.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryUseCase.java new file mode 100644 index 0000000..f148f17 --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/application/port/in/AuctionHouseDiscoveryUseCase.java @@ -0,0 +1,8 @@ +package ch.unisg.tapas.auctionhouse.application.port.in; + +import ch.unisg.tapas.auctionhouse.domain.AuctionHouseDiscoveryInformation; + +public interface AuctionHouseDiscoveryUseCase { + + AuctionHouseDiscoveryInformation auctionHouseDiscovery(AuctionHouseDiscoveryCommand command); +} diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscoveryInformation.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscoveryInformation.java new file mode 100644 index 0000000..e8f462d --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/domain/AuctionHouseDiscoveryInformation.java @@ -0,0 +1,62 @@ +package ch.unisg.tapas.auctionhouse.domain; + +import lombok.Getter; +import lombok.Value; + +import java.util.ArrayList; + +//TODO: change to array type +public class AuctionHouseDiscoveryInformation { + + @Getter + private final AuctionHouseUri auctionHouseUri; + + @Getter + private final WebSubUri webSubUri; + + @Getter + private final TaskTypes taskTypes; + + @Getter + private final TimeStamp timeStamp; + + //Had to initialize this in the constructor, but it's not required, so I'm not sure + //if this is correct + @Getter + private final GroupName groupName; + + public AuctionHouseDiscoveryInformation(AuctionHouseUri auctionHouseUri, WebSubUri webSubUri, + TaskTypes taskTypes, TimeStamp timeStamp, GroupName groupName){ + this.auctionHouseUri = auctionHouseUri; + this.webSubUri = webSubUri; + this.taskTypes = taskTypes; + this.timeStamp = timeStamp; + this.groupName = groupName; //see above + } + + + @Value + public static class AuctionHouseUri { + private String value; + } + + @Value + public static class WebSubUri { + private String value; + } + + @Value + public static class TaskTypes { + private String value; + } + + @Value + public static class TimeStamp { + private String value; + } + + @Value + public static class GroupName { + private String value; + } +}