public async Task <IActionResult> Edit(EditPropertyInputModel input)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            var property = this.propertiesService.GetById(input.Id, user.Id);

            if (property == null)
            {
                this.TempData[GlobalConstants.ErrorMessages.EditErrorKey] = GlobalConstants.ErrorMessages.EditErrorValue;
                return(this.RedirectToAction(nameof(this.All)));
            }

            if (this.propertiesService.CheckIfEditInputNameIsAvailable(input.Name, input.Id))
            {
                this.ModelState.AddModelError(nameof(input.Name), GlobalConstants.ErrorMessages.PropertyNameIsAlreadyUsed);
            }

            if (!this.ModelState.IsValid)
            {
                input.Rules      = this.rulesService.GetAllByPropertyId <EditRuleInputModel>(input.Id);
                input.Facilities = this.facilitiesService.GetAllByPropertyId(input.Id);

                return(this.View(input));
            }

            await this.propertiesService.UpdateAsync(input);

            this.TempData[GlobalConstants.SuccessMessages.EditKey] = GlobalConstants.SuccessMessages.EditValue;
            return(this.RedirectToAction(nameof(this.ById), new { id = input.Id }));
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, EditPropertyInputModel property)
        {
            if (!this.ModelState.IsValid)
            {
                property.TypeOfPropertiesItems = this.typeOfPropertiesService.GetAllKeyValuePairs();
                return(this.View());
            }

            await this.propertiesService.UpdateAsync(id, property);

            return(this.RedirectToAction("ById", new { id = id }));
        }
        public async Task UpdateAsync(int id, EditPropertyInputModel input)
        {
            var property = this.propertiesRepository.All().FirstOrDefault(x => x.Id == id);

            property.Name             = input.Name;
            property.Description      = input.Description;
            property.Location         = input.Location;
            property.Price            = input.Price;
            property.Area             = input.Area;
            property.Baths            = input.Baths;
            property.Beds             = input.Beds;
            property.Garages          = input.Garages;
            property.TypeOfPropertyId = input.TypeOfPropertyId;
            property.Address          = input.Address;

            await this.propertiesRepository.SaveChangesAsync();
        }
Example #4
0
        public ResponseWrapper <EditPropertyModel> EditProperty(int propertyId, EditPropertyInputModel model)
        {
            var entity = context
                         .Properties
                         .Single(x =>
                                 x.PropertyId == propertyId
                                 );

            entity.Name         = model.Name;
            entity.PropertyType = model.PropertyType;
            entity.EntityId     = model.EntityId;
            context.SaveChanges();
            var response = new EditPropertyModel
            {
                PropertyId   = entity.PropertyId,
                Name         = entity.Name,
                PropertyType = entity.PropertyType,
                EntityId     = entity.EntityId,
            };

            return(new ResponseWrapper <EditPropertyModel>(_validationDictionary, response));
        }
        public dynamic EditProperty(int propertyId, [FromBody] EditPropertyInputModel model)
        {
            var orchestrator = new PropertyOrchestrator(new ModelStateWrapper(this.ModelState));

            return(orchestrator.EditProperty(propertyId, model).GetResponse());
        }
Example #6
0
        public async Task UpdateAsync(EditPropertyInputModel input)
        {
            var property = this.propertiesRepository
                           .All()
                           .FirstOrDefault(p => input.Id == p.Id);

            property.Name        = input.Name;
            property.Floors      = input.Floors;
            property.Stars       = input.PropertyRating;
            property.Description = input.Description;

            var propertyRules = this.propertiesRepository
                                .AllWithDeleted()
                                .Where(p => p.Id == input.Id)
                                .Select(p => p.PropertyRules)
                                .FirstOrDefault();
            var rulesIds = input.RulesIds != null ? input.RulesIds : new List <int>();

            foreach (var propertyRule in propertyRules)
            {
                propertyRule.IsAllowed = rulesIds.Contains(propertyRule.RuleId) ? true : false;
            }

            var propertyFacilities = this.propertyFacilitiesRepository
                                     .AllWithDeleted()
                                     .Where(f => f.PropertyId == input.Id)
                                     .ToList();
            var facilitiesIds = input.FacilitiesIds != null ? input.FacilitiesIds : new List <int>();

            foreach (var facilityId in facilitiesIds)
            {
                var isNeededToBeAdded = !propertyFacilities.Any(f => f.FacilityId == facilityId);
                if (isNeededToBeAdded)
                {
                    var propertyFacility = new PropertyFacility
                    {
                        PropertyId = input.Id,
                        FacilityId = facilityId,
                    };

                    await this.propertyFacilitiesRepository.AddAsync(propertyFacility);

                    continue;
                }
                else
                {
                    var propertyFacility = propertyFacilities.FirstOrDefault(f => f.FacilityId == facilityId);
                    if (propertyFacility.IsDeleted == true)
                    {
                        this.propertyFacilitiesRepository.Undelete(propertyFacility);

                        continue;
                    }
                }
            }

            foreach (var propertyFacility in propertyFacilities)
            {
                var isNeededToBeDeleted = !facilitiesIds.Contains(propertyFacility.FacilityId);
                if (isNeededToBeDeleted)
                {
                    this.propertyFacilitiesRepository.Delete(propertyFacility);
                }
            }

            await this.propertyFacilitiesRepository.SaveChangesAsync();

            await this.propertiesRepository.SaveChangesAsync();
        }