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; /// /// 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. /// [ApiController] [Authorize] [Route("[controller]")] [Produces("application/json")] public class MoneyManagementController : BaseController { private readonly IMoneyManagementService _moneyManagementService; /// /// Initializes a new instance of the class. /// /// The service for managing money management strategies. /// The service for user-related operations. public MoneyManagementController( IMoneyManagementService moneyManagementService, IUserService userService) : base(userService) { _moneyManagementService = moneyManagementService; } /// /// Creates a new money management strategy or updates an existing one for the authenticated user. /// /// The money management strategy to create or update. /// The created or updated money management strategy. [HttpPost] public async Task> 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}"); } } /// /// Retrieves all money management strategies for the authenticated user. /// /// A list of money management strategies. [HttpGet] [Route("moneymanagements")] public async Task>> 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}"); } } /// /// Retrieves a specific money management strategy by name for the authenticated user. /// /// The name of the money management strategy to retrieve. /// The requested money management strategy if found. [HttpGet] public async Task> 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}"); } } /// /// Deletes a specific money management strategy by name for the authenticated user. /// /// The name of the money management strategy to delete. /// An ActionResult indicating the outcome of the operation. [HttpDelete] public async Task 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}"); } } }