I have been getting 500 error when I tested my POST method in postman and I really have no idea why no matter how my times I modified my code.
Here is the model I added after connecting to the external db in MSSQL server:
public partial class Shipment
{
public Shipment()
{
Packages = new HashSet<Package>();
}
/// <summary>
/// Shipment Unique Identifier
/// </summary>
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid SSuid { get; set; }
public string? SType { get; set; }
public string? SFrom { get; set; }
public string? SCompanyName { get; set; }
public string? SFname { get; set; }
public string? SMname { get; set; }
public string? SLname { get; set; }
public string? SAddress1 { get; set; }
public string? SAddress2 { get; set; }
public string? SCity { get; set; }
public string? SState { get; set; }
public string? SPostalCode { get; set; }
public string? SCountry { get; set; }
public string? SPo { get; set; }
public DateTime SDateReceived { get; set; }
public DateTime SDateEntered { get; set; }
public string? SUserName { get; set; }
public virtual ICollection<Package> Packages { get; set; }
}
Here is my repository:
public async Task AddShipment(Shipment shipment)
{
await _operationsContext.Shipments.AddAsync(shipment);
await _operationsContext.SaveChangesAsync();
}
public async Task<Shipment> GetById(Guid id)
{
return await _operationsContext.Shipments.FirstOrDefaultAsync(s => s.SSuid == id);
}
Here are my controllers:
[Produces("application/json")]
[Route("api/shipment")]
[ApiController]
public class ShipmentController : Controller
{
private readonly IShipmentRepository _shipmentRepository;
[HttpGet("{ssuid}", Name = "GetShipmentById")]
public async Task<ActionResult<Shipment>> GetShipmentById(Guid ssuid)
{
try
{
var result = await _shipmentRepository.GetById(ssuid);
if(result == null)
{
return BadRequest();
}
return result;
} catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}
[HttpPost]
public async Task<ActionResult<Shipment>> AddShipment([FromBody] Shipment shipment)
{
try
{
await _shipmentRepository.AddShipment(shipment);
return CreatedAtRoute(nameof(GetShipmentById), new {id = shipment.SSuid}, shipment);
} catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
}
When I tried to add a new shipment
{
"sType": "UPS",
"sFrom": "Mike",
"sCompanyName": "",
"sFname": "David",
"sMname": "",
"sLname": "Beckham",
"sAddress1": "1990 e 12th st",
"sAddress2": "",
"sCity": "Irvine",
"sState": "California",
"sPostalCode": "75655",
"sCountry": "United States",
"sPo": "",
"sDateReceived": "2022-09-09T00:00:00",
"sDateEntered": "2022-09-09T10:56:24.617",
"sUserName": "mbeckham"
}
in postman, I get
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.6.1",
"title": "An error occurred while processing your request.",
"status": 500,
"traceId": "00-fa8bb4a7d8b5372766baa5d2c1030b39-29b3770b4eb356de-00"
}
I would appreciate any helps from the experts!
CodePudding user response:
Hard to say what the exact issue might be, but have u set the newid()
for GUID field SSuid in Database. Assuming its the PK, decorate the property with [KEY]
attribute.
Please put a try catch block and post the exact error.
CodePudding user response:
It looks like you have a constructor dependency. Maybe you should make sure that you register a concrete implementation of IShipmentRepository when you configure IoC. For example as follows:
x.For<IShipmentRepository>().Use<MyService>();