Ejemplo n.º 1
0
        public async Task <ActionResult> Create(CreateRentalInfoViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(ModelState.ToDictionary()));
            }

            var resultRental = await _rentalInfoManager.CreateRentalInfo(model, User.Identity.GetUserId());

            return(Json(resultRental));
        }
Ejemplo n.º 2
0
        //Work well
        public async Task <EditRentalInfoForPropertyViewModel> CreateRentalInfo(CreateRentalInfoViewModel rentalInfoToAddInProperty, string loggedUserId)
        {
            if (string.IsNullOrEmpty(loggedUserId))
            {
                throw new ArgumentNullException(nameof(loggedUserId));
            }

            var property = await unitOfWork.PropertiesRepository
                           .Include(p => p.Images)
                           .Where(p => p.Id == rentalInfoToAddInProperty.PropertyId)
                           .FirstOrDefaultAsync() ?? throw new ContentNotFoundException("Имотът не е намерен!");

            if (property.OwnerId != loggedUserId && property.AgentId != loggedUserId && !await userManager.IsInRoleAsync(loggedUserId, "Administrator"))
            {
                throw new NotAuthorizedException("Нямате право да създавате части в този имот!");
            }

            var areaInSquareFt = rentalInfoToAddInProperty.Attributes
                                 .Where(a => a.Key == AttributesResolvers.AreaInSquareMetersAttribute)
                                 .Select(a => a.Value)
                                 .FirstOrDefault();

            int?areaFinal = int.TryParse(areaInSquareFt, out var tempArea) ? tempArea : (int?)null;

            RentalsInfo rental = new RentalsInfo
            {
                PropertyId            = rentalInfoToAddInProperty.PropertyId,
                RentalPrice           = rentalInfoToAddInProperty.RentalPrice,
                RentalPricePeriodId   = rentalInfoToAddInProperty.RentalPricePeriodId,
                UnitCount             = rentalInfoToAddInProperty.UnitsCount,
                UnitTypeId            = rentalInfoToAddInProperty.UnitTypeId,
                AdditionalDescription = rentalInfoToAddInProperty.AdditionalInfo,
                AreaInSquareMeters    = areaFinal,
                Attributes            = new HashSet <KeyValuePairs>(rentalInfoToAddInProperty.Attributes
                                                                    .Select(a => AttributesResolvers.AttributesResolver(a.Key, a.Value))
                                                                    .ToList()),
                Extras = new HashSet <Extras>(_extrasManager.GetRentalExtras(rentalInfoToAddInProperty
                                                                             .RentalExtras
                                                                             .Where(e => e.IsChecked)
                                                                             .Select(e => e.ExtraId)
                                                                             .ToList()))
            };

            unitOfWork.RentalsRepository.Add(rental);
            await unitOfWork.SaveAsync();

            #region Notifications

            var creatorName = await unitOfWork.UsersRepository
                              .Where(u => u.Id == property.AgentId)
                              .Select(a => a.FirstName + " " + a.LastName)
                              .FirstOrDefaultAsync();

            var notificationToCreate = new NotificationCreateViewModel
            {
                NotificationTypeId  = (int)NotificationType.Property,
                NotificationPicture = property.Images.Select(i => i.ImagePath).FirstOrDefault(),
                NotificationLink    = "/properties/details?id=" + rental.Id + "&isrentsearching=True",
                NotificationText    = creatorName + " добави имот: " + property.PropertyName
            };

            await _notificationCreator.CreateGlobalNotification(notificationToCreate, property.AgentId);

            #endregion

            return(new EditRentalInfoForPropertyViewModel
            {
                UnitCount = rental.UnitCount,
                UnitTypeId = rental.UnitTypeId,
                // ReSharper disable once PossibleInvalidOperationException
                // Rental Create ViewModel guarantee for value here
                RentalPrice = (decimal)rental.RentalPrice,
                RentalInfoId = rental.Id,
                // ReSharper disable once PossibleInvalidOperationException
                RentalPricePeriodId = (int)rental.RentalPricePeriodId,
                AdditionalInfo = rental.AdditionalDescription,
                Attributes = rental.Attributes.Select(ra => new AttributesKeyValueViewModel
                {
                    Key = ra.Key,
                    Value = ra.Value
                }).ToList(),
                RentalExtras = rental.Extras.Select(re => new ExtraCheckBoxViewModel
                {
                    ExtraId = re.ExtraId,
                    ExtraName = re.ExtraName,
                    IsChecked = true
                }).ToList()
            });
        }