230 lines
7.4 KiB
C#
230 lines
7.4 KiB
C#
using AssetManager.Data;
|
|
using AssetManager.Models.DTOs;
|
|
using AssetManager.Models;
|
|
using AssetManager.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Security.Claims;
|
|
|
|
namespace AssetManager.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/v1/strategy")]
|
|
[Authorize]
|
|
public class StrategyController : ControllerBase
|
|
{
|
|
private readonly ILogger<StrategyController> _logger;
|
|
private readonly IStrategyService _strategyService;
|
|
|
|
public StrategyController(ILogger<StrategyController> logger, IStrategyService strategyService)
|
|
{
|
|
_logger = logger;
|
|
_strategyService = strategyService;
|
|
}
|
|
|
|
private string GetCurrentUserId()
|
|
{
|
|
return User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? throw new Exception("User not authenticated");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取策略列表
|
|
/// </summary>
|
|
/// <returns>策略列表</returns>
|
|
/// <remarks>
|
|
/// 此接口用于获取当前登录用户的所有策略列表。
|
|
/// </remarks>
|
|
[HttpGet("strategies")]
|
|
public ActionResult<ApiResponse<List<Strategy>>> GetStrategies()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Request to get strategies");
|
|
|
|
var userId = GetCurrentUserId();
|
|
var strategies = _strategyService.GetStrategies(userId);
|
|
|
|
_logger.LogInformation("Strategies retrieved successfully");
|
|
|
|
return Ok(new ApiResponse<List<Strategy>>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.Success,
|
|
data = strategies,
|
|
message = "Success"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error retrieving strategies");
|
|
|
|
return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<List<Strategy>>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.InternalServerError,
|
|
data = null,
|
|
message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单个策略详情
|
|
/// </summary>
|
|
/// <param name="id">策略ID</param>
|
|
/// <returns>策略详情</returns>
|
|
/// <remarks>
|
|
/// 此接口用于获取指定策略的详细信息。
|
|
/// </remarks>
|
|
[HttpGet("{id}")]
|
|
public ActionResult<ApiResponse<Strategy>> GetStrategyById(string id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation($"Request to get strategy by id: {id}");
|
|
|
|
var userId = GetCurrentUserId();
|
|
var strategy = _strategyService.GetStrategyById(id, userId);
|
|
|
|
_logger.LogInformation($"Strategy {id} retrieved successfully");
|
|
|
|
return Ok(new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.Success,
|
|
data = strategy,
|
|
message = "Success"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error retrieving strategy {id}");
|
|
|
|
return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.InternalServerError,
|
|
data = null,
|
|
message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建新策略
|
|
/// </summary>
|
|
/// <param name="request">策略创建请求参数</param>
|
|
/// <returns>创建的策略详情</returns>
|
|
/// <remarks>
|
|
/// 此接口用于创建新的策略。
|
|
/// </remarks>
|
|
[HttpPost]
|
|
public ActionResult<ApiResponse<Strategy>> CreateStrategy([FromBody] CreateStrategyRequest request)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation($"Request to create strategy: {request.name}");
|
|
|
|
var userId = GetCurrentUserId();
|
|
var strategy = _strategyService.CreateStrategy(request, userId);
|
|
|
|
_logger.LogInformation($"Strategy {strategy.Id} created successfully");
|
|
|
|
return Ok(new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.Success,
|
|
data = strategy,
|
|
message = "Strategy created successfully"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error creating strategy");
|
|
|
|
return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.InternalServerError,
|
|
data = null,
|
|
message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新策略
|
|
/// </summary>
|
|
/// <param name="id">策略ID</param>
|
|
/// <param name="request">策略更新请求参数</param>
|
|
/// <returns>更新后的策略详情</returns>
|
|
/// <remarks>
|
|
/// 此接口用于更新指定策略的信息。
|
|
/// </remarks>
|
|
[HttpPut("{id}")]
|
|
public ActionResult<ApiResponse<Strategy>> UpdateStrategy(string id, [FromBody] UpdateStrategyRequest request)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation($"Request to update strategy: {id}");
|
|
|
|
var userId = GetCurrentUserId();
|
|
var strategy = _strategyService.UpdateStrategy(id, request, userId);
|
|
|
|
_logger.LogInformation($"Strategy {id} updated successfully");
|
|
|
|
return Ok(new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.Success,
|
|
data = strategy,
|
|
message = "Strategy updated successfully"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error updating strategy {id}");
|
|
|
|
return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<Strategy>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.InternalServerError,
|
|
data = null,
|
|
message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除策略
|
|
/// </summary>
|
|
/// <param name="id">策略ID</param>
|
|
/// <returns>删除结果</returns>
|
|
/// <remarks>
|
|
/// 此接口用于删除指定的策略。
|
|
/// </remarks>
|
|
[HttpDelete("{id}")]
|
|
public ActionResult<ApiResponse<object>> DeleteStrategy(string id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation($"Request to delete strategy: {id}");
|
|
|
|
var userId = GetCurrentUserId();
|
|
var success = _strategyService.DeleteStrategy(id, userId);
|
|
|
|
_logger.LogInformation($"Strategy {id} deleted successfully");
|
|
|
|
return Ok(new ApiResponse<object>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.Success,
|
|
data = new { id = id, status = "deleted" },
|
|
message = "Strategy deleted successfully"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error deleting strategy {id}");
|
|
|
|
return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<object>
|
|
{
|
|
code = AssetManager.Models.StatusCodes.InternalServerError,
|
|
data = null,
|
|
message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
} |