public async Task <PropertySearchListViewModel> CreateAsync(PropertySearchCreateViewModel model, string agentId) { if (string.IsNullOrEmpty(agentId)) { throw new ArgumentException("Не е установен брокерът задаващ въпроса!"); } if (!(await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Agent)) || await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Administrator)))) { throw new NotAuthorizedUserException("Потребителят няма право на това действие! Само админи и брокери имат достъп !"); } PropertySearches propertySearch = new PropertySearches { CityId = model.CityId, AdditionalInformation = model.AdditionalInformation, AgentId = agentId, AreaInSquareMeters = model.AreaInSquareMeters, Areas = model.Areas, IsRentSearch = model.IsRentSearch, PriceFrom = model.PriceFrom, PriceTo = model.PriceTo, UnitTypes = new HashSet <PropertyTypes>(await _dbContext.PropertyTypes .Where(pt => model.UnitTypeIds.Any(ut => pt.PropertyTypeId == ut)) .ToListAsync()), PersonSearcher = new PersonSearcher { Name = model.PersonSearcher.Name, PhoneNumber = model.PersonSearcher.PhoneNumber, Email = model.PersonSearcher.Email, AdditionalInformation = model.PersonSearcher.AdditionalInformation } }; _dbContext.PropertySearches.Add(propertySearch); await _dbContext.SaveChangesAsync(); #region Create notification var creatorName = await _dbContext.Users .Where(u => u.Id == propertySearch.AgentId) .Select(a => a.FirstName + " " + a.LastName) .FirstOrDefaultAsync(); var notificationToCreate = new NotificationCreateViewModel { NotificationTypeId = (int)NotificationType.Property, NotificationLink = "/propertysearches/details?id=" + propertySearch.Id, NotificationText = creatorName + " добави търсене на имот с цена " + propertySearch.PriceFrom + "лв." + " - " + propertySearch.PriceTo + "лв." }; await _notificationCreator.CreateGlobalNotification(notificationToCreate, propertySearch.AgentId); #endregion return(await GetAsync(propertySearch.Id)); }
public async Task <ActionResult> Create(PropertySearchCreateViewModel model) { if (!ModelState.IsValid) { return(Json(ModelState.ToDictionary())); } var createdPropertySearch = await _propertySearchServices.CreateAsync(model, User.Identity.GetUserId()); return(Json(createdPropertySearch)); }