CustomerController.java 1.83 KB
Newer Older
jorgecacs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package com.rshk.demo.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.rshk.demo.beans.Customer;
import com.rshk.demo.beans.CustomerGetRespData;
import com.rshk.demo.beans.CustomerListData;
import com.rshk.demo.services.CustomerService;

@RestController
@RequestMapping("/customers")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity getCustomerList() {

		try {
			ResponseEntity response = null;

			List<Customer> customerList = customerService.getCustomers();

			return new ResponseEntity(new CustomerListData(customerList), HttpStatus.OK);

		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<com.rshk.demo.beans.Error>(
					new com.rshk.demo.beans.Error("FTL_PF_1231", e.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
		}

	}

	@RequestMapping(value = "/{document}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity get(@PathVariable String id) {
		try {
			ResponseEntity response = null;
			
			Customer customer = customerService.get(id);
			
			return new ResponseEntity(new CustomerGetRespData(customer), HttpStatus.OK);
			
		}catch(Exception e) {
			e.printStackTrace();
			return new ResponseEntity<Error>(new Error(), HttpStatus.UNPROCESSABLE_ENTITY);
		}
	}

}