Commit 4be642aa authored by Philippe Fonzin's avatar Philippe Fonzin

Merge branch 'backend2' into 'master'

Backend2

See merge request !14
parents e2ed61e6 8f6b9676
...@@ -80,6 +80,11 @@ ...@@ -80,6 +80,11 @@
<artifactId>google-api-client</artifactId> <artifactId>google-api-client</artifactId>
<version>1.31.2</version> <version>1.31.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.ascendfinalproject.warehouse.controllers; package com.ascendfinalproject.warehouse.controllers;
import com.ascendfinalproject.warehouse.models.OrderResponse; import com.ascendfinalproject.warehouse.models.WarehouseOrderRequest;
import com.ascendfinalproject.warehouse.models.Session; import com.ascendfinalproject.warehouse.models.WarehouseOrderResponse;
import com.ascendfinalproject.warehouse.models.WarehouseOrder;
import com.ascendfinalproject.warehouse.services.SessionService;
import com.ascendfinalproject.warehouse.services.WarehouseOrderService; import com.ascendfinalproject.warehouse.services.WarehouseOrderService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.validation.Valid;
@RestController @RestController
@RequestMapping(value = "/api") @RequestMapping(value = "/api")
public class WarehouseController { public class WarehouseController {
...@@ -23,7 +21,7 @@ public class WarehouseController { ...@@ -23,7 +21,7 @@ public class WarehouseController {
@CrossOrigin @CrossOrigin
@GetMapping(value = "/orders") @GetMapping(value = "/orders")
public Flux<WarehouseOrder> getOrders() { public Flux<WarehouseOrderResponse> getOrders() {
return orderService.getOrders(); return orderService.getOrders();
} }
...@@ -38,14 +36,17 @@ public class WarehouseController { ...@@ -38,14 +36,17 @@ public class WarehouseController {
@CrossOrigin @CrossOrigin
@PostMapping(value = "/orders") @PostMapping(value = "/orders")
public Mono<WarehouseOrder> createOrder(@RequestBody WarehouseOrder order) { public Mono<WarehouseOrderResponse> createOrder(@Valid @RequestBody WarehouseOrderRequest order) {
return orderService.createOrder(order); return orderService.createOrder(order);
} }
@CrossOrigin @CrossOrigin
@PutMapping(value = "/orders/{id}") @PutMapping(value = "/orders/{id}")
public Mono<WarehouseOrder> updateOrder(@RequestBody WarehouseOrder order, @PathVariable(value = "id") String id) { public Mono<ResponseEntity> updateOrder(@RequestBody WarehouseOrderResponse order, @PathVariable(value = "id") String id) {
return orderService.updateOrder(order, id); return orderService.updateOrder(order, id)
.map(updatedOrder -> (ResponseEntity.status(HttpStatus.OK).body(order)))
.cast(ResponseEntity.class)
.defaultIfEmpty(ResponseEntity.status(HttpStatus.NOT_FOUND).body(null));
} }
@CrossOrigin @CrossOrigin
......
package com.ascendfinalproject.warehouse.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException {
public NotFoundException() {
super();
}
public NotFoundException(String message) {
super(message);
}
}
package com.ascendfinalproject.warehouse.models;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Address {
private String street;
private String city;
private String state;
private String zip;
public Address(String street, String city, String state, String zip) {
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
}
package com.ascendfinalproject.warehouse.models;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Getter
@Setter
public class OrderResponse {
private List<String> allIds = new ArrayList<>();
private Map<String, WarehouseOrder> byId = new HashMap<>();
public void appendId(String id) {
allIds.add(id);
}
public void addOrder(WarehouseOrder order) {
byId.put(order.getId(), order);
}
@Override
public String toString() {
return "OrderResponse{" +
"allIds=" + allIds +
", byId=" + byId +
'}';
}
}
package com.ascendfinalproject.warehouse.models;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import java.util.List;
@Getter
@Setter
public class WarehouseOrderRequest {
@Id
private String id;
private String orderTrackingCode;
private String orderStatus;
private long orderCreatedAt;
private long orderUpdatedAt;
private String customerId;
private String customerEmailAddress;
private List<Item> orderItems;
private Address customerAddress;
}
...@@ -9,7 +9,7 @@ import java.util.List; ...@@ -9,7 +9,7 @@ import java.util.List;
@Getter @Getter
@Setter @Setter
public class WarehouseOrder { public class WarehouseOrderResponse {
@Id @Id
private String id; private String id;
private String orderId; private String orderId;
...@@ -19,6 +19,6 @@ public class WarehouseOrder { ...@@ -19,6 +19,6 @@ public class WarehouseOrder {
private List<Item> orderItems; private List<Item> orderItems;
private String address; private String address;
public WarehouseOrder() { public WarehouseOrderResponse() {
} }
} }
package com.ascendfinalproject.warehouse.repositories;
import com.ascendfinalproject.warehouse.models.Session;
import org.springframework.data.repository.CrudRepository;
public interface SessionRepository extends CrudRepository<Session, String> {
}
package com.ascendfinalproject.warehouse.repositories; package com.ascendfinalproject.warehouse.repositories;
import com.ascendfinalproject.warehouse.models.WarehouseOrder; import com.ascendfinalproject.warehouse.models.WarehouseOrderResponse;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
public interface WarehouseOrderRepository extends ReactiveMongoRepository<WarehouseOrder, String> { public interface WarehouseOrderRepository extends ReactiveMongoRepository<WarehouseOrderResponse, String> {
} }
...@@ -7,7 +7,6 @@ import org.springframework.stereotype.Service; ...@@ -7,7 +7,6 @@ import org.springframework.stereotype.Service;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Scanner;
@Service @Service
public class SessionService { public class SessionService {
......
package com.ascendfinalproject.warehouse.services; package com.ascendfinalproject.warehouse.services;
import com.ascendfinalproject.warehouse.models.Item; import com.ascendfinalproject.warehouse.exceptions.NotFoundException;
import com.ascendfinalproject.warehouse.models.OrderResponse; import com.ascendfinalproject.warehouse.models.Address;
import com.ascendfinalproject.warehouse.models.WarehouseOrder; import com.ascendfinalproject.warehouse.models.WarehouseOrderRequest;
import com.ascendfinalproject.warehouse.models.WarehouseOrderResponse;
import com.ascendfinalproject.warehouse.repositories.WarehouseOrderRepository; import com.ascendfinalproject.warehouse.repositories.WarehouseOrderRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
@Service @Service
public class WarehouseOrderService { public class WarehouseOrderService {
...@@ -21,27 +19,31 @@ public class WarehouseOrderService { ...@@ -21,27 +19,31 @@ public class WarehouseOrderService {
@Autowired @Autowired
WarehouseOrderRepository orderRepository; WarehouseOrderRepository orderRepository;
public Mono<WarehouseOrder> getById(String id) { public Mono<WarehouseOrderResponse> getById(String id) {
return orderRepository.findById(id); return orderRepository.findById(id);
} }
public Flux<WarehouseOrder> getOrders() { public Flux<WarehouseOrderResponse> getOrders() {
return orderRepository.findAll(); return orderRepository.findAll();
} }
public Mono<WarehouseOrder> createOrder(WarehouseOrder order) { public Mono<WarehouseOrderResponse> createOrder(WarehouseOrderRequest order) {
order.setStatus("RECEIVED"); WarehouseOrderResponse response = new WarehouseOrderResponse();
order.setCreatedAt(new Date(System.currentTimeMillis())); response.setOrderId(order.getId());
List<Item> itemList = Arrays.asList( response.setStatus("RECEIVED");
new Item("3", "Hamburger", 3, 3, 33), response.setCreatedAt(new Date(order.getOrderCreatedAt()));
new Item("4", "Sausage", 4, 5, 66), response.setModifiedAt(new Date(order.getOrderUpdatedAt()));
new Item("5", "Fries", 3, 4, 33)); response.setOrderItems(order.getOrderItems());
order.setOrderItems(itemList); Address address = order.getCustomerAddress();
order.setAddress("123 apple st"); response.setAddress(address.getStreet() + ", " +
return orderRepository.save(order); address.getCity() + ", " +
address.getState() + ", " +
address.getZip()
);
return orderRepository.save(response);
} }
public Mono<WarehouseOrder> updateOrder(WarehouseOrder order, String id) { public Mono<WarehouseOrderResponse> updateOrder(WarehouseOrderResponse order, String id) {
return orderRepository.findById(id) return orderRepository.findById(id)
.flatMap(existingOrder -> { .flatMap(existingOrder -> {
existingOrder.setStatus(order.getStatus()); existingOrder.setStatus(order.getStatus());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment