Exemple #1
0
 public ActionResult <Contract> Create([FromBody] Contract newContract)
 {
     try
     {
         return(Ok(_serv.Create(newContract)));
     }
     catch (System.Exception err)
     {
         return(BadRequest(err.Message));
     }
 }
        public async Task <IActionResult> ConcludeContract(int id, [Bind("ContractId,PropertyId,PaymentMethod,Paid,RentStart,RentFinish,Rent,Deposit,ClientId,Duration")] Contract contract)
        {
            if (ModelState.IsValid &&
                contract.RentStart.CompareTo(DateTime.Now.Date) >= 0 &&
                contract.RentFinish.CompareTo(DateTime.Now.Date) > 0)
            {
                // Если эта проперти уже не доступна
                if (_service.GetRentableProperties().FirstOrDefault(p => p.PropertyId == contract.PropertyId) == null)
                {
                    ViewData["BackController"] = "Contracts";
                    ViewData["BackAction"]     = "Index";
                    ViewData["ErrorMessage"]   = "This property already has a contract.";
                    return(View("DbUpdateError"));
                }

                int result = await _service.Create(contract);

                if (result == 0)
                {
                    ViewData["BackController"] = "Contracts";
                    ViewData["BackAction"]     = "Index";
                    ViewData["ErrorMessage"]   = "Form data has been lost. Database refused your request. Contact sysadmin.";
                    return(View("DbUpdateError"));
                }

                return(RedirectToAction("Index"));
            }

            if (!(contract.RentStart.CompareTo(DateTime.Now.Date) >= 0))
            {
                ModelState.AddModelError("RentStart", "Дата начала аренды не должна быть раньше сегодняшнего числа.");
            }
            if (!(contract.RentFinish.CompareTo(DateTime.Now.Date) > 0))
            {
                ModelState.AddModelError("RentFinish", "Дата окончания аренды должна быть позже сегодняшнего числа.");
            }

            PropertyForRent property = _service.Properties().FirstOrDefault(pfr => pfr.PropertyId == id);

            if (property == null)
            {
                return(NotFound());
            }

            User myUser = await _service.GetUserFromEmail(User.Identity.Name);

            if (myUser == null)
            {
                return(NotFound());
            }

            Client myClient = _service.Clients().FirstOrDefault(x => x.ClientPassportNo == myUser.Passport);

            if (myClient == null)
            {
                return(NotFound());
            }

            ViewData["AllClients"]  = new SelectList(_service.Clients(), "ClientId", "ClientPassportNo");
            ViewData["propertyNo"]  = property.PropertyNo;
            ViewData["PaymentMeth"] = new SelectList(_service.GetPaymentMethods());

            return(View(contract));
        }