sspirit
sspirit
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Anime reference cause saw your Youtube channel haha. Have a great day man. Arigato!
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
No description
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Ok got it, will update here if I able to restructure it successfully. Thanks for the help :feelscomfyman:
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Understood ... :NotedHmm: so it should be a good practice to always use DTO so we can modify the API request body.. For example in my db i have an entity with columns of brand/model/color.. assuming the brand is created by default i should not showcase it in my request body, I create a DTO only for model/color ... Correct? haha
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Correct me if I'm wrong so if a use DTO , my API call request body can be modified? I was in the assumption that all the columns in a table/entity should be made available in JSON Request body during an API call
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Yes, because when owner purchase a product it should come with a warranty
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
[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());
}
[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());
}
In case this helps :/ or should I post the code else where
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
No description
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
Yes, when a owner is created automatically in Warranty a new record is created in controller. But since this is related to navigation property I declared
public ProductOwner? ProductOwner
public ProductOwner? ProductOwner
for the one-to-one relationship
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
public class WarrantyDetail
{
[Key]
public int WarrantyDetailId { get; set; }
[DisplayFormat(DataFormatString = "{0:dd:MM:yyyy}", ApplyFormatInEditMode = true)]
public DateTime ExpirationDate { get; set; }
public string WarrantyStatus { get; set; } = "";
public ProductOwner? ProductOwner { get; set; }

}
public class WarrantyDetail
{
[Key]
public int WarrantyDetailId { get; set; }
[DisplayFormat(DataFormatString = "{0:dd:MM:yyyy}", ApplyFormatInEditMode = true)]
public DateTime ExpirationDate { get; set; }
public string WarrantyStatus { get; set; } = "";
public ProductOwner? ProductOwner { get; set; }

}
This productOwner inside Warranty model. Sorry not the list ones
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
I dont quite get it sorry. but I paste the response call that I got the error from. In my model I've created a list of Owners. but during Post API if the owner is not created yet how would I get the details to put in the list?
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
No description
39 replies
CC#
Created by sspirit on 7/2/2024 in #help
✅ EF Core Relationship
No description
39 replies