add new payload format

This commit is contained in:
Tibor Tarnai 2019-12-11 10:54:48 +01:00
parent 81e3545234
commit ef8bf3e86a
3 changed files with 25 additions and 12 deletions

View file

@ -3,6 +3,7 @@ package com.sap.tamagotchi.configuration;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
@ -17,6 +18,7 @@ public class ApplicationConfiguration {
objectMapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return objectMapper;
}
}

View file

@ -1,11 +1,14 @@
package com.sap.tamagotchi.controller;
import static com.sap.tamagotchi.model.Color.RED;
import static com.sap.tamagotchi.model.Color.YELLOW;
import static org.springframework.http.ResponseEntity.ok;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.sap.tamagotchi.model.Color;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
@ -27,6 +30,15 @@ public class DeviceController {
this.tamagotchiService = tamagotchiService;
}
private static Color mapColor(String productId) {
switch (productId) {
case "66VCHSJNUP":
return RED;
default:
return YELLOW;
}
}
@RequestMapping("/devices/{deviceId}")
public Device getDevice(String deviceId) {
return tamagotchiService.getDevice(deviceId);
@ -41,7 +53,7 @@ public class DeviceController {
public ResponseEntity createDevice(@RequestBody Collection<CreateDevicePayload> payload) {
List<Device> devices = new ArrayList<>();
for (CreateDevicePayload p : payload) {
devices.add(tamagotchiService.createDevice(new Device(p.getOwner(), p.getColor())));
devices.add(tamagotchiService.createDevice(new Device(p.getOwner(), mapColor(p.getProductId()))));
}
return ok(devices);
}
@ -50,5 +62,4 @@ public class DeviceController {
public String warmup() {
return "warming up";
}
}

View file

@ -4,22 +4,22 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CreateDevicePayload {
private final String owner;
private final Color color;
private final String userId;
private final String productId;
@JsonCreator
public CreateDevicePayload(@JsonProperty("owner") String owner, @JsonProperty("color") Color color) {
this.owner = owner;
this.color = color;
public CreateDevicePayload(@JsonProperty("userId") String userId, @JsonProperty("productId") String productId) {
this.userId = userId;
this.productId = productId;
}
@JsonProperty("owner")
@JsonProperty("userId")
public String getOwner() {
return owner;
return userId;
}
@JsonProperty("color")
public Color getColor() {
return color;
@JsonProperty("productId")
public String getProductId() {
return productId;
}
}