Files
managing-apps/src/Managing.Api/Controllers/MoneyManagementController.cs
Oda 422fecea7b Postgres (#30)
* Add postgres

* Migrate users

* Migrate geneticRequest

* Try to fix Concurrent call

* Fix asyncawait

* Fix async and concurrent

* Migrate backtests

* Add cache for user by address

* Fix backtest migration

* Fix not open connection

* Fix backtest command error

* Fix concurrent

* Fix all concurrency

* Migrate TradingRepo

* Fix scenarios

* Migrate statistic repo

* Save botbackup

* Add settings et moneymanagement

* Add bot postgres

* fix a bit more backups

* Fix bot model

* Fix loading backup

* Remove cache market for read positions

* Add workers to postgre

* Fix workers api

* Reduce get Accounts for workers

* Migrate synth to postgre

* Fix backtest saved

* Remove mongodb

* botservice decorrelation

* Fix tradingbot scope call

* fix tradingbot

* fix concurrent

* Fix scope for genetics

* Fix account over requesting

* Fix bundle backtest worker

* fix a lot of things

* fix tab backtest

* Remove optimized moneymanagement

* Add light signal to not use User and too much property

* Make money management lighter

* insert indicators to awaitable

* Migrate add strategies to await

* Refactor scenario and indicator retrieval to use asynchronous methods throughout the application

* add more async await

* Add services

* Fix and clean

* Fix bot a bit

* Fix bot and add message for cooldown

* Remove fees

* Add script to deploy db

* Update dfeeploy script

* fix script

* Add idempotent script and backup

* finish script migration

* Fix did user and agent name on start bot
2025-07-27 20:42:17 +07:00

126 lines
4.5 KiB
C#

using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Domain.MoneyManagements;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Managing.Api.Controllers;
/// <summary>
/// Controller for managing money management strategies.
/// Provides endpoints for creating, retrieving, updating, and deleting money management strategies.
/// Requires authorization for access and produces JSON responses.
/// </summary>
[ApiController]
[Authorize]
[Route("[controller]")]
[Produces("application/json")]
public class MoneyManagementController : BaseController
{
private readonly IMoneyManagementService _moneyManagementService;
/// <summary>
/// Initializes a new instance of the <see cref="MoneyManagementController"/> class.
/// </summary>
/// <param name="moneyManagementService">The service for managing money management strategies.</param>
/// <param name="userService">The service for user-related operations.</param>
public MoneyManagementController(
IMoneyManagementService moneyManagementService,
IUserService userService)
: base(userService)
{
_moneyManagementService = moneyManagementService;
}
/// <summary>
/// Creates a new money management strategy or updates an existing one for the authenticated user.
/// </summary>
/// <param name="moneyManagement">The money management strategy to create or update.</param>
/// <returns>The created or updated money management strategy.</returns>
[HttpPost]
public async Task<ActionResult<MoneyManagement>> PostMoneyManagement(MoneyManagement moneyManagement)
{
try
{
var user = await GetUser();
var result = await _moneyManagementService.CreateOrUpdateMoneyManagement(user, moneyManagement);
return Ok(result);
}
catch (Exception ex)
{
return StatusCode(500, $"Error creating/updating money management: {ex.Message}");
}
}
/// <summary>
/// Retrieves all money management strategies for the authenticated user.
/// </summary>
/// <returns>A list of money management strategies.</returns>
[HttpGet]
[Route("moneymanagements")]
public async Task<ActionResult<IEnumerable<MoneyManagement>>> GetMoneyManagements()
{
try
{
var user = await GetUser();
var moneyManagements = await _moneyManagementService.GetMoneyMangements(user);
return Ok(moneyManagements);
}
catch (Exception ex)
{
return StatusCode(500, $"Error retrieving money managements: {ex.Message}");
}
}
/// <summary>
/// Retrieves a specific money management strategy by name for the authenticated user.
/// </summary>
/// <param name="name">The name of the money management strategy to retrieve.</param>
/// <returns>The requested money management strategy if found.</returns>
[HttpGet]
public async Task<ActionResult<MoneyManagement>> GetMoneyManagement(string name)
{
try
{
var user = await GetUser();
var result = await _moneyManagementService.GetMoneyMangement(user, name);
if (result == null)
{
return NotFound($"Money management strategy '{name}' not found");
}
return Ok(result);
}
catch (Exception ex)
{
return StatusCode(500, $"Error retrieving money management: {ex.Message}");
}
}
/// <summary>
/// Deletes a specific money management strategy by name for the authenticated user.
/// </summary>
/// <param name="name">The name of the money management strategy to delete.</param>
/// <returns>An ActionResult indicating the outcome of the operation.</returns>
[HttpDelete]
public async Task<ActionResult> DeleteMoneyManagement(string name)
{
try
{
var user = await GetUser();
var result = await _moneyManagementService.DeleteMoneyManagement(user, name);
if (!result)
{
return NotFound($"Money management strategy '{name}' not found or could not be deleted");
}
return Ok(new { success = true, message = $"Money management strategy '{name}' deleted successfully" });
}
catch (Exception ex)
{
return StatusCode(500, $"Error deleting money management: {ex.Message}");
}
}
}