package io.reactivesw.order.zone.application.controller;

import io.reactivesw.common.model.UpdateRequest;
import io.reactivesw.order.zone.application.model.Zone;
import io.reactivesw.order.zone.application.model.mapper.ZoneMapper;
import io.reactivesw.order.zone.domain.entity.ZoneEntity;
import io.reactivesw.order.zone.domain.service.ZoneService;
import io.reactivesw.route.ZoneRouter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by umasuo on 16/12/20.
 */
@RestController
public class ZoneController {

  /**
   * logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(ZoneController.class);

  /**
   * zone service.
   */
  @Autowired
  private transient ZoneService service;

  /**
   * get by id.
   *
   * @param id
   * @return Zone
   */
  @GetMapping(ZoneRouter.ZONE_WITH_ID)
  public Zone getZoneById(@PathVariable String id) {
    LOG.info("enter: id: {}", id);

    ZoneEntity entity = service.getById(id);

    LOG.info("exit: entity: {}", entity);
    return ZoneMapper.entityToModel(entity);
  }

  /**
   * get zone by location.
   *
   * @param country String
   * @param state   String
   * @return List of Zone
   */
  @GetMapping(ZoneRouter.ZONE_BASE_URL)
  public List<Zone> getByLocation(@RequestParam(name = "country") String country,
                                  @RequestParam(required = false, name = "state") String state) {
    List<ZoneEntity> zoneEntities = service.getByLocation(country, state);

    List<Zone> zones = zoneEntities.parallelStream().map(
        zoneEntity -> ZoneMapper.entityToModel(zoneEntity)
    ).collect(Collectors.toList());

    return zones;
  }

  /**
   * update zone.
   *
   * @param id            zone id String
   * @param updateRequest update request
   * @return updated Zone
   */
  @PutMapping(ZoneRouter.ZONE_WITH_ID)
  public Zone updateZone(@PathVariable String id,
                         @RequestBody UpdateRequest updateRequest) {
    LOG.info("enter: id: {}, UpdateRequest: {}", id, updateRequest);

    ZoneEntity entity = service.updateZone(id, updateRequest.getVersion(), updateRequest
        .getActions());

    LOG.info("exit: ZoneEntity: {}", entity);
    return ZoneMapper.entityToModel(entity);
  }

  /**
   * delete a zone.
   *
   * @param id      String
   * @param version Integer
   */
  public void deleteZone(@PathVariable String id, @RequestParam Integer version) {
    LOG.info("enter: id: {}, version: {}", id, version);

    service.deleteById(id, version);
  }
}

@@ -378,24 +378,108 @@
 ZoneRouter;%0A
+import io.swagger.annotations.ApiOperation;%0Aimport io.swagger.annotations.ApiParam;%0A
 import org.s
@@ -586,16 +586,78 @@
 owired;%0A
+import org.springframework.web.bind.annotation.DeleteMapping;%0A
 import o
@@ -1414,32 +1414,61 @@
 turn Zone%0A   */%0A
+  @ApiOperation(%22Get by id%22)%0A
   @GetMapping(Zo
@@ -1521,32 +1521,53 @@
 Id(@PathVariable
+ @ApiParam(%22zone id%22)
  String id) %7B%0A  
@@ -1862,16 +1862,68 @@
 e%0A   */%0A
+  @ApiOperation(%22Get by locatino: country & state%22)%0A
   @GetMa
@@ -2020,22 +2020,81 @@
 untry%22) 
-String
+@ApiParam(%22country%22) String%0A                                     
  country
@@ -2176,16 +2176,95 @@
 %22state%22)
+%0A                                  @ApiParam(value = %22state%22, required = false)
  String 
@@ -2668,16 +2668,55 @@
 e%0A   */%0A
+  @ApiOperation(%22Update zone with id%22)%0A
   @PutMa
@@ -2776,32 +2776,53 @@
 ne(@PathVariable
+ @ApiParam(%22zone id%22)
  String id,%0A    
@@ -2855,16 +2855,103 @@
 estBody 
+@ApiParam(%22update request with version & update actions%22)%0A                             
 UpdateRe
@@ -3351,69 +3351,227 @@
 /%0A  
-public void deleteZone(@PathVariable String id, @RequestParam
+@ApiOperation(%22delete zone with id & version%22)%0A  @DeleteMapping(ZoneRouter.ZONE_WITH_ID)%0A  public void deleteZone(@PathVariable @ApiParam(%22zone id%22) String id,%0A                         @RequestParam @ApiParam(%22version%22)
  Int

add swagger