152 lines
5.0 KiB
C#
152 lines
5.0 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using QYZH.InteractiveMagazine.IService;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
|||
|
|
|
|||
|
|
namespace QYZH.InteractiveMagazine.WebApi.Controllers;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 商品管理控制器
|
|||
|
|
/// </summary>
|
|||
|
|
[Route("api/[controller]")]
|
|||
|
|
[ApiController]
|
|||
|
|
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
|
|||
|
|
public class ProductController : BaseController
|
|||
|
|
{
|
|||
|
|
private readonly IProductService _productService;
|
|||
|
|
private readonly ILogger<ProductController> _logger;
|
|||
|
|
|
|||
|
|
public ProductController(IProductService productService, ILogger<ProductController> logger)
|
|||
|
|
{
|
|||
|
|
_productService = productService;
|
|||
|
|
_logger = logger;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 创建商品
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input">商品信息</param>
|
|||
|
|
/// <returns>创建的商品信息</returns>
|
|||
|
|
[HttpPost]
|
|||
|
|
public async Task<BaseResponse<ProductOutput>> CreateAsync([FromBody] ProductInput input)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = await _productService.CreateAsync(input);
|
|||
|
|
return Success(result, "创建商品成功");
|
|||
|
|
}
|
|||
|
|
catch (BusinessException ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning(ex, "创建商品业务异常: {Message}", ex.Message);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "创建商品系统异常,参数:{Input}", input);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail("创建商品失败,请稍后重试");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 更新商品
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">商品ID</param>
|
|||
|
|
/// <param name="input">商品信息</param>
|
|||
|
|
/// <returns>更新后的商品信息</returns>
|
|||
|
|
[HttpPut("{id}")]
|
|||
|
|
public async Task<BaseResponse<ProductOutput>> UpdateAsync(long id, [FromBody] ProductInput input)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = await _productService.UpdateAsync(id, input);
|
|||
|
|
return Success(result, "更新商品成功");
|
|||
|
|
}
|
|||
|
|
catch (BusinessException ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning(ex, "更新商品业务异常: {Message}", ex.Message);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "更新商品系统异常,ID:{Id},参数:{Input}", id, input);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail("更新商品失败,请稍后重试");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除商品
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">商品ID</param>
|
|||
|
|
/// <returns>操作结果</returns>
|
|||
|
|
[HttpDelete("{id}")]
|
|||
|
|
public async Task<BaseResponse<object>> DeleteAsync(long id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
await _productService.DeleteAsync(id);
|
|||
|
|
return Success(new object(), "删除商品成功");
|
|||
|
|
}
|
|||
|
|
catch (BusinessException ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning(ex, "删除商品业务异常: {Message}", ex.Message);
|
|||
|
|
return BaseResponse<object>.Fail(ex.Message);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "删除商品系统异常,ID:{Id}", id);
|
|||
|
|
return BaseResponse<object>.Fail("删除商品失败,请稍后重试");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 根据ID获取商品
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id">商品ID</param>
|
|||
|
|
/// <returns>商品信息</returns>
|
|||
|
|
[HttpGet("{id}")]
|
|||
|
|
public async Task<BaseResponse<ProductOutput>> GetByIdAsync(long id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = await _productService.GetByIdAsync(id);
|
|||
|
|
return Success(result);
|
|||
|
|
}
|
|||
|
|
catch (BusinessException ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning(ex, "获取商品业务异常: {Message}", ex.Message);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail(ex.Message);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "获取商品系统异常,ID:{Id}", id);
|
|||
|
|
return BaseResponse<ProductOutput>.Fail("获取商品信息失败,请稍后重试");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 分页查询商品列表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="input">查询条件</param>
|
|||
|
|
/// <returns>分页结果</returns>
|
|||
|
|
[HttpPost("list")]
|
|||
|
|
public async Task<BaseResponse<PageListModel<ProductOutput>>> GetListAsync([FromBody] ProductQueryInput input)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = await _productService.GetListAsync(input);
|
|||
|
|
return Success(result);
|
|||
|
|
}
|
|||
|
|
catch (BusinessException ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning(ex, "查询商品列表业务异常: {Message}", ex.Message);
|
|||
|
|
return BaseResponse<PageListModel<ProductOutput>>.Fail(ex.Message);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "查询商品列表系统异常,参数:{Input}", input);
|
|||
|
|
return BaseResponse<PageListModel<ProductOutput>>.Fail("查询商品列表失败,请稍后重试");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|