latest controller

This commit is contained in:
Tibor Tarnai 2019-12-10 17:21:40 +01:00
parent 6de5dd02ed
commit a207d663a7
4 changed files with 60 additions and 7 deletions

View file

@ -0,0 +1,22 @@
package com.sap.tamagotchi.configuration;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfiguration {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
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);
return objectMapper;
}
}

View file

@ -10,7 +10,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import static org.springframework.http.ResponseEntity.ok; import static org.springframework.http.ResponseEntity.ok;
@ -35,7 +37,11 @@ public class DeviceController {
} }
@PostMapping("/devices") @PostMapping("/devices")
public ResponseEntity createDevice(@RequestBody CreateDevicePayload payload) { public ResponseEntity createDevice(@RequestBody Collection<CreateDevicePayload> payload) {
return ok(tamagotchiService.createDevice(new Device(payload.getOwner(), payload.getColor()))); List<Device> devices = new ArrayList<>();
for (CreateDevicePayload p : payload) {
devices.add(tamagotchiService.createDevice(new Device(p.getOwner(), p.getColor())));
}
return ok(devices);
} }
} }

View file

@ -1,18 +1,24 @@
package com.sap.tamagotchi.model; package com.sap.tamagotchi.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CreateDevicePayload { public class CreateDevicePayload {
private final String owner; private final String owner;
private final Color color; private final Color color;
public CreateDevicePayload(String owner, Color color) { @JsonCreator
public CreateDevicePayload(@JsonProperty("owner") String owner, @JsonProperty("color") Color color) {
this.owner = owner; this.owner = owner;
this.color = color; this.color = color;
} }
@JsonProperty("owner")
public String getOwner() { public String getOwner() {
return owner; return owner;
} }
@JsonProperty("color")
public Color getColor() { public Color getColor() {
return color; return color;
} }

View file

@ -1,46 +1,65 @@
package com.sap.tamagotchi.model; package com.sap.tamagotchi.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
public class Device extends Thread { @JsonIgnoreProperties(ignoreUnknown = true)
public class Device {
@JsonProperty("id")
private final String getDeviceId = UUID.randomUUID().toString(); private final String getDeviceId = UUID.randomUUID().toString();
@JsonProperty("owner")
private final String owner; private final String owner;
@JsonProperty("color")
private final Color color; private final Color color;
@JsonProperty("born")
private final Instant born = Instant.now(); private final Instant born = Instant.now();
private final Queue<String> messages = new ConcurrentLinkedQueue<String>(); private final Queue<IoTMessage> messages = new ConcurrentLinkedQueue<>();
public Device(String owner, Color color) { public Device(String owner, Color color) {
this.owner = owner; this.owner = owner;
this.color = color; this.color = color;
} }
@JsonProperty("id")
public String getDeviceId() { public String getDeviceId() {
return getDeviceId; return getDeviceId;
} }
@JsonProperty("owner")
public String getOwner() { public String getOwner() {
return owner; return owner;
} }
@JsonProperty("color")
public Color getColor() { public Color getColor() {
return color; return color;
} }
@JsonProperty("born")
public Instant getBorn() { public Instant getBorn() {
return born; return born;
} }
@JsonIgnore
public boolean hasMessages() { public boolean hasMessages() {
return !messages.isEmpty(); return !messages.isEmpty();
} }
@JsonIgnore
public Collection<IoTMessage> getMessages() { public Collection<IoTMessage> getMessages() {
return Collections.emptyList(); ArrayList<IoTMessage> m = new ArrayList<>();
while (!messages.isEmpty()) {
m.add(messages.poll());
}
return m;
} }
} }