public async Task <IActionResult> Add(AddOfferInputModel input) { var user = await this.userManager.GetUserAsync(this.User); var ifUserHasAccess = this.propertiesService.IsUserHasAccessToProperty(input.PropertyId, user.Id); if (!ifUserHasAccess) { this.TempData[GlobalConstants.ErrorMessages.PropertyAccessKey] = GlobalConstants.ErrorMessages.PropertyAccessValue; return(this.RedirectToAction("All", "Properties")); } this.ValidateCheckToDate(input); if (!this.ModelState.IsValid) { input.OfferFacilities = this.facilitiesService.GetAllExeptInGeneralCategory <AddOfferFacilityInputModel>(); input.BedTypes = this.bedTypesService.GetAllByKeyValuePairs(); input.CurrencyCode = this.currenciesService.GetByPropertyId(input.PropertyId); input.PropertyName = this.propertiesService.GetNameById(input.PropertyId); return(this.View(input)); } try { await this.offersService.CreateAsync(input, $"{this.environment.WebRootPath}{GlobalConstants.OfferImagesPath}"); } catch (Exception ex) { if (ex.Message.Contains(GlobalConstants.ErrorMessages.MembersCount)) { this.ModelState.AddModelError(nameof(input.BedTypesCounts), ex.Message); } else { this.ModelState.AddModelError(nameof(input.Images), ex.Message); } input.OfferFacilities = this.facilitiesService.GetAllExeptInGeneralCategory <AddOfferFacilityInputModel>(); input.BedTypes = this.bedTypesService.GetAllByKeyValuePairs(); input.CurrencyCode = this.currenciesService.GetByPropertyId(input.PropertyId); return(this.View(input)); } this.TempData[GlobalConstants.SuccessMessages.AddOfferKey] = GlobalConstants.SuccessMessages.AddOfferValue; return(this.RedirectToAction("ById", "Properties", new { id = input.PropertyId })); }
public async Task <IActionResult> Add(string id) { var user = await this.userManager.GetUserAsync(this.User); var ifUserHasAccess = this.propertiesService.IsUserHasAccessToProperty(id, user.Id); if (!ifUserHasAccess) { this.TempData[GlobalConstants.ErrorMessages.PropertyAccessKey] = GlobalConstants.ErrorMessages.PropertyAccessValue; return(this.RedirectToAction("All", "Properties")); } var viewModel = new AddOfferInputModel { PropertyId = id, PropertyName = this.propertiesService.GetNameById(id), OfferFacilities = this.facilitiesService.GetAllExeptInGeneralCategory <AddOfferFacilityInputModel>(), BedTypes = this.bedTypesService.GetAllByKeyValuePairs(), CurrencyCode = this.currenciesService.GetByPropertyId(id), }; return(this.View(viewModel)); }
public async Task CreateAsync(AddOfferInputModel input, string imagePath) { var offer = new Offer { ValidTo = (DateTime)input.ValidTo, ValidFrom = (DateTime)input.ValidFrom, PropertyId = input.PropertyId, PricePerPerson = input.PricePerPerson, Count = input.Count, }; var index = 0; var count = 0; var bedTypesIds = this.bedTypesRepository .All() .OrderBy(b => b.Type) .Select(b => b.Id) .ToList(); var bedTypesDb = this.bedTypesRepository .All() .Select(b => new { b.Id, b.Capacity, }) .ToList(); foreach (var bedTypeId in bedTypesIds) { var id = bedTypeId; var bedTypeCount = input.BedTypesCounts.ToList()[index]; if (bedTypeCount > 0) { for (int i = 0; i < bedTypeCount; i++) { var offerBedType = new OfferBedType { BedTypeId = id, Offer = offer, }; offer.OfferBedTypes.Add(offerBedType); } count += bedTypesDb.First(x => x.Id == bedTypeId).Capacity *bedTypeCount; if (count > 30) { throw new Exception(GlobalConstants.ErrorMessages.MembersCount); } } index++; } if (count <= 0) { throw new Exception(GlobalConstants.ErrorMessages.MembersCount); } var facilitiesIds = input.OfferFacilitiesIds; if (facilitiesIds != null) { foreach (var facilityId in facilitiesIds) { var offerFacility = new OfferFacility { Offer = offer, FacilityId = facilityId, }; offer.OfferFacilities.Add(offerFacility); } } 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 offerImage = new OfferImage { Extension = extension, }; offer.OfferImages.Add(offerImage); var physicalPath = $"{imagePath}{offerImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await image.CopyToAsync(fileStream); } } await this.offersRepository.AddAsync(offer); await this.offersRepository.SaveChangesAsync(); }