using Managing.Api.Models.Responses; using Managing.Application.Abstractions; using Managing.Application.Abstractions.Services; using Managing.Domain.Scenarios; using Managing.Domain.Strategies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using static Managing.Common.Enums; namespace Managing.Api.Controllers; /// /// Controller for managing scenarios and strategies within the application. /// Provides endpoints for creating, retrieving, and deleting scenarios and strategies. /// Requires authorization for access and produces JSON responses. /// [ApiController] [Authorize] [Route("[controller]")] [Produces("application/json")] public class ScenarioController : BaseController { private readonly IScenarioService _scenarioService; /// /// Initializes a new instance of the class. /// /// The service for managing scenarios. /// The service for user-related operations. public ScenarioController( IScenarioService scenarioService, IUserService userService) : base(userService) { _scenarioService = scenarioService; } /// /// Retrieves all scenarios for the authenticated user. /// /// A list of scenarios. [HttpGet] public async Task>> GetScenarios() { var user = await GetUser(); var scenarios = await _scenarioService.GetScenariosByUserAsync(user); var scenarioViewModels = scenarios.Select(MapToScenarioViewModel); return Ok(scenarioViewModels); } /// /// Creates a new scenario with the specified name and strategies for the authenticated user. /// /// The name of the scenario. /// A list of strategy names to include in the scenario. /// The created scenario. [HttpPost] public async Task> CreateScenario(string name, List strategies, int? loopbackPeriod = null) { var user = await GetUser(); var scenario = await _scenarioService.CreateScenarioForUser(user, name, strategies, loopbackPeriod); var scenarioViewModel = MapToScenarioViewModel(scenario); return Ok(scenarioViewModel); } /// /// Deletes a scenario by name for the authenticated user. /// /// The name of the scenario to delete. /// An ActionResult indicating the outcome of the operation. [HttpDelete] public async Task DeleteScenario(string name) { var user = await GetUser(); return Ok(await _scenarioService.DeleteScenarioByUser(user, name)); } // Update scenario [HttpPut] public async Task UpdateScenario(string name, List strategies, int? loopbackPeriod = null) { var user = await GetUser(); return Ok(await _scenarioService.UpdateScenarioByUser(user, name, strategies, loopbackPeriod)); } /// /// Retrieves all strategies for the authenticated user. /// /// A list of strategies. [HttpGet] [Route("indicator")] public async Task>> GetIndicators() { var user = await GetUser(); var indicators = await _scenarioService.GetIndicatorsAsync(); var indicatorViewModels = indicators.Select(MapToIndicatorViewModel); return Ok(indicatorViewModels); } /// /// Creates a new indicator with specified parameters for the authenticated user. /// /// The type of the indicator. /// The name of the indicator. /// The period for the indicator (optional). /// The fast periods for the indicator (optional). /// The slow periods for the indicator (optional). /// The signal periods for the indicator (optional). /// The multiplier for the indicator (optional). /// The stochastic periods for the indicator (optional). /// The smooth periods for the indicator (optional). /// The cycle periods for the indicator (optional). /// The created indicator. [HttpPost] [Route("indicator")] public async Task> CreateIndicator( IndicatorType indicatorType, string name, int? period = null, int? fastPeriods = null, int? slowPeriods = null, int? signalPeriods = null, double? multiplier = null, int? stochPeriods = null, int? smoothPeriods = null, int? cyclePeriods = null) { var user = await GetUser(); var indicator = await _scenarioService.CreateIndicatorForUser( user, indicatorType, name, period, fastPeriods, slowPeriods, signalPeriods, multiplier, stochPeriods, smoothPeriods, cyclePeriods); var indicatorViewModel = MapToIndicatorViewModel(indicator); return Ok(indicatorViewModel); } /// /// Deletes a indicator by name for the authenticated user. /// /// The name of the indicator to delete. /// An ActionResult indicating the outcome of the operation. [HttpDelete] [Route("indicator")] public async Task DeleteIndicator(string name) { var user = await GetUser(); return Ok(await _scenarioService.DeleteIndicatorByUser(user, name)); } // Update indicator [HttpPut] [Route("indicator")] public async Task Updateindicator( IndicatorType indicatorType, string name, int? period = null, int? fastPeriods = null, int? slowPeriods = null, int? signalPeriods = null, double? multiplier = null, int? stochPeriods = null, int? smoothPeriods = null, int? cyclePeriods = null) { var user = await GetUser(); return Ok(await _scenarioService.UpdateIndicatorByUser( user, indicatorType, name, period, fastPeriods, slowPeriods, signalPeriods, multiplier, stochPeriods, smoothPeriods, cyclePeriods)); } private static ScenarioViewModel MapToScenarioViewModel(Scenario scenario) { return new ScenarioViewModel { Name = scenario.Name, LoopbackPeriod = scenario.LookbackPeriod, UserName = scenario.User?.Name, Indicators = scenario.Indicators?.Select(MapToIndicatorViewModel).ToList() ?? new List() }; } private static IndicatorViewModel MapToIndicatorViewModel(IndicatorBase indicatorBase) { return new IndicatorViewModel { Name = indicatorBase.Name, Type = indicatorBase.Type, SignalType = indicatorBase.SignalType, MinimumHistory = indicatorBase.MinimumHistory, Period = indicatorBase.Period, FastPeriods = indicatorBase.FastPeriods, SlowPeriods = indicatorBase.SlowPeriods, SignalPeriods = indicatorBase.SignalPeriods, Multiplier = indicatorBase.Multiplier, SmoothPeriods = indicatorBase.SmoothPeriods, StochPeriods = indicatorBase.StochPeriods, CyclePeriods = indicatorBase.CyclePeriods, UserName = indicatorBase.User?.Name }; } }