CustomerController.java 2.23 KB
Newer Older
jorgecacs committed
1 2 3 4 5 6 7 8 9
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;
jorgecacs committed
10
import org.springframework.web.bind.annotation.RequestBody;
jorgecacs committed
11 12 13 14 15 16 17 18 19 20
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
jorgecacs committed
21
@RequestMapping("/api-customers")
jorgecacs committed
22 23 24 25 26 27 28 29 30 31 32
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;

jorgecacs committed
33
			return new ResponseEntity(customerService.getCustomers(), HttpStatus.OK);
jorgecacs committed
34 35 36 37 38 39 40 41 42 43 44 45 46

		} 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;
jorgecacs committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

			return new ResponseEntity(customerService.get(id), HttpStatus.OK);

		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<Error>(new Error(), HttpStatus.UNPROCESSABLE_ENTITY);
		}
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity get(@RequestBody Customer customer) {
		try {
			ResponseEntity response = null;

			return new ResponseEntity(customerService.insert(customer), HttpStatus.OK);

		} catch (Exception e) {
jorgecacs committed
64 65 66 67 68 69
			e.printStackTrace();
			return new ResponseEntity<Error>(new Error(), HttpStatus.UNPROCESSABLE_ENTITY);
		}
	}

}