ranyeli
9/6/2017 - 9:51 PM

Api to handle orders

Api to manage order request and responses

package com.project.api;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.project.dao.OrderDao;
import com.project.dto.OrderItemsDTO;
import com.project.model.CustomerOrder;
import com.project.model.Item;
import com.project.model.OrderItem;

@RestController
public class OrderApi {
	
	@Autowired
	private OrderDao _dao;
	
	@PostMapping("api/order/add")
	public void addCustomerOrder(@RequestBody OrderItemsDTO dto) {
		CustomerOrder order = dto.getOrder();
		List<Item> items = _dao.referToAll(Item.class, dto.getItemsId());
		
		items.forEach(item->{
			OrderItem orderItem = new OrderItem();
			orderItem.setItems(item);
			orderItem.setOrder(order);
			order.getOrderItem().add(orderItem);
		});

		_dao.save(order);
	}
	
	@PostMapping("api/orders/add")
	public void addCustomerOrder(@RequestBody List<CustomerOrder> order) {
		_dao.saveAll(order);
	}
	
	@DeleteMapping("api/order/delete")
	public void deleteCustomerOrder(@RequestBody CustomerOrder order) {
		_dao.delete(order);
	}
	
	@PutMapping("api/order/update")
	public void updateCustomerOrder(@RequestBody CustomerOrder order) {
		_dao.update(order);
	}
	
	@GetMapping("api/order/{id}")
	public CustomerOrder getCustomerOrder(@PathVariable("id") long id) {
		return _dao.findById(CustomerOrder.class, id);
	}
	
	@GetMapping("api/orders")
	public List<CustomerOrder> getCustomerOrders(
			@RequestParam(value="offset", required=false) Integer offset, 
			@RequestParam(value="limit", required=false) Integer limit){
		return _dao.findAll(CustomerOrder.class, offset, limit);
	}
	
/*	@GetMapping("api/orders/search/{by}")
	public List<CustomerOrder> searchCustomerOrder(
			@PathVariable("by") String by, 
			@RequestParam(value="term", required=false) String term,
			@RequestParam(value = "min", required=false) Double min,
			@RequestParam(value="max", required=false) Double max
			){
		return by.trim().equals("price") ? 
				_dao.findByPrice(min, max) :_dao.findByDesc(term) ;
	}*/

}