public IActionResult Add() { var viewModel = new AddPropertyInputModel(); viewModel.Countries = this.countriesService.GetAllByKeyValuePairs(); viewModel.PropertyCategories = this.propertyCategoriesService.GetAllByKeyValuePairs(); viewModel.Facilities = this.facilitiesService.GetAllInGeneralCategory <AddFacilityIdNameInputModel>(); viewModel.Rules = this.rulesService.GetAll <AddRuleInputModel>(); return(this.View(viewModel)); }
public async Task <IActionResult> Add(AddPropertyInputModel input) { if (this.propertiesService.CheckIfNameIsAvailable(input.Name)) { this.ModelState.AddModelError(nameof(input.Name), GlobalConstants.ErrorMessages.PropertyNameIsAlreadyUsed); } if (!this.ModelState.IsValid) { input.Countries = this.countriesService.GetAllByKeyValuePairs(); input.PropertyCategories = this.propertyCategoriesService.GetAllByKeyValuePairs(); input.Facilities = this.facilitiesService.GetAllInGeneralCategory <AddFacilityIdNameInputModel>(); input.Rules = this.rulesService.GetAll <AddRuleInputModel>(); return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); try { await this.propertiesService.CreateAsync(input, user.Id, $"{this.environment.WebRootPath}{GlobalConstants.PropertyImagesPath}"); } catch (Exception ex) { this.ModelState.AddModelError(nameof(input.Images), ex.Message); input.Countries = this.countriesService.GetAllByKeyValuePairs(); input.PropertyCategories = this.propertyCategoriesService.GetAllByKeyValuePairs(); input.Facilities = this.facilitiesService.GetAllInGeneralCategory <AddFacilityIdNameInputModel>(); input.Rules = this.rulesService.GetAll <AddRuleInputModel>(); return(this.View(input)); } this.TempData[GlobalConstants.SuccessMessages.AddKey] = GlobalConstants.SuccessMessages.AddValue; return(this.Redirect(nameof(this.All))); }
public async Task CreateAsync(AddPropertyInputModel input, string userId, string imagePath) { var property = new Property { Name = input.Name, Address = input.Address, PropertyCategoryId = input.PropertyCategoryId, Floors = input.Floors, Stars = input.PropertyRating, TownId = input.TownId, ApplicationUserId = userId, Description = input.Description, }; var rules = this.rulesRepository.All(); var rulesIds = input.RulesIds != null ? input.RulesIds : new List <int>(); foreach (var rule in rules) { var propertyRule = new PropertyRule { Property = property, RuleId = rule.Id, IsAllowed = rulesIds.Contains(rule.Id), }; property.PropertyRules.Add(propertyRule); } var facilitiesIds = input.FacilitiesIds; if (facilitiesIds != null) { foreach (var facilityId in facilitiesIds) { var propertyFacility = new PropertyFacility { Property = property, FacilityId = facilityId, }; property.PropertyFacilities.Add(propertyFacility); } } Directory.CreateDirectory(imagePath); if (input.Images != null && input.Images.Any()) { foreach (var image in input.Images) { var extension = Path.GetExtension(image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new Exception($"{GlobalConstants.ErrorMessages.ImageExtention} {extension}"); } var propertyImage = new PropertyImage { Extension = extension, }; property.PropertyImages.Add(propertyImage); var physicalPath = $"{imagePath}{propertyImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } } await this.propertiesRepository.AddAsync(property); await this.propertiesRepository.SaveChangesAsync(); }