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.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean; 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(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return objectMapper; return objectMapper;
} }
} }

View file

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

View file

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