2026-06-16 15:18:22 +06:00
using Asp.Versioning ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
2026-06-16 17:22:46 +06:00
using Microsoft.Extensions.Logging ;
2026-06-16 15:18:22 +06:00
using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations ;
using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations ;
2026-06-16 17:22:46 +06:00
using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Integrations ;
using System ;
2026-06-16 15:18:22 +06:00
using System.Threading.Tasks ;
namespace OnlineSalesAutoCrop.CoreAPI.Controllers.IntegretionApi
{
/// <summary>
///
/// </summary>
/// <remarks>
///
/// </remarks>
/// <param name="logger"></param>
2026-06-16 17:22:46 +06:00
/// <param name="integrationService"></param>
2026-06-16 15:18:22 +06:00
[Authorize]
[ApiController]
[ApiVersion("1.0")]
[ValidateAntiForgeryToken]
[Route("api/v{version:apiVersion}/Integration")]
2026-06-16 17:22:46 +06:00
public class IntegrationController ( ILogger < IntegrationAuthController > logger , IIntegrationService integrationService ) : ControllerBase
2026-06-16 15:18:22 +06:00
{
2026-06-16 17:22:46 +06:00
private readonly ILogger _logger = logger ;
private readonly IIntegrationService _integrationService = integrationService ;
2026-06-16 17:24:37 +06:00
/// <summary>
/// Insert or Update Customers for SAP
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="request"></param>
/// <returns>if created return 201 or if updated return 204 true</returns>
2026-06-16 18:15:04 +06:00
[HttpPost("Customers")]
2026-06-16 15:18:22 +06:00
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IntegrationLoginResponse))]
2026-06-16 17:22:46 +06:00
public async Task < IActionResult > UpsertCustomers ( CustomerIntegrationRequest request )
2026-06-16 15:18:22 +06:00
{
2026-06-16 17:22:46 +06:00
CustomerIntegrationResponse response = new CustomerIntegrationResponse ( ) ;
try
{
2026-06-16 17:51:28 +06:00
response = await _integrationService . UpsertCustomerAsync ( request ) ;
if ( ! response . IsUpdated )
2026-06-16 17:22:46 +06:00
{
response . ReturnMessage . Add ( $"Customer Created Successfully for CustomerNumber :{request.CustomerNumber} & CompanyCode:{request.CompanyCode}" ) ;
response . ReturnStatus = StatusCodes . Status201Created ;
return StatusCode ( StatusCodes . Status201Created , response ) ;
}
else
{
response . ReturnMessage . Add ( $"Customer Updated Successfully for CustomerNumber :{request.CustomerNumber} & CompanyCode:{request.CompanyCode}" ) ;
2026-06-16 18:15:04 +06:00
response . ReturnStatus = StatusCodes . Status200OK ;
return StatusCode ( StatusCodes . Status200OK , response ) ;
2026-06-16 17:22:46 +06:00
}
}
catch ( Exception ex )
{
string msg = $"Exception Occur in Customer Operation {request?.CustomerName}~{request?.CompanyCode}" ;
_logger . LogError ( exception : ex , msg ) ;
response . ReturnStatus = StatusCodes . Status500InternalServerError ;
response . ReturnMessage . Add ( ex . InnerException ! = null ? ex . InnerException . Message : ex . Message ) ;
return StatusCode ( StatusCodes . Status500InternalServerError , response ) ;
}
2026-06-16 15:18:22 +06:00
}
2026-06-16 19:17:16 +06:00
/// <summary>
/// Insert or Update Employees for SAP
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="request"></param>
/// <returns>if created return 201 or if updated return 204 true</returns>
[HttpGet("Employees")]
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EmployeeIntegrationReqResponse))]
public async Task < IActionResult > UpsertEmployee ( EmployeeIntegrationRequest request )
{
EmployeeIntegrationReqResponse response = new EmployeeIntegrationReqResponse ( ) ;
try
{
2026-06-17 13:51:19 +06:00
response = await _integrationService . UpsertEmployeeAsync ( request ) ;
if ( ! response . IsUpdated )
2026-06-16 19:17:16 +06:00
{
response . ReturnMessage . Add ( $"Employee Created Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}" ) ;
response . ReturnStatus = StatusCodes . Status201Created ;
return StatusCode ( StatusCodes . Status201Created , response ) ;
}
else
{
response . ReturnMessage . Add ( $"Employee Updated Successfully for EmployeeVendorName :{request.EmployeeVendorName} & SalesOrgCode:{request.SalesOrgCode}" ) ;
response . ReturnStatus = StatusCodes . Status204NoContent ;
return StatusCode ( StatusCodes . Status204NoContent , response ) ;
}
}
catch ( Exception ex )
{
string msg = $"Exception Occur in Employee Operation {request?.EmployeeVendorName}~{request?.SalesOrgCode}" ;
_logger . LogError ( exception : ex , msg ) ;
response . ReturnStatus = StatusCodes . Status500InternalServerError ;
response . ReturnMessage . Add ( ex . InnerException ! = null ? ex . InnerException . Message : ex . Message ) ;
return StatusCode ( StatusCodes . Status500InternalServerError , response ) ;
}
}
2026-06-16 15:18:22 +06:00
}
}