first commit
This commit is contained in:
@@ -0,0 +1,365 @@
|
||||
using Fitness_core_BE.Helpers;
|
||||
using Fitness_core_BE.Models.IOs;
|
||||
using Fitness_core_BE.Services.FunctionHandler;
|
||||
using Fitness_core_BE.Services.Document;
|
||||
using Fitness_core_BE.Interfaces.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
using System.Text.Json;
|
||||
using static Fitness_core_BE.Helpers.JwtMiddleware;
|
||||
using Fitness_core_BE.DTOs;
|
||||
|
||||
namespace Fitness_core_BE.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Main API Controller for handling various operations
|
||||
/// </summary>
|
||||
[Route("api")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[EnableCors("AllowAll")]
|
||||
public class APIController : ControllerBase
|
||||
{
|
||||
private readonly TicketFuncHandler _ticketFuncHandler;
|
||||
private readonly ActivityLogFuncHandler _activityLogFuncHandler;
|
||||
private readonly ClientFuncHandler _clientFuncHandler;
|
||||
private readonly StaffFunctionHandler _staffFuncHandler;
|
||||
private readonly ClientInvoiceFunctionHandler _clientInvoiceFuncHandler;
|
||||
private readonly CustomerFunctionHandler _customerFuncHandler;
|
||||
private readonly CustomerInvoiceFunctionHandler _customerInvoiceFuncHandler;
|
||||
private readonly CustomerPlanFunctionHandler _customerPlanFuncHandler;
|
||||
private readonly CustomerSubscriptionFunctionHandler _customerSubscriptionFuncHandler;
|
||||
private readonly MealFunctionHandler _mealFuncHandler;
|
||||
private readonly AuthFuncHandler _authFuncHandler;
|
||||
private readonly WorkoutFunctionHandler _workoutFuncHandler;
|
||||
private readonly DocumentFunctionHandler _documentFuncHandler;
|
||||
private readonly ChatFunctionHandler _chatFuncHandler;
|
||||
private readonly FeedFuncHandler _feedFuncHandler;
|
||||
private readonly AccessDeviceFunctionHandler _accessDeviceFuncHandler;
|
||||
private readonly AttendanceFunctionHandler _attendanceFuncHandler;
|
||||
private readonly DocumentFunctionHandler _docHandler;
|
||||
private readonly IDocumentService _docService;
|
||||
private readonly UserRegisterHandler _userRegisterHandler;
|
||||
private readonly ClientStatFuncHandler _clientStatFuncHandler;
|
||||
private readonly PaymentFunctionHandler _paymentFuncHandler;
|
||||
|
||||
public APIController(
|
||||
TicketFuncHandler ticketFuncHandler,
|
||||
ActivityLogFuncHandler activityLogFuncHandler,
|
||||
ClientFuncHandler clientFuncHandler,
|
||||
AuthFuncHandler authFuncHandler,
|
||||
StaffFunctionHandler staffFuncHandler,
|
||||
ClientInvoiceFunctionHandler clientInvoiceFuncHandler,
|
||||
CustomerFunctionHandler customerFuncHandler,
|
||||
CustomerInvoiceFunctionHandler customerInvoiceFuncHandler,
|
||||
CustomerPlanFunctionHandler customerPlanFuncHandler,
|
||||
CustomerSubscriptionFunctionHandler customerSubscriptionFuncHandler,
|
||||
MealFunctionHandler mealFuncHandler,
|
||||
WorkoutFunctionHandler workoutFuncHandler,
|
||||
DocumentFunctionHandler documentFuncHandler,
|
||||
FeedFuncHandler feedFuncHandler,
|
||||
AccessDeviceFunctionHandler accessDeviceFuncHandler,
|
||||
AttendanceFunctionHandler attendanceFuncHandler,
|
||||
ChatFunctionHandler chatFunctionHandler,
|
||||
DocumentFunctionHandler docHandler,
|
||||
IDocumentService docService,
|
||||
UserRegisterHandler userRegisterHandler,
|
||||
ClientStatFuncHandler clientStatFuncHandler,
|
||||
PaymentFunctionHandler paymentFuncHandler)
|
||||
{
|
||||
_ticketFuncHandler = ticketFuncHandler;
|
||||
_activityLogFuncHandler = activityLogFuncHandler;
|
||||
_clientFuncHandler = clientFuncHandler;
|
||||
_staffFuncHandler = staffFuncHandler;
|
||||
_clientInvoiceFuncHandler = clientInvoiceFuncHandler;
|
||||
_customerFuncHandler = customerFuncHandler;
|
||||
_customerInvoiceFuncHandler = customerInvoiceFuncHandler;
|
||||
_customerPlanFuncHandler = customerPlanFuncHandler;
|
||||
_customerSubscriptionFuncHandler = customerSubscriptionFuncHandler;
|
||||
_mealFuncHandler = mealFuncHandler;
|
||||
_authFuncHandler = authFuncHandler;
|
||||
_workoutFuncHandler = workoutFuncHandler;
|
||||
_documentFuncHandler = documentFuncHandler;
|
||||
_feedFuncHandler = feedFuncHandler;
|
||||
_accessDeviceFuncHandler = accessDeviceFuncHandler;
|
||||
_attendanceFuncHandler = attendanceFuncHandler;
|
||||
_chatFuncHandler = chatFunctionHandler;
|
||||
_docHandler = docHandler;
|
||||
_docService = docService;
|
||||
_userRegisterHandler = userRegisterHandler;
|
||||
_clientStatFuncHandler = clientStatFuncHandler;
|
||||
_paymentFuncHandler = paymentFuncHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check API health status
|
||||
/// </summary>
|
||||
/// <returns>API status and current timestamp</returns>
|
||||
/// <response code="200">Returns API status</response>
|
||||
[HttpGet("status")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public IActionResult GetStatus()
|
||||
{
|
||||
return Ok(new { status = "API is running", timestamp = System.DateTime.UtcNow });
|
||||
}
|
||||
|
||||
[HttpPost("ticket")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteTicket([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _ticketFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("activitylog")]
|
||||
public async Task<IActionResult> ExecuteActivityLog([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _activityLogFuncHandler.ExecuteAsync(request.FunctionName, request.Payload, HttpContext);
|
||||
return Ok(new ApiResponse<object>
|
||||
{
|
||||
StatusCode = 200,
|
||||
Message = "Success",
|
||||
Data = response
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("client")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteClient([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _clientFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("auth")]
|
||||
public async Task<IActionResult> ExecuteAuth([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _authFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("VerifyOTP")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ForgotPassword([FromBody] ApiRequest request)
|
||||
{
|
||||
request.FunctionName = "VerifyOTP";
|
||||
var response = await _authFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("refreshToken")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> RefreshToken([FromBody] ApiRequest request)
|
||||
{
|
||||
request.FunctionName = "RefreshToken";
|
||||
var response = await _authFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
// Anonymous so logout still clears cookies even if the access token has expired.
|
||||
[HttpPost("logout")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Logout([FromBody] ApiRequest? request)
|
||||
{
|
||||
var logoutRequest = request ?? new ApiRequest();
|
||||
logoutRequest.FunctionName = "LogOut";
|
||||
var response = await _authFuncHandler.Execute(logoutRequest, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpPost("staff")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteStaff([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _staffFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("clientinvoice")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteClientInvoice([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _clientInvoiceFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("customer")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteCustomer([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _customerFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("customerinvoice")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteCustomerInvoice([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _customerInvoiceFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("customerplan")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteCustomerPlan([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _customerPlanFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("customersubscription")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteCustomerSubscription([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _customerSubscriptionFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("meal")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteMeal([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _mealFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("workout")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteWorkout([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _workoutFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("document")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteDocument([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _documentFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("feed")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteFeed([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _feedFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("accessdevice")]
|
||||
public async Task<IActionResult> ExecuteAccessDevice([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _accessDeviceFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("attendance")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteAttendance([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _attendanceFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("chat")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteChat([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _chatFuncHandler.Execute(request);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
[HttpPost("initChat")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> InitChat([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _chatFuncHandler.Execute(request);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("payment")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecutePayment([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _paymentFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("clientstat")]
|
||||
[CustomMiddleware]
|
||||
[TenantResolution]
|
||||
public async Task<IActionResult> ExecuteClientStat([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _clientStatFuncHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> ExecuteRegsiter([FromBody] ApiRequest request)
|
||||
{
|
||||
var response = await _userRegisterHandler.Execute(request, HttpContext);
|
||||
return StatusCode(response.StatusCode, response);
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("docService/{docId}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> GetDocument(string docId)
|
||||
{
|
||||
if (!int.TryParse(docId, out int documentId))
|
||||
return BadRequest("Invalid Document ID format.");
|
||||
|
||||
|
||||
DocumentDownloadModel result = await _docService.GetFileBytesById(documentId);
|
||||
|
||||
if (result == null)
|
||||
return NotFound("Document not found");
|
||||
|
||||
try
|
||||
{
|
||||
byte[] fileBytes = result.FileData;
|
||||
string fileName = result.FileName;
|
||||
|
||||
var provider = new FileExtensionContentTypeProvider();
|
||||
if (!provider.TryGetContentType(fileName, out var contentType))
|
||||
{
|
||||
contentType = "application/octet-stream";
|
||||
}
|
||||
|
||||
return File(fileBytes, contentType, fileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest($"Error processing document: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user