public async Task <ActionResult <ErrandApplication> > PostErrandApplication([FromBody] PostErrandApplicationDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var errand = await _context.Errands.FindAsync(model.ErrandId); if (errand == null) { return(NotFound()); } if (await _context.ErrandApplications.Where(x => x.ErrandId == errand.ErrandId && x.UserId == User.Identity.Name).FirstOrDefaultAsync() != null) { return(BadRequest("You have already applied to this Errand")); } if (errand.UserId == User.Identity.Name) { return(BadRequest("You can't do that")); } var errandApplication = new ErrandApplication() { ErrandId = errand.ErrandId, UserId = User.Identity.Name }; if (errand.Reward > 0) { 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("CreditCard belongs to someone else")); } errandApplication.CreditCardId = creditCard.CreditCardId; } await _context.ErrandApplications.AddAsync(errandApplication); await _context.SaveChangesAsync(); return(Ok("Application Created")); }
public async Task <ActionResult <ErrandApplication> > PutErrandApplication([FromRoute] Guid id, [FromBody] PutErrandApplicationDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != model.ErrandApplicationId) { return(BadRequest()); } var applicationErrand = await _context.ErrandApplications.FindAsync(model.ErrandApplicationId); if (applicationErrand == null) { return(NotFound("ErrandApplication not found")); } var errand = await _context.Errands.FindAsync(applicationErrand.ErrandId); if (errand == null) { return(NotFound("Errand not found")); } if (errand.UserId != User.Identity.Name) { return(Unauthorized()); } var errandApplication = new ErrandApplication() { ErrandApplicationId = applicationErrand.ErrandApplicationId, ErrandId = errand.ErrandId, UserId = applicationErrand.UserId, CreditCardId = applicationErrand.CreditCardId, ApplicationStatus = model.ApplicationStatus }; _context.Entry(errandApplication).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok($"ApplicationErrandStatus: {errandApplication.ApplicationStatus.ToString()}")); }