public void BuyProductByCharacter(int userId, int charId, int prodId) { Character character = GetCharacterForUser(charId, userId); Product product = GetProduct(prodId); if (character.Credits < product.Price) { throw new Exception("Insufficient funds"); } CharactersProducts buyProd = new CharactersProducts { CreatedAt = DateTime.UtcNow, CharacterId = character.Id, ProductId = product.Id }; character.Inventory.Add(buyProd); character.Credits = character.Credits - product.Price; character = _characterRepository.Update(character); }
public void SellCharacterProduct(int userId, int charId, int prodId) { Character character = GetCharacterForUser(charId, userId); Product product = GetProduct(prodId); if (!character.Inventory.Any(x => x.ProductId.Equals(product.Id))) { throw new Exception("Current product not found in character inventory."); } CharactersProducts cp = _cpRepository.Table.FirstOrDefault(x => x.ProductId == product.Id && x.CharacterId == character.Id); if (cp == null) { throw new Exception("Current product not found in character inventory."); } character.Credits += product.SellPrice; _cpRepository.Delete(cp); character = GetCharacterForUser(charId, userId); }