com.recursivechaos.clearent.controller.SaleController.java Source code

Java tutorial

Introduction

Here is the source code for com.recursivechaos.clearent.controller.SaleController.java

Source

/**
 * Created by Andrew Bell 2/1/2016
 * www.recursivechaos.com
 * andrew@recursivechaos.com
 * Licensed under MIT License 2016. See license.txt for details.
 */

package com.recursivechaos.clearent.controller;

import com.recursivechaos.clearent.domain.Sale;
import com.recursivechaos.clearent.service.ResponseService;
import com.recursivechaos.clearent.service.SaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;

@RestController
public class SaleController {

    @Autowired
    SaleService saleService;

    @Autowired
    ResponseService responseService;

    @RequestMapping(value = "/sales", method = RequestMethod.POST)
    public ResponseEntity<Void> postSale(@RequestBody Sale sale) {
        Sale newSale = saleService.createSale(sale);
        return new ResponseEntity<>(createHeaders(newSale), HttpStatus.CREATED);
    }

    private HttpHeaders createHeaders(Sale newSale) {
        final URI location = responseService.getLocationUri(newSale);
        final HttpHeaders headers = new HttpHeaders();
        headers.setLocation(location);
        return headers;
    }

}