Skip to content

Commit e8a760f

Browse files
author
Anca
committed
add create/update via rest call
1 parent a4dc2bd commit e8a760f

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ Current available actions using the GUI:
138138

139139
- upload a new image
140140
- delete shipment from the list
141+
- create and update shipment are available only via Postman (or any other API platform)
141142

142143
Files that are not pictures will be deleted
143144
and the shipment picture will be replaced with a generic icon, because we don't want any trouble.

src/main/java/dev/ancaghenade/shipmentlistdemo/controller/ShipmentController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import dev.ancaghenade.shipmentlistdemo.entity.Shipment;
55
import dev.ancaghenade.shipmentlistdemo.service.ShipmentService;
66
import java.util.List;
7+
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.http.MediaType;
89
import org.springframework.web.bind.annotation.CrossOrigin;
910
import org.springframework.web.bind.annotation.DeleteMapping;
1011
import org.springframework.web.bind.annotation.GetMapping;
1112
import org.springframework.web.bind.annotation.PathVariable;
1213
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestBody;
1315
import org.springframework.web.bind.annotation.RequestMapping;
1416
import org.springframework.web.bind.annotation.RequestParam;
1517
import org.springframework.web.bind.annotation.RestController;
@@ -22,6 +24,7 @@ public class ShipmentController {
2224

2325
private final ShipmentService shipmentService;
2426

27+
@Autowired
2528
public ShipmentController(ShipmentService shipmentService) {
2629
this.shipmentService = shipmentService;
2730
}
@@ -31,6 +34,12 @@ public List<Shipment> getAllShipments() {
3134
return shipmentService.getAllShipments();
3235
}
3336

37+
@GetMapping(
38+
path = "{shipmentId}/image/download", produces = MediaType.IMAGE_JPEG_VALUE)
39+
public byte[] downloadShipmentImage(@PathVariable("shipmentId") String shipmentId) {
40+
return shipmentService.downloadShipmentImage(shipmentId);
41+
}
42+
3443
@DeleteMapping("/{shipmentId}")
3544
public String deleteShipment(@PathVariable("shipmentId") String shipmentId) {
3645
return shipmentService.deleteShipment(shipmentId);
@@ -45,10 +54,11 @@ public void uploadShipmentImage(@PathVariable("shipmentId") String shipmentId,
4554
shipmentService.uploadShipmentImage(shipmentId, file);
4655
}
4756

48-
49-
@GetMapping(
50-
path = "{shipmentId}/image/download", produces = MediaType.IMAGE_JPEG_VALUE)
51-
public byte[] downloadShipmentImage(@PathVariable("shipmentId") String shipmentId) {
52-
return shipmentService.downloadShipmentImage(shipmentId);
57+
@PostMapping(
58+
consumes = MediaType.APPLICATION_JSON_VALUE,
59+
produces = MediaType.APPLICATION_JSON_VALUE)
60+
public void saveUpdateShipment(@RequestBody Shipment shipment) {
61+
shipmentService.saveShipment(shipment);
5362
}
63+
5464
}

src/main/java/dev/ancaghenade/shipmentlistdemo/entity/Shipment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import lombok.Getter;
1212
import lombok.NoArgsConstructor;
1313
import lombok.Setter;
14+
import org.springframework.lang.NonNull;
1415

1516
@Data
1617
@Getter
@@ -28,9 +29,11 @@ public class Shipment {
2829
private String shipmentId;
2930

3031
@DynamoDBAttribute
32+
@NonNull
3133
private Participant recipient;
3234

3335
@DynamoDBAttribute
36+
@NonNull
3437
private Participant sender;
3538

3639
@DynamoDBAttribute

src/main/java/dev/ancaghenade/shipmentlistdemo/repository/ShipmentRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public ShipmentRepository(DynamoDBMapper dynamoDBMapper) {
2323
}
2424

2525
public Shipment upsert(Shipment shipment) {
26+
if (Objects.isNull(shipment.getShipmentId())) {
27+
dynamoDBMapper.save(shipment);
28+
}
2629
Shipment shp = dynamoDBMapper.load(Shipment.class, shipment.getShipmentId());
2730
if (Objects.isNull(shp)) {
2831
dynamoDBMapper.save(shipment);

0 commit comments

Comments
 (0)