<commit_before>package de.escalon.hypermedia.sample.store;

import de.escalon.hypermedia.action.Cardinality;
import de.escalon.hypermedia.action.Input;
import de.escalon.hypermedia.action.ResourceHandler;
import de.escalon.hypermedia.affordance.TypedResource;
import de.escalon.hypermedia.sample.beans.store.Offer;
import de.escalon.hypermedia.sample.beans.store.Order;
import de.escalon.hypermedia.sample.beans.store.OrderedItem;
import de.escalon.hypermedia.sample.beans.store.Product;
import de.escalon.hypermedia.sample.model.store.OrderModel;
import de.escalon.hypermedia.sample.model.store.ProductModel;
import de.escalon.hypermedia.spring.AffordanceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

import static de.escalon.hypermedia.spring.AffordanceBuilder.linkTo;
import static de.escalon.hypermedia.spring.AffordanceBuilder.methodOn;

/**
 * Created by Dietrich on 17.02.2015.
 */
@ExposesResourceFor(Order.class)
@RequestMapping("/orders")
@Controller
public class OrderController {

    @Autowired
    private OrderBackend orderBackend;

    @Autowired
    private OrderAssembler orderAssembler;

    @Autowired
    private ProductController productController;


    @ResourceHandler(Cardinality.COLLECTION)
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> makeOrder(@Input(include = "productID") @RequestBody Product product) {

        OrderModel orderModel = orderBackend.createOrder();
        Product resolvedProduct = productController.getProduct(product.productID);
        orderModel = orderBackend.addOrderedItem(orderModel.getId(),
                new ProductModel(resolvedProduct.name, resolvedProduct.productID));
        AffordanceBuilder location = linkTo(methodOn(this.getClass()).getOrder(orderModel.getId()));
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(location.toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.SEE_OTHER);

    }

    @RequestMapping("/{orderId}")
    public ResponseEntity<Order> getOrder(@PathVariable int orderId) {
        OrderModel orderModel = orderBackend.getOrder(orderId);
        Order order = orderAssembler.toResource(orderModel);

        // offer extras for each product
        List<? extends Product> items = order.getItems();
        for (int i = 0; i < items.size(); i++) {
            Product product = items.get(i);
            Offer offer = new Offer();
            if (!product
                    .hasExtra("9052006")) {
                Offer shot = createAddOnOffer(product, "9052006", 0.2, orderId, i);
                offer.addOn(shot);
            }
            if (!product.hasExtra("9052007")) {
                Offer briocheCrema = createAddOnOffer(product, "9052007", 0.8, orderId, i);
                offer.addOn(briocheCrema);
            }
            if (!offer.getAddOns()
                    .isEmpty()) {
                product.addOffer(offer);
            }
        }

        order.add(linkTo(methodOn(PaymentController.class).makePayment(orderId)).withRel("paymentUrl"));
        return new ResponseEntity<Order>(order, HttpStatus.OK);
    }

    @RequestMapping(value = "/{orderId}/items/{orderedItemId}/addOns", method = RequestMethod.POST)
    public ResponseEntity<Void> orderAccessory(@PathVariable int orderId, @PathVariable int orderedItemId,
                                               @RequestBody @Input(include = "productID") Product product) {
        // TODO should write a productBackend to avoid this resolution nonsense:
        Product resolvedProduct = productController.getProduct(product.productID);<commit_msg>renamed /addOns to /accessories<commit_after>package de.escalon.hypermedia.sample.store;

import de.escalon.hypermedia.action.Cardinality;
import de.escalon.hypermedia.action.Input;
import de.escalon.hypermedia.action.ResourceHandler;
import de.escalon.hypermedia.affordance.TypedResource;
import de.escalon.hypermedia.sample.beans.store.Offer;
import de.escalon.hypermedia.sample.beans.store.Order;
import de.escalon.hypermedia.sample.beans.store.OrderedItem;
import de.escalon.hypermedia.sample.beans.store.Product;
import de.escalon.hypermedia.sample.model.store.OrderModel;
import de.escalon.hypermedia.sample.model.store.ProductModel;
import de.escalon.hypermedia.spring.AffordanceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

import static de.escalon.hypermedia.spring.AffordanceBuilder.linkTo;
import static de.escalon.hypermedia.spring.AffordanceBuilder.methodOn;

/**
 * Created by Dietrich on 17.02.2015.
 */
@ExposesResourceFor(Order.class)
@RequestMapping("/orders")
@Controller
public class OrderController {

    @Autowired
    private OrderBackend orderBackend;

    @Autowired
    private OrderAssembler orderAssembler;

    @Autowired
    private ProductController productController;


    @ResourceHandler(Cardinality.COLLECTION)
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> makeOrder(@Input(include = "productID") @RequestBody Product product) {

        OrderModel orderModel = orderBackend.createOrder();
        Product resolvedProduct = productController.getProduct(product.productID);
        orderModel = orderBackend.addOrderedItem(orderModel.getId(),
                new ProductModel(resolvedProduct.name, resolvedProduct.productID));
        AffordanceBuilder location = linkTo(methodOn(this.getClass()).getOrder(orderModel.getId()));
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(location.toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.SEE_OTHER);

    }

    @RequestMapping("/{orderId}")
    public ResponseEntity<Order> getOrder(@PathVariable int orderId) {
        OrderModel orderModel = orderBackend.getOrder(orderId);
        Order order = orderAssembler.toResource(orderModel);

        // offer extras for each product
        List<? extends Product> items = order.getItems();
        for (int i = 0; i < items.size(); i++) {
            Product product = items.get(i);
            Offer offer = new Offer();
            if (!product
                    .hasExtra("9052006")) {
                Offer shot = createAddOnOffer(product, "9052006", 0.2, orderId, i);
                offer.addOn(shot);
            }
            if (!product.hasExtra("9052007")) {
                Offer briocheCrema = createAddOnOffer(product, "9052007", 0.8, orderId, i);
                offer.addOn(briocheCrema);
            }
            if (!offer.getAddOns()
                    .isEmpty()) {
                product.addOffer(offer);
            }
        }

        order.add(linkTo(methodOn(PaymentController.class).makePayment(orderId)).withRel("paymentUrl"));
        return new ResponseEntity<Order>(order, HttpStatus.OK);
    }

    @RequestMapping(value = "/{orderId}/items/{orderedItemId}/accessories", method = RequestMethod.POST)
    public ResponseEntity<Void> orderAccessory(@PathVariable int orderId, @PathVariable int orderedItemId,
                                               @RequestBody @Input(include = "productID") Product product) {
        // TODO should write a productBackend to avoid this resolution nonsense:
        Product resolvedProduct = productController.getProduct(product.productID);
{'fraction_non_alphanumeric': 0.10546827426622823, 'fraction_numerical': 0.009986603336986968, 'mean_word_length': 3.5672969966629586, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155257', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2264, 'n_words': 414}