[HttpPost]
public async Task<ActionResult<ProductOwner>> PostProductOwner(ProductOwner productOwner)
{
if (_context.ProductOwners == null)
{
return Problem("Entity set 'ApplicationDbContext.ProductOwners' is null.");
}
// Configure warranty expiration date and status
var warrantyDetail = new WarrantyDetail
{
ExpirationDate = DateTime.Now.AddYears(2).Date,
WarrantyStatus = "Active",
};
productOwner.ProductSerialNumber = await GenerateUniqueSerialNumberAsync();
// Query ProductDetail
var productDetail = await _context.ProductDetails.FirstOrDefaultAsync(pd => pd.ProductDetailId == productOwner.ProductDetailId);
if (productDetail == null) {
return BadRequest();
}
var newProductOwner = new ProductOwner
{
OwnerFirstName = productOwner.OwnerFirstName,
OwnerLastName = productOwner.OwnerLastName,
EmailAddress = productOwner.EmailAddress,
PhoneNum = productOwner.PhoneNum,
ProductSerialNumber = productOwner.ProductSerialNumber,
ProductDetailId = productOwner.ProductDetailId,
ProductDetail = productDetail,
WarrantyDetail = warrantyDetail
};
// Setting the reverse navigation property
warrantyDetail.ProductOwner = newProductOwner;
productDetail.ProductOwners.Add(newProductOwner);
_context.ProductOwners.Add(newProductOwner);
await _context.SaveChangesAsync();
return Ok(await _context.ProductOwners.ToListAsync());
}