public IActionResult MakePurchase( [FromBody] PurchaseForCreationDto newPurchase) { if (newPurchase == null) { return(BadRequest()); } if (!_playerRepository.Exists(newPurchase.PlayerId) || !_chestRepository.Exists(newPurchase.ChestId)) { return(NotFound()); } bool purchaseIsSuccessful = _purchaseRepository.MakePurchase(newPurchase.PlayerId, newPurchase.ChestId, newPurchase.IsPayedInGold, newPurchase.Quantity); if (purchaseIsSuccessful) { if (!_purchaseRepository.Save()) { return(StatusCode(500, "A problem happened while handling your request")); } return(Ok("Purchase validated with success")); } else { return(StatusCode(501, "Invalid purchase")); } }
public async Task <IActionResult> CreatePurchaseForUser([FromBody] PurchaseForCreationDto purchase) { var product = _repository.Product.GetProductByIdAsync(purchase.ProductId, trackChanges: false); var price = product.Result.Price; if (product == null) { _logger.LogInfo($"Product with id: {purchase.ProductId} doesn't exist in the database."); return(NotFound()); } var userId = _manager.FindByNameAsync(User.Identity.Name).Result.Id; var purchaseEntity = _mapper.Map <Purchase>(purchase); _repository.Purchase.CreatePurchaseForUser(userId, purchase.ProductId, price, purchaseEntity); await _repository.SaveAsync(); var purchaseDto = _mapper.Map <PurchaseDto>(purchaseEntity); return(CreatedAtRoute("GetPurchaseById", new { id = purchaseDto.Id }, purchaseDto)); }