private void UpdateLiteratureTypes() { using (var context = new BookOrdersContext()) { for (int i = LiteratureTypes.Count - 1; i >= 0; --i) { if (context.LiteratureTypes.Find(LiteratureTypes[i].Id) == null) { LiteratureTypes.Remove(LiteratureTypes[i]); } } foreach (LiteratureType literatureType in context.LiteratureTypes) { if (LiteratureTypes.FirstOrDefault(element => element.Id == literatureType.Id) is LiteratureType tempLiteratureType) { tempLiteratureType.Name = literatureType.Name; } else { LiteratureTypes.Add(literatureType); } } } }
private static void PreviewKeyDown(object eventArgs) { var tempEventArgs = (object[])eventArgs; if (!(tempEventArgs[0] is KeyEventArgs e && tempEventArgs[1] is DataGrid sender)) { throw new ArgumentException("Bad arguments in PreviewKeyDown"); } switch (e.Key) { case Key.Escape: sender.CancelEdit(); sender.CancelEdit(); return; case Key.Delete: if (!(sender.SelectedItem is Genre genre)) { return; } using (var context = new BookOrdersContext()) { context.Genres.Attach(genre); context.Genres.Remove(genre); context.SaveChanges(); } return; } }
private void RegisterUser(PasswordBox sender) { using (var context = new BookOrdersContext()) { using (DbContextTransaction transaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead)) { try { NewUser.Login = Regex.Replace(NewUser.Login.Trim(), @"\s+", " "); NewUser.Password = _BCrypt.HashPassword(sender.Password.Trim(), 12); context.Users.Add(NewUser); context.SaveChanges(); transaction.Commit(); } catch (Exception e) { Exception innerException = e; while (innerException?.InnerException != null) { innerException = innerException.InnerException; } transaction.Rollback(); MessageBox.Show(innerException?.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } RegisterConfirmed?.Invoke(NewUser); } } }
public AddEntityViewModel() { using (var context = new BookOrdersContext()) { Books = new ObservableCollection <Book>(context.Books); Authors = new ObservableCollection <Author>(context.Authors); Publishers = new ObservableCollection <Publisher>(context.Publishers); NewPublisherId = (int)context.Database.SqlQuery <decimal>("SELECT last_value FROM publishers_id_seq") .First() + 1; } NewBook = new Book(); NewAuthor = new Author(); NewPublisher = new Publisher(); NewBookAndAuthor = new BookAndAuthor(); NewBook.InitializeValidator(null, new List <Publisher>(Publishers) { new Publisher { Id = NewPublisherId } }); NewAuthor.InitializeValidator(); NewPublisher.InitializeValidator(); NewBookAndAuthor.InitializeValidator(); AddNewEntityCommand = new DelegateCommand <object>(AddNewEntity, CanAddNewEntity); NewBook.PropertyChanged += delegate { if (NewBook.Validator.IsValid) { NewPublisher.Id = ISBNUtils.GetPublisherId(NewBook.ISBN); } }; NewPublisher.PropertyChanged += OnNewPublisherOnPropertyChanged; }
private void AuthenticationCompleted() { BookOrdersContext.RaiseUserStatusLoaded(_currentUser); IsAdmin = _currentUser?.Role == "ADMIN"; AuthorsViewModel.SelectedAuthorChanged += AuthorsViewModel_SelectedAuthorChanged; PublishersViewModel.SelectedAuthorChanged += PublishersViewModel_SelectedAuthorChanged; _mainWindow.Show(); }
protected static void PreviewKeyDown(object eventArgs) { var tempEventArgs = (object[])eventArgs; if (!(tempEventArgs[0] is KeyEventArgs e && tempEventArgs[1] is DataGrid sender)) { throw new ArgumentException("Bad arguments in PreviewKeyDown"); } switch (e.Key) { case Key.Escape: sender.CancelEdit(); sender.CancelEdit(); return; case Key.Delete: if (!(sender.SelectedItem is TEntity entity)) { return; } using (var context = new BookOrdersContext()) { try { PropertyInfo property = context.GetType().GetProperty(typeof(TEntity).Name.Replace("And", "sAnd") + 's'); if (property != null) { ((DbSet <TEntity>)property.GetValue(context)).Attach(entity); ((DbSet <TEntity>)property.GetValue(context)).Remove(entity); } context.SaveChanges(); } catch (DbUpdateException exception) { if (!(exception.InnerException is UpdateException innerException)) { throw; } if (!(innerException.InnerException is Npgsql.PostgresException innerInnerException)) { throw; } if (innerInnerException.SqlState == "23503") { MessageBox.Show(innerInnerException.MessageText, "Error", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; } else { throw; } } } return; } }
protected override void BookOrdersContext_NewEntityAdded() { PageLoaded(null); using (var context = new BookOrdersContext()) { UpdateEntity(Entities, context.BooksAndAuthors.ToList(), Books, Authors, Entities); foreach (BookAndAuthor bookAndAuthor in Entities) { bookAndAuthor.AuthorBookNumberText = bookAndAuthor.AuthorBookNumber.ToString(); } } }
public UsersViewModel() { using (var context = new BookOrdersContext()) { Entities = new ObservableCollection <User>(context.Users); } foreach (User user in Entities) { user.InitializeValidator(Entities); } }
public AuthorsViewModel() { using (var context = new BookOrdersContext()) { Entities = new ObservableCollection <Author>(context.Authors); } foreach (Author author in Entities) { author.InitializeValidator(Entities); } }
private void UpdateDependentEntities() { using (var context = new BookOrdersContext()) { foreach (DbEntityEntry entity in context.ChangeTracker.Entries()) { entity.Reload(); } UpdateEntity(Books, context.Books.ToList()); UpdateEntity(Authors, context.Authors.ToList()); } }
protected override void BookOrdersContext_NewEntityAdded() { using (var context = new BookOrdersContext()) { foreach (DbEntityEntry entity in context.ChangeTracker.Entries()) { entity.Reload(); } UpdateEntity(Entities, context.Books.ToList(), Entities, Publishers); } PageLoaded(null); }
public AuthenticationViewModel() { using (var context = new BookOrdersContext()) { NewUser = new User { Password = "******", Role = "USER" }; } SignInCommand = new DelegateCommand <PasswordBox>(SignIn); SignUpCommand = new DelegateCommand <object>(SignUp); SkipSignInCommand = new DelegateCommand <object>(SkipSignIn); }
public RegistrationViewModel() { using (var context = new BookOrdersContext()) { NewUser = new User { Password = "******", Role = "USER", RegistrationDate = DateTime.Now }; NewUser.InitializeValidator(context.Users.ToList()); } PasswordChangedCommand = new DelegateCommand <PasswordBox>(PasswordChanged); PasswordLostFocusCommand = new DelegateCommand <object>(PasswordLostFocus); RegisterUserCommand = new DelegateCommand <PasswordBox>(RegisterUser, CanRegisterUser); }
private static void PreviewKeyDown(object eventArgs) { var tempEventArgs = (object[])eventArgs; if (!(tempEventArgs[0] is KeyEventArgs e && tempEventArgs[1] is DataGrid sender)) { throw new ArgumentException("Bad arguments in PreviewKeyDown"); } switch (e.Key) { case Key.Escape: sender.CancelEdit(); sender.CancelEdit(); return; case Key.Delete: if (!(sender.SelectedItem is LiteratureType literatureType)) { return; } using (var context = new BookOrdersContext()) { try { context.LiteratureTypes.Attach(literatureType); context.LiteratureTypes.Remove(literatureType); context.SaveChanges(); } catch (DbUpdateException exception) { if (!(exception.InnerException is UpdateException innerException)) { throw; } if (!(innerException.InnerException is Npgsql.PostgresException innerInnerException)) { throw; } if (innerInnerException.SqlState == "23503") { MessageBox.Show("There are some genres that link to this literature type. Delete them first.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; } else { throw; } } } return; } }
private void SignIn(PasswordBox passwordBox) { using (var context = new BookOrdersContext()) { User possibleUser = context.Users.FirstOrDefault(user => user.Login == NewUser.Login); if (possibleUser != null && _BCrypt.Verify(passwordBox.Password.Trim(), possibleUser.Password)) { SignInConfirmed?.Invoke(possibleUser); } else { MessageBox.Show("Invalid Login or Password", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public BooksViewModel() { using (var context = new BookOrdersContext()) { Entities = new ObservableCollection <Book>(context.Books); Publishers = new ObservableCollection <Publisher>(context.Publishers); } foreach (Book book in Entities) { book.InitializeValidator(Entities, Publishers); } DataGridCellEditEndingCommand = new DelegateCommand <DataGridCellEditEndingEventArgs>(DataGridCellEditEnding); }
private void ShowAllInfoAboutSelectedAuthor(object sender) { IList <BookAndAuthor> results; using (var context = new BookOrdersContext()) { results = (from bookAndAuthor in context.BooksAndAuthors where bookAndAuthor.AuthorId == _selectedAuthor.Id select bookAndAuthor) .Include(entity => entity.Author) .Include(entity => entity.Book) .Include(entity => entity.Book.Publisher) .ToList(); } ShowAllInfoBooksAndAuthorsRaised?.Invoke(results); }
public BooksAndAuthorsViewModel() { using (var context = new BookOrdersContext()) { Entities = new ObservableCollection <BookAndAuthor>(context.BooksAndAuthors); Books = new ObservableCollection <Book>(context.Books); Authors = new ObservableCollection <Author>(context.Authors); } foreach (BookAndAuthor booksAndAuthors in Entities) { booksAndAuthors.AuthorBookNumberText = booksAndAuthors.AuthorBookNumber.ToString(); booksAndAuthors.InitializeValidator(Books, Authors, Entities); } DataGridCellEditEndingCommand = new DelegateCommand <DataGridCellEditEndingEventArgs>(DataGridCellEditEnding); }
protected static void DataGridRowEditEnding(object eventArgs) { var tempEventArgs = (object[])eventArgs; if (!(tempEventArgs[0] is DataGridRowEditEndingEventArgs e && tempEventArgs[1] is DataGrid sender)) { throw new ArgumentException("Bad arguments in PreviewKeyDown"); } if (e.EditAction == DataGridEditAction.Cancel || !(e.Row.Item is TEntity entity) || !entity.Validator.IsValid || !entity.IsDirty) { return; } using (var context = new BookOrdersContext()) { try { if (e.Row.IsNewItem) { PropertyInfo property = context.GetType().GetProperty(typeof(TEntity).Name.Replace("And", "sAnd") + 's'); if (property != null) { ((DbSet <TEntity>)property.GetValue(context)).Add(entity); } } else { context.Entry(entity).State = EntityState.Modified; } context.SaveChanges(); } catch (Exception exception) { sender.CancelEdit(); sender.CancelEdit(); Exception innerException = exception; while (innerException?.InnerException != null) { innerException = innerException.InnerException; } MessageBox.Show(innerException?.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public LiteratureTypesViewModel() { using (var context = new BookOrdersContext()) { LiteratureTypes = new ObservableCollection <LiteratureType>(context.LiteratureTypes); } foreach (LiteratureType literatureType in LiteratureTypes) { literatureType.InitializeValidator(LiteratureTypes); } DataGridAddingNewItemCommand = new DelegateCommand <AddingNewItemEventArgs>(DataGridAddingNewItem); PreviewKeyDownCommand = new DelegateCommand <object>(PreviewKeyDown); DataGridRowEditEndingCommand = new DelegateCommand <DataGridRowEditEndingEventArgs>(DataGridRowEditEnding); }
private void AddNewEntity(object sender) { using (var context = new BookOrdersContext()) { using (DbContextTransaction transaction = context.Database.BeginTransaction(IsolationLevel.RepeatableRead)) { try { if (!Publishers.Any(NewPublisher.Equals)) { context.Publishers.Add(NewPublisher); } NewBook.PublisherId = NewPublisher.Id; if (!Books.Any(NewBook.Equals)) { context.Books.Add(NewBook); } if (!Authors.Any(NewAuthor.Equals)) { NewAuthor.Id = 0; context.Authors.Add(NewAuthor); } NewBookAndAuthor.AuthorId = NewAuthor.Id; NewBookAndAuthor.ISBN = NewBook.ISBN; context.BooksAndAuthors.Add(NewBookAndAuthor); context.SaveChanges(); transaction.Commit(); } catch (Exception e) { Exception innerException = e; while (innerException?.InnerException != null) { innerException = innerException.InnerException; } transaction.Rollback(); MessageBox.Show(innerException?.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } AddedNewEntity?.Invoke(); } } }
private static void DataGridRowEditEnding(DataGridRowEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Cancel || !(e.Row.Item is Genre genre) || !genre.Validator.IsValid || !genre.IsDirty) { return; } using (var context = new BookOrdersContext()) { if (e.Row.IsNewItem) { context.Genres.Add(genre); } else { context.Entry(genre).State = EntityState.Modified; } context.SaveChanges(); } }
public GenresViewModel() { using (var context = new BookOrdersContext()) { Genres = new ObservableCollection <Genre>(context.Genres); LiteratureTypes = new ObservableCollection <LiteratureType>(context.LiteratureTypes); } foreach (Genre genre in Genres) { genre.PopularityText = genre.Popularity.ToString(); genre.InitializeValidator(Genres); } DataGridAddingNewItemCommand = new DelegateCommand <AddingNewItemEventArgs>(DataGridAddingNewItem); PreviewKeyDownCommand = new DelegateCommand <object>(PreviewKeyDown); DataGridRowEditEndingCommand = new DelegateCommand <DataGridRowEditEndingEventArgs>(DataGridRowEditEnding); PageLoadedCommand = new DelegateCommand <object>(PageLoaded); }
private void AddEntityViewModel_AddedNewEntity() { BookOrdersContext.RaiseNewEntityAdded(); _addEntityWindow.Close(); }
protected override void BookOrdersContext_NewEntityAdded() { using (var context = new BookOrdersContext()) { UpdateEntity(Entities, context.Authors.ToList(), Entities); } }
private void UpdateDependentEntities() { using (var context = new BookOrdersContext()) { UpdateEntity(Publishers, context.Publishers.ToList()); } }