public bool Save(Position position) { var entity = PositionMapper.ToEntity(position); if (entity.Id != null) { _roleRepository.Update(entity); return true; } _roleRepository.Add(entity); return true; }
private void InitializeView() { SearchForPositionCommand = new RelayCommand(() => { PositionsFoundHashes.Clear(); var found = _positionService.Find(SearchField); if (PositionsFound.Count > 0) { var index = 0; do { if (PositionsFound[index] == SelectedPosition) index++; else PositionsFound.RemoveAt(index); } while (index < PositionsFound.Count); } foreach(var position in found) { if (SelectedPosition == null || position.Id != SelectedPosition.Id) PositionsFound.Add(position); } var hashes = PositionsFound.Select(x => (long)(x.Title + x.Description).GetHashCode()); PositionsFoundHashes.AddRange(hashes); }, () => { return true; }); SavePositionCommand = new RelayCommand( () => { _positionService.Save(SelectedPosition); PositionsFoundHashes.Clear(); var hashes = PositionsFound.Select(x => (long)(x.Title + x.Description).GetHashCode()); PositionsFoundHashes.AddRange(hashes); SearchField = SelectedPosition.Title; SavePositionCommand.RaiseCanExecuteChanged(); } ,() => { var selectedPositionHash = SelectedPosition == null ? new Random().Next() : (SelectedPosition.Title + SelectedPosition.Description).GetHashCode(); return SelectedPosition != null && SelectedPosition.Title != null && SelectedPosition.Title.Length > 1 && SelectedPosition.Description != null && SelectedPosition.Description.Length > 1 && !PositionsFoundHashes.Contains(selectedPositionHash); } ); ClearPositionCommand = new RelayCommand(() => { PositionsFound.Clear(); SelectedPosition = new Position(); SearchField = ""; }); UpdateCanSaveCommand = new RelayCommand(() => { SavePositionCommand.RaiseCanExecuteChanged(); }); ExpandComboBoxCommand = new RelayCommand(() => { ShowMatchingPositions = true; }); SelectedPosition = new Position(); PositionsFound = new ObservableCollection<Position>(); }