ContentController.cs: Handle CommonInventoryRequests in CreatePet (#20)

This commit is contained in:
J. D 2023-08-18 15:47:37 -05:00 committed by GitHub
parent 17303d9256
commit c3bb769934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -388,7 +388,20 @@ public class ContentController : Controller {
ctx.Images.Add(image);
ctx.SaveChanges();
// TODO: handle CommonInventoryRequests here too
if (raisedPetRequest.CommonInventoryRequests is not null) {
foreach (var req in raisedPetRequest.CommonInventoryRequests) {
InventoryItem? item = viking.Inventory.InventoryItems.FirstOrDefault(e => e.ItemId == req.ItemID);
//Does the item exist in the user's inventory?
if (item is null) continue; //If not, skip it.
if (item.Quantity + req.Quantity >= 0 ) { //If so, can we update it appropriately?
//We can. Do so.
item.Quantity += req.Quantity; //Note that we use += here because req.Quantity is negative.
ctx.SaveChanges();
}
}
}
return Ok(new CreatePetResponse {
RaisedPetData = GetRaisedPetDataFromDragon(dragon)