public async Task <IActionResult> PutErrand([FromRoute] Guid id, [FromBody] PutErrandDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != model.ErrandId) { return(BadRequest()); } var errand = await _context.Errands.FindAsync(id); if (errand == null) { return(NotFound()); } if (errand.UserId != User.Identity.Name || !User.IsInRole("Admin")) { return(Unauthorized()); } if (errand.Status != ErrandStatus.Open) { return(BadRequest("Errand is either in progress or completed. Can't be modified")); } var oldErrand = errand; int oldUsersRequired = errand.UsersRequired; float oldReward = errand.Reward; if (model.UsersRequired != null) { errand.UsersRequired = (int)(model.UsersRequired); } if (model.Reward != null) { errand.Reward = (float)(model.Reward); } _context.Entry(errand).State = EntityState.Modified; if (oldReward > 0) { var oldTransactionInfo = _context.ErrandTransactionInfos.Where(x => x.ErrandId == errand.ErrandId).FirstOrDefault(); if (oldTransactionInfo == null) { return(BadRequest("There is a Reward but there are no transaction info saved")); } if (errand.Reward == 0) { _context.ErrandTransactionInfos.Remove(oldTransactionInfo); } else if (errand.UsersRequired != oldUsersRequired || errand.Reward != oldReward) { var newTransactionInfo = new ErrandTransactionInfo() { TransactionInfoId = oldTransactionInfo.TransactionInfoId, ErrandId = errand.ErrandId, CreditCardId = oldTransactionInfo.CreditCardId, CreditCard = oldTransactionInfo.CreditCard, TotalCost = errand.Reward * errand.UsersRequired, Profit = errand.Reward * errand.UsersRequired * 0.05f }; _context.Entry(newTransactionInfo).State = EntityState.Modified; } } else { if (errand.Reward > 0) { var creditCard = await _context.CreditCards.FindAsync(model.CreditCardId); if (creditCard == null) { return(NotFound("This Credit Card doesn't exist")); } if (creditCard.UserId != User.Identity.Name) { return(Unauthorized("This Credit Card belongs to someone else")); } var newTransactionInfo = new ErrandTransactionInfo() { ErrandId = errand.ErrandId, CreditCardId = creditCard.CreditCardId, CreditCard = creditCard, TotalCost = errand.Reward * errand.UsersRequired, Profit = errand.Reward * errand.UsersRequired * 0.05f }; if (newTransactionInfo.CreditCard.Balance < newTransactionInfo.TotalCost + newTransactionInfo.Profit) { return(BadRequest("Not enough money")); } await _context.ErrandTransactionInfos.AddAsync(newTransactionInfo); } } if (oldErrand != errand) { var errandApplications = await _context.ErrandApplications.Where(x => x.ErrandId == errand.ErrandId).ToListAsync(); if (errandApplications != null) { foreach (var application in errandApplications) { _context.ErrandApplications.Remove(application); } } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ErrandExists(id)) { return(NotFound()); } else { throw; } } return(Ok("Errand Modified Successfully")); }
public async Task <ActionResult <Errand> > PostErrand(PostErrandDto model) { if (_context.Errands.Where(x => x.UserId == User.Identity.Name && x.Status != ErrandStatus.Completed).Count() >= 5) { return(BadRequest("You have 5 active Errands already")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var errand = new Errand() { UserId = User.Identity.Name, Description = model.Description, StartDateTime = model.StartDateTime, EndDateTime = model.EndDateTime, UsersRequired = model.UsersRequired, Reward = model.Reward }; var existingErrand = _context.Errands.Where(x => x.Description == errand.Description && x.UserId == errand.UserId).FirstOrDefault(); if (existingErrand != null) { return(BadRequest($"You have already requested errand with this description: {existingErrand.Description}")); } await _context.Errands.AddAsync(errand); if (errand.Reward > 0 && model.CreditCardId != null) { var creditCard = await _context.CreditCards.FindAsync(model.CreditCardId); if (creditCard == null) { return(NotFound("Credit Card doesn't exist")); } if (creditCard.UserId != User.Identity.Name) { return(Unauthorized("Credit Card belongs to someone else")); } var transactionInfo = new ErrandTransactionInfo() { ErrandId = errand.ErrandId, CreditCardId = creditCard.CreditCardId, CreditCard = creditCard, TotalCost = errand.Reward * errand.UsersRequired, Profit = errand.Reward * errand.UsersRequired * 0.05f }; if (transactionInfo.CreditCard.Balance < transactionInfo.TotalCost + transactionInfo.Profit) { return(BadRequest("Not enough money")); } await _context.ErrandTransactionInfos.AddAsync(transactionInfo); } try { await _context.SaveChangesAsync(); } catch (Exception) { BadRequest(); } return(Ok(new { message = "Errand Creation Successful" })); }