public async Task <IActionResult> Buy(BuyerToBuyDto buyertobuyDto) { if (!await _repo.BuyerExists(buyertobuyDto.BuyerId)) { return(BadRequest("Buyer doesn't exist")); } if (!await _repo.ApartmentExists(buyertobuyDto.ApartmentId)) { return(BadRequest("Apartment doesn't exist")); } if (await _repo.VerifyPurchased(buyertobuyDto.ApartmentId)) { return(BadRequest("This apartment has already been bought")); } if (!await _repo.VerifyCredit(buyertobuyDto.BuyerId, buyertobuyDto.ApartmentId)) { return(BadRequest("Cannot buy, not enough credit")); } _repo.Buy(buyertobuyDto.BuyerId, buyertobuyDto.ApartmentId); return(StatusCode(201)); }
public async Task <IActionResult> Buy([FromRoute] int buyerid, [FromRoute] int appid) { if (!await apprepo.AppartmentExists(appid)) { return(BadRequest("Appartment doesnt exist.")); } if (!await repo.BuyerExists(buyerid)) { return(BadRequest("Buyer doesnt exist.")); } var apprtmentFromRepo = await apprepo.GetAppartment(appid); if (apprtmentFromRepo == null) { return(BadRequest()); } var buyerFromRepo = await repo.GetBuyer(buyerid); if (buyerFromRepo.Credit < apprtmentFromRepo.Price) { return(BadRequest("Buyer doesnt have enough credit.")); } var ownedAppartment = mapper.Map <OwnedAppartment>(apprtmentFromRepo); ownedAppartment = await repo.Buy(buyerFromRepo, ownedAppartment, appid); if (await repo.SaveAll()) { var id = await repo.GetId(ownedAppartment.Address, buyerid); ownedAppartment.Id = id; return(CreatedAtRoute("GetOwnedAppartment", new { id = ownedAppartment.OwnerId, appid = ownedAppartment.Id }, ownedAppartment)); } return(BadRequest()); }