Example #1
0
        public async Task <IActionResult> CreateApartment([FromBody] Apartment _)
        {
            var host = await _hostService.LoggedHost();

            if (host != null)
            {
                _.HostId = host.Id;
                await _apartmentService.CreateApartment(_);

                return(Ok());
            }
            return(BadRequest());
        }
        public IActionResult Create([FromForm] ApartmentModel model)
        {
            if (ModelState.IsValid)
            {
                var apt = _apartmentService.GetApartmentByName(model.Name);
                if (apt != null)
                {
                    model.IsDuplicateName  = true;
                    model.Cities           = GetAllCities();
                    model.NumberOfBedsList = GetNumberOfBeds();
                    return(View(model));
                }
                if (model.AvailableFrom < DateTime.Today)
                {
                    model.Cities           = GetAllCities();
                    model.NumberOfBedsList = GetNumberOfBeds();
                    model.IsLowerDate      = true;
                    return(View(model));
                }
                if (model.Phone != null)
                {
                    model.Phone = model.Phone.ToString().Replace("-", "");
                }
                var apartment = new Apartment
                {
                    Name          = model.Name,
                    Address       = model.Address,
                    Price         = model.Price.GetValueOrDefault(),
                    NumberOfBeds  = model.NumberOfBeds,
                    Description   = model.Description,
                    Phone         = model.Phone,
                    CityId        = model.CityId,
                    AvailableFrom = model.AvailableFrom.GetValueOrDefault()
                };
                try
                {
                    _apartmentService.CreateApartment(apartment);
                }
                catch (Exception ex)
                {
                    model.IsTryCatch = true;
                    _log.LogError(ex, ex.Message);
                }
                var apartmentid = _apartmentService.GetApartmentByName(apartment.Name);
                return(RedirectToAction("Index", "Details", new { id = apartmentid.Id }));
            }

            model.Cities           = GetAllCities();
            model.NumberOfBedsList = GetNumberOfBeds();
            return(View(model));
        }