From 9b228e9c0594be73217b9a99ecba5f25cbc89f39 Mon Sep 17 00:00:00 2001 From: claw_bot Date: Tue, 10 Mar 2026 10:02:24 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=A0=E9=99=A4=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=8E=A5=E5=8F=A3=EF=BC=9A=E6=94=AF=E6=8C=81DELETE=20?= =?UTF-8?q?/api/v1/portfolio/{id}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PortfolioController.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/AssetManager.API/Controllers/PortfolioController.cs b/AssetManager.API/Controllers/PortfolioController.cs index 715eb16..5b64834 100644 --- a/AssetManager.API/Controllers/PortfolioController.cs +++ b/AssetManager.API/Controllers/PortfolioController.cs @@ -501,4 +501,94 @@ public class PortfolioController : ControllerBase }); } } + + /// + /// 删除投资组合 + /// + /// 投资组合ID + /// 删除结果 + [HttpDelete("{id}")] + public async Task>> DeletePortfolio(string id) + { + try + { + var userId = GetCurrentUserId(); + if (string.IsNullOrEmpty(userId)) + { + return Unauthorized(new ApiResponse + { + code = AssetManager.Models.StatusCodes.Unauthorized, + data = false, + message = "用户未授权" + }); + } + + _logger.LogInformation($"Request to delete portfolio: {id}"); + + var db = _databaseService.GetDb(); + + // 校验组合是否存在且属于当前用户 + var portfolio = db.Queryable() + .Where(p => p.Id == id && p.UserId == userId) + .First(); + + if (portfolio == null) + { + return NotFound(new ApiResponse + { + code = AssetManager.Models.StatusCodes.NotFound, + data = false, + message = "投资组合不存在或无权限删除" + }); + } + + // 开启事务删除相关数据 + db.Ado.BeginTran(); + try + { + // 删除交易记录 + db.Deleteable() + .Where(t => t.PortfolioId == id) + .ExecuteCommand(); + + // 删除持仓 + db.Deleteable() + .Where(p => p.PortfolioId == id) + .ExecuteCommand(); + + // 删除组合本身 + db.Deleteable() + .Where(p => p.Id == id) + .ExecuteCommand(); + + db.Ado.CommitTran(); + } + catch (Exception ex) + { + db.Ado.RollbackTran(); + _logger.LogError(ex, "删除投资组合事务失败"); + throw; + } + + _logger.LogInformation($"Portfolio {id} deleted successfully"); + + return Ok(new ApiResponse + { + code = AssetManager.Models.StatusCodes.Success, + data = true, + message = "删除成功" + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error deleting portfolio"); + + return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse + { + code = AssetManager.Models.StatusCodes.InternalServerError, + data = false, + message = ex.Message + }); + } + } }