package io.reactivesw.catalog.inventory.application.controller;

import static io.reactivesw.route.InventoryEntryRouter.INVENTORY_ENTRY_ID;
import static io.reactivesw.route.InventoryEntryRouter.INVENTORY_ENTRY_ROOT;
import static io.reactivesw.route.InventoryEntryRouter.INVENTORY_ENTRY_WITH_ID;

import io.reactivesw.catalog.inventory.application.model.InventoryEntry;
import io.reactivesw.catalog.inventory.application.model.InventoryEntryDraft;
import io.reactivesw.catalog.inventory.domain.service.InventoryEntryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Davis on 16/12/21.
 */
@RestController
public class InventoryEntryController {
  /**
   * log.
   */
  private static final Logger LOG = LoggerFactory.getLogger(InventoryEntryController.class);

  /**
   * The Inventory entry service.
   */
  @Autowired
  private transient InventoryEntryService inventoryEntryService;

  /**
   * Create inventory entry inventory entry.
   *
   * @param draft the draft
   * @return the inventory entry
   */
  @ApiOperation("create InventoryEntry")
  @PostMapping(INVENTORY_ENTRY_ROOT)
  public InventoryEntry createInventoryEntry(InventoryEntryDraft draft) {
    LOG.debug("enter createInventoryEntry, inventory entry draft is : {}", draft.toString());

    InventoryEntry result = inventoryEntryService.createInventoryEntry(draft);

    LOG.debug("end createInventoryEntry, new InventoryEntry is : {}", result.toString());

    return result;
  }

  /**
   * Delete inventory entry.
   *
   * @param id      the id
   * @param version the version
   */
  @ApiOperation("delete InventoryEntry")
  @DeleteMapping(INVENTORY_ENTRY_WITH_ID)
  public void deleteInventoryEntry(@PathVariable(value = INVENTORY_ENTRY_ID)
                                   @ApiParam(value = "InventoryEntry Id", required = true)
                                       String id,
                                   @RequestBody
                                   @ApiParam(value = "InventoryEntry Version", required = true)
                                       Integer version) {
    LOG.debug("enter deleteInventoryEntry, id is : {}, version is : {}", id, version);

    inventoryEntryService.deleteInventoryEntry(id, version);

    LOG.debug("end deleteInventoryEntry, id is : {}, version is : {}", id, version);
  }

  /**
   * Gets inventory entry by id.
   *
   * @param id the id
   * @return the inventory entry by id
   */
  @ApiOperation("get InventoryEntry by Id")
  @GetMapping(INVENTORY_ENTRY_WITH_ID)
  public InventoryEntry getInventoryEntryById(@PathVariable(value = INVENTORY_ENTRY_ID)
                                              @ApiParam(value = "InventoryEntry ID",
                                                  required = true)
                                                  String id) {
    LOG.debug("enter getInventoryEntryById, id is : {}", id);

    InventoryEntry result = inventoryEntryService.getInventoryEntryById(id);

    LOG.debug("end getInventoryEntryById, get result is : {}", result.toString());

    return result;
  }
}

@@ -519,16 +519,65 @@
 ervice;%0A
+import io.reactivesw.common.model.UpdateRequest;%0A
 import i
@@ -1007,32 +1007,91 @@
 on.PostMapping;%0A
+import org.springframework.web.bind.annotation.PutMapping;%0A
 import org.sprin
@@ -2945,24 +2945,1197 @@
 sion);%0A  %7D%0A%0A
+  /**%0A   * Update InventoryEntry.%0A   *%0A   * @param id            the id%0A   * @param updateRequest the update request%0A   * @return the inventory entry%0A   */%0A  @ApiOperation(value = %22Update InventoryEntry%22)%0A  @PutMapping(INVENTORY_ENTRY_WITH_ID)%0A  public InventoryEntry updateInventoryEntry(@PathVariable(value = INVENTORY_ENTRY_ID)%0A                                             @ApiParam(value = %22InventoryEntry ID%22, required = true)%0A                                                 String id,%0A                                             @RequestBody%0A                                             @ApiParam(value = %22InventoryEntry Update Request%22,%0A                                                 required = true)%0A                                                 UpdateRequest updateRequest) %7B%0A    LOG.debug(%22enter updateInventoryEntry, id is %7B%7D, update InventoryEntry is %7B%7D%22,%0A        id, updateRequest.toString());%0A%0A    InventoryEntry result = inventoryEntryService.updateInventoryEntry(id,%0A        updateRequest.getVersion(), updateRequest.getActions());%0A%0A    LOG.debug(%22end updateInventoryEntry, updated InventoryEntry is %7B%7D%22, result.toString());%0A%0A    return result;%0A  %7D%0A%0A
   /**%0A   * G

add Update method to inventoryEntryController