Пример #1
0
        public async Task <IActionResult> CreateAppartment(ApartmentForCreationDto appartmentocreate)
        {
            if (await repo.AppartmentExists(appartmentocreate.Address))
            {
                return(BadRequest("Appartment Already Exists!"));
            }

            Appartment CreatedAppartment = mapper.Map <Appartment>(appartmentocreate);
            await repo.Create(CreatedAppartment);

            if (await repo.SaveAll())
            {
                return(CreatedAtRoute("GetAppartment", new { id = CreatedAppartment.Id }, CreatedAppartment));
            }
            return(BadRequest());
        }
Пример #2
0
        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());
        }