You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
4.5 KiB
123 lines
4.5 KiB
using Kean.Application.Command.ViewModels;
|
|
using Kean.Application.Query.ViewModels;
|
|
using Kean.Domain;
|
|
using Kean.Domain.Basic.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Kean.Presentation.Rest.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Item服务
|
|
/// </summary>
|
|
[ApiController, Route("api/items")]
|
|
public class ItemsController : ControllerBase
|
|
{
|
|
private readonly Application.Command.Interfaces.IBasicService _basicCommandService; // 基础信息命令服务
|
|
private readonly Application.Query.Interfaces.IBasicService _basicQueryService; // 基础信息查询服务
|
|
|
|
/// <summary>
|
|
/// 依赖注入
|
|
/// </summary>
|
|
public ItemsController(
|
|
Application.Command.Interfaces.IBasicService basicCommandService,
|
|
Application.Query.Interfaces.IBasicService basicQueryService)
|
|
{
|
|
_basicCommandService = basicCommandService;
|
|
_basicQueryService = basicQueryService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取item列表
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
[HttpGet]
|
|
[ProducesResponseType(200)]
|
|
public async Task<IActionResult> GetList(
|
|
[FromQuery] string name,
|
|
[FromQuery] string sort,
|
|
[FromQuery] int? offset,
|
|
[FromQuery] int? limit)
|
|
{
|
|
var items = await _basicQueryService.GetItemList(name, sort, offset, limit);
|
|
if (offset.HasValue || limit.HasValue)
|
|
{
|
|
var total = await _basicQueryService.GetItemCount(name);
|
|
return StatusCode(200, new { items, total });
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(200, new { items, total = items.Count() });
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建Item
|
|
/// </summary>
|
|
/// <response code="201">成功</response>
|
|
/// <response code="409">信息已存在</response>
|
|
/// <response code="410">信息已删除</response>
|
|
/// <response code="422">请求内容错误</response>
|
|
[HttpPost]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(409)]
|
|
[ProducesResponseType(410)]
|
|
[ProducesResponseType(422)]
|
|
public async Task<IActionResult> CreateItem(Kean.Application.Command.ViewModels.Item item)
|
|
{
|
|
var result = await _basicCommandService.CreateItem(item);
|
|
return result switch
|
|
{
|
|
{ Id: > 0 } => StatusCode(201, result.Id),
|
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
|
_ => StatusCode(422, result.Failure)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改Item
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
/// <response code="409">信息已存在</response>
|
|
/// <response code="410">信息已删除</response>
|
|
/// <response code="422">请求内容错误</response>
|
|
[HttpPut("{code}")]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(409)]
|
|
[ProducesResponseType(410)]
|
|
[ProducesResponseType(422)]
|
|
public async Task<IActionResult> ModifyItem(string code, Kean.Application.Command.ViewModels.Item item)
|
|
{
|
|
item.ItemName = code;
|
|
var result = await _basicCommandService.ModifyItem(item);
|
|
return result switch
|
|
{
|
|
{ Success: true } => StatusCode(200),
|
|
{ Failure.ErrorCode: nameof(ErrorCode.Conflict) } => StatusCode(409, result.Failure),
|
|
{ Failure.ErrorCode: nameof(ErrorCode.Gone) } => StatusCode(410, result.Failure),
|
|
_ => StatusCode(422, result.Failure)
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量处理物料
|
|
/// </summary>
|
|
/// <response code="200">成功</response>
|
|
/// <response code="405">方法不支持</response>
|
|
[HttpPost("batch")]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(405)]
|
|
public async Task<IActionResult> BatchItem(Batch<string> batch)
|
|
{
|
|
return batch.Method switch
|
|
{
|
|
BatchMethod.Delete => StatusCode(200, await _basicCommandService.DeleteItem(batch.Data)),
|
|
_ => StatusCode(405)
|
|
};
|
|
}
|
|
|
|
}
|
|
}
|