private void CloseBtn_Click(object sender, RoutedEventArgs e) { var mb = new FamilyOrganizerMessageBox("Refresh the page to see the changes"); mb.Show(); Close(); }
private void Button_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(TranSum.Text)) { var mb = new FamilyOrganizerMessageBox("The sum is required"); mb.Show(); return; } if (!_intAndFloat.IsMatch(TranSum.Text)) { var mb = new FamilyOrganizerMessageBox("The sum is invalid"); mb.Show(); return; } var sum = Convert.ToDouble(TranSum.Text); var toUser = _currentUser; var transaction = new Transaction { Type = "Deposit", Date = DateTime.Now, Sum = sum, ToUser = toUser }; _context.Transactions.Add(transaction); _currentUser.Balance += sum; _context.Entry(_currentUser).State = EntityState.Modified; var currentBalanceEntry = _context.Balances.OrderByDescending(b => b.Date).FirstOrDefault(); if (currentBalanceEntry == null) { currentBalanceEntry = new Balance { Date = DateTime.Now } } ; var currentBalance = _context.AppUsers.Sum(u => u.Balance); if (currentBalanceEntry.Date.Day == transaction.Date.Day && _context.Balances.Count() > 0) { _context.Balances.Remove(currentBalanceEntry); } _context.Balances.Add(new Balance { Date = transaction.Date, CurrentBalance = currentBalance + sum }); _context.SaveChanges(); TranSum.Text = ""; }
private async void DeleteUser_Click(object sender, RoutedEventArgs e) { var currentUser = DataContext as AppUser; if (_context == null) { var a = VisualTreeHelper.GetParent(this); while (!(a is Family)) { a = VisualTreeHelper.GetParent(a); } _context = (a as Family)._context; } if (!await _context.AppUsers.AnyAsync(u => u.Id == currentUser.Id)) { return; } _context.AppUsers.Remove(currentUser); var currentBalance = await _context.AppUsers.Where(u => u.UserName != currentUser.UserName).SumAsync(u => u.Balance); var currentBalanceEntry = await _context.Balances.OrderByDescending(b => b.Date).FirstOrDefaultAsync(); if (currentBalanceEntry == null) { currentBalanceEntry = new Balance { CurrentBalance = 0, Date = DateTime.Now } } ; if (currentBalanceEntry.Date.Day == DateTime.Now.Day && _context.Balances.Count() > 0) { _context.Balances.Remove(currentBalanceEntry); } _context.Balances.Add(new Balance { Date = DateTime.Now, CurrentBalance = currentBalance }); await _context.SaveChangesAsync(); var mb = new FamilyOrganizerMessageBox("Refresh the page to see the changes"); mb.Show(); }
private async void saveLogin_Click(object sender, RoutedEventArgs e) { var username = TextBoxCurrentUsername.Text; var password = TextBoxPassword.Password; if (string.IsNullOrWhiteSpace(username)) { var mb = new FamilyOrganizerMessageBox("Username cannot be empty"); mb.Show(); return; } if (string.IsNullOrWhiteSpace(password)) { var mb = new FamilyOrganizerMessageBox("Invalid password"); mb.Show(); return; } if (_context.AppUsers.Any(u => u.UserName == username && u.UserName != _currentUser.UserName)) { var mb = new FamilyOrganizerMessageBox("Username is taken"); mb.Show(); return; } var hashedPassword = PasswordHash.CreateHash(password); if (password != "k*7m\\Z!~") { _currentUser.Password = hashedPassword; } _currentUser.UserName = username; _currentUser.Photo = await _context.Photos.FirstOrDefaultAsync(p => p.Id == PhotoId); _context.Entry(_currentUser).State = EntityState.Modified; if (await _context.SaveChangesAsync() > 0) { var mb = new FamilyOrganizerMessageBox("Your changes have been applied"); mb.Show(); } }
private async void Button_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(UsernameLogin.Text) || string.IsNullOrEmpty(PasswordLogin.Password)) { var mb = new FamilyOrganizerMessageBox("Enter both login and password"); mb.Show(); return; } // после проверок var username = UsernameLogin.Text; var user = await context.AppUsers.FirstOrDefaultAsync(u => u.UserName == username); if (user == null) { var mb = new FamilyOrganizerMessageBox("Check username and password"); mb.Show(); return; } var password = PasswordLogin.Password; if (PasswordHash.ValidatePassword(password, user.Password)) { var mw = new MainWindow(context, user); mw.Show(); Close(); } else { var mb = new FamilyOrganizerMessageBox("Check username and password"); mb.Show(); return; } }
private async void Button_Click(object sender, RoutedEventArgs e) { var username = UsernameRegister.Text; if (string.IsNullOrEmpty(username)) { var mb = new FamilyOrganizerMessageBox("Username is required"); mb.Show(); return; } if (string.IsNullOrEmpty(PasswordRegister.Password)) { var mb = new FamilyOrganizerMessageBox("Password is required"); mb.Show(); return; } if (PasswordRegister.Password.Length < 6) { var mb = new FamilyOrganizerMessageBox("Minimum password length is 6"); mb.Show(); return; } if (_context.AppUsers.Any(u => u.UserName == username)) { var mb = new FamilyOrganizerMessageBox("The username is taken"); mb.Show(); return; } if (_context.AppUsers.Count() == 0) { Checked = true; } var hashedPassword = PasswordHash.CreateHash(PasswordRegister.Password); var user = new AppUser { UserName = username, Password = hashedPassword, Role = Checked ? "Parent" : "Child" }; _context.AppUsers.Add(user); await _context.SaveChangesAsync(); if (_toMainWindow) { var mv = new MainWindow(_context, user); mv.Show(); } else { var mb = new FamilyOrganizerMessageBox("Refresh the page to see the changes"); mb.Show(); } Close(); }
private void Button_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(TranSum.Text)) { var mb = new FamilyOrganizerMessageBox("The sum is required"); mb.Show(); return; } if (!_intAndFloat.IsMatch(TranSum.Text)) { var mb = new FamilyOrganizerMessageBox("The sum is invalid"); mb.Show(); return; } var toUsername = ToUser.SelectedItem?.ToString(); var sum = -Convert.ToDouble(TranSum.Text); var toUser = _currentUser; var type = "Money transfer"; if (_currentUser.Balance < Math.Abs(sum) && (TypeChoice.SelectedItem as ComboBoxItem).Content?.ToString() != "Deposit") { var mb = new FamilyOrganizerMessageBox("You don't have enough money"); mb.Show(); return; } switch ((TypeChoice.SelectedItem as ComboBoxItem)?.Content?.ToString()) { case "Transfer": _currentUser.Balance += sum; _context.Entry(_currentUser).State = EntityState.Modified; toUser = _context.AppUsers.FirstOrDefault(u => u.UserName == toUsername); sum = -sum; break; case "Deposit": sum = -sum; _currentUser.Balance += sum; _context.Entry(_currentUser).State = EntityState.Modified; type = "Deposit"; break; case "Expense": _currentUser.Balance += sum; _context.Entry(_currentUser).State = EntityState.Modified; type = (CategoryChoice.SelectedItem as ComboBoxItem).Content?.ToString(); break; default: return; } var transaction = new Transaction { Type = type, Date = DateTime.Now, Sum = sum, ToUser = toUser }; _context.Transactions.Add(transaction); if (transaction.ToUser != _currentUser) // transfer { toUser.Balance += sum; _context.Entry(toUser).State = EntityState.Modified; _context.Transactions.Add(new Transaction { Type = transaction.Type, Date = transaction.Date, Sum = -transaction.Sum, ToUser = _currentUser }); } else { var currentBalanceEntry = _context.Balances.OrderByDescending(b => b.Date).FirstOrDefault(); if (currentBalanceEntry == null) { currentBalanceEntry = new Balance { Date = DateTime.Now } } ; var currentBalance = _context.AppUsers.Sum(u => u.Balance); if (currentBalanceEntry.Date.Day == transaction.Date.Day && _context.Balances.Count() > 0) { _context.Balances.Remove(currentBalanceEntry); } _context.Balances.Add(new Balance { Date = transaction.Date, CurrentBalance = currentBalance + sum }); } _context.SaveChanges(); TranSum.Text = ""; }