using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using ShippingAPI.Classes;
using System.Threading.Tasks;
namespace ShippingAPI.Controllers
[Produces("application/json")]
[Route("api/[controller]")]
public class ShipMethodController : ControllerBase
private readonly string _connectionString;
private readonly IConfiguration _configuration;
public ShipMethodController(IConfiguration configuration)
_configuration = configuration;
_connectionString = _configuration.GetSection("ConnectionStrings").GetValue<string>("ShippingDatabase");
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult Get()
var shpMthRsonse = getShipMethodResult();
catch (SqlException sqlex)
return new ObjectResult(GetErrorResponse("Internal Database Error", 50000))
Log.Error($"Exception occurred:{ex.Message}");
return new ObjectResult(GetErrorResponse("Internal Server Error.", 50000))
public ShipMethodResponse getShipMethodResult()
ShipMethodResponse ship_Method_Response = new();
var proc = "dbo.up_GetActiveShipMethods";
var conn = new SqlConnection(_connectionString);
var result = (conn.QueryAsync<ShipMethodEntity>(proc, commandType: CommandType.StoredProcedure, commandTimeout: 0).Result);
ship_Method_Response.ShipMethodEntity = result;
var param = new DynamicParameters();
var proc = "IC.up_icsStartSession";
param.Add("@Channel", "API");
param.Add("@LocalName", Dns.GetHostName());
param.Add("@RemoteName", (await Dns.GetHostAddressesAsync(Dns.GetHostName()))[0].ToString());
param.Add("@SPOutput", 1);
var connection = new SqlConnection(_connectionString);
sessionId = (connection.QueryAsync<int>(proc, param, commandType: CommandType.StoredProcedure, commandTimeout: 0)).First();
ship_Method_Response.session_id = sessionId;
return ship_Method_Response;
public ShipMethodResponse GetErrorResponse(string errorDescription, int errorNumber)
return new ShipMethodResponse()
ErrorDescription = errorDescription,
ErrorNumber = errorNumber
public class ShipMethodResponse
public string Result { get; set; }
public int? ErrorNumber { get; set; }
public string ErrorDescription { get; set; }
public int session_id {get;set;}
public IEnumerable<ShipMethodEntity> ShipMethodEntity { get; set; }