public async Task <ActionResult> Edit(PropertySearchEditViewModel model) { if (!ModelState.IsValid) { return(Json(ModelState.ToDictionary())); } var editedPropertySearch = await _propertySearchServices.EditAsync(model, User.Identity.GetUserId()); return(Json(editedPropertySearch)); }
public async Task <PropertySearchListViewModel> EditAsync(PropertySearchEditViewModel model, string agentId) { if (string.IsNullOrEmpty(agentId)) { throw new ArgumentException("Не е установен брокерът задаващ въпроса!"); } var propertySearchToEdit = await _dbContext.PropertySearches .Include(p => p.PersonSearcher) .FirstOrDefaultAsync(p => p.Id == model.Id) ?? throw new ContentNotFoundException("Не е намерено търсенето, което искате да редактирате"); if (propertySearchToEdit.AgentId != agentId && !await _userManager.IsInRoleAsync(agentId, Enum.GetName(typeof(Role), Role.Administrator))) { throw new NotAuthorizedUserException("Нямате право да изтривате чуждо търсене!"); } _dbContext.PropertySearches.Attach(propertySearchToEdit); propertySearchToEdit.CityId = model.CityId; propertySearchToEdit.AdditionalInformation = model.AdditionalInformation; propertySearchToEdit.AreaInSquareMeters = model.AreaInSquareMeters; propertySearchToEdit.Areas = model.Areas; propertySearchToEdit.IsRentSearch = model.IsRentSearch; propertySearchToEdit.PriceFrom = model.PriceFrom; propertySearchToEdit.PriceTo = model.PriceTo; var unitTypeIds = model.UnitTypes.Select(u => u.PropertyTypeId); propertySearchToEdit.UnitTypes = new HashSet <PropertyTypes>(await _dbContext.PropertyTypes .Where(pt => unitTypeIds.Any(ut => pt.PropertyTypeId == ut)) .ToListAsync()); propertySearchToEdit.PersonSearcher.Name = model.PersonSearcher.Name; propertySearchToEdit.PersonSearcher.PhoneNumber = model.PersonSearcher.PhoneNumber; propertySearchToEdit.PersonSearcher.Email = model.PersonSearcher.Email; propertySearchToEdit.PersonSearcher.AdditionalInformation = model.PersonSearcher.AdditionalInformation; await _dbContext.SaveChangesAsync(); return(await GetAsync(propertySearchToEdit.Id)); }