// functionality to reset password private void btCheckCode_Click(object sender, EventArgs e) { if (tbCodeFromEmail.Text != string.Empty) { if (tbEnterCode.Text != string.Empty && tbEnterCode.Text == tbCodeFromEmail.Text) { var userRepo = new BaseRepository <User>(); var emailId = new BaseRepository <UserEmail>(userRepo.ContextDb) .Get(x => x.EmailName == tbEmail.Text.Trim()).First().User.Id; tbNewPass.Text = RandomStrings.TempPassword(); var user = userRepo.Get(x => x.Id == emailId) .Include(x => x.UserType) .First(); user.Password = tbNewPass.Text.GetMd5Hash(); userRepo.Update(user); MessageContainer.DisplayInfo("Password has been changed. Please login using new password", "Success"); } else { MessageContainer.DisplayError("Invalid code from email", "Error"); } } else { MessageContainer.DisplayError("Request reser code first", "Error"); } }
// functionality to deactivate user's account private void btDeactivateAccount_Click(object sender, EventArgs e) { var userRepo = new BaseRepository <User>(); var currentUser = userRepo.Get(x => x.Id == _currentUserId) .Include(x => x.UserType) .First(); if (currentUser.IsActiveStatus) { DialogResult result = MessageBox.Show(@"Do you want to deactivate your account?" + Environment.NewLine + @"You will need to contact our customer survice to reactivate it", @"Deactivate account", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { currentUser.IsActiveStatus = false; userRepo.Update(currentUser); MessageContainer.DisplayInfo("Your account has been deactivated.\n" + "You should contact our managers to reactivate it in the future.", "Success"); } } else { MessageContainer.DisplayInfo("Your account is already deactivated\n" + "You should contact our managers to reactivate it", "Account status"); } }
// the method saves newly-created user private void SaveAddedDetails() { var md5Hash = MD5.Create(); var userRepo = new BaseRepository <User>(); userRepo.Insert(new User() { FirstName = tbFirstName.Text, LastName = tbLastName.Text, Login = tbLogin.Text, Password = tbPassword.Text.GetMd5Hash(), UserType = new BaseRepository <UserType>(userRepo.ContextDb) .Get(x => x.TypeName == "Client").FirstOrDefault() }); MessageContainer.DisplayInfo("Created new user successfully.\nYou may log in with new credentials.", "New user has been created"); }
// method to change chosen user's type private void cbUserType_SelectedIndexChanged(object sender, EventArgs e) { if (!_userType) { var userRepo = new BaseRepository <User>(); var user = userRepo.Get(x => x.Id == _selectedUser) .Include(x => x.UserType) .First(); // check if the user is active // in case he is not, show error message that it is forbidden to change type for inactive user if (user.IsActiveStatus) { var newTypeId = (int)Enum.Parse(typeof(EUserType), cbUserType.SelectedItem.ToString()); var newType = new BaseRepository <UserType>(userRepo.ContextDb) .Get(x => x.Id == newTypeId).FirstOrDefault(); if (newTypeId != user.UserType.Id && MessageBox.Show(@"Do you want to change user type from " + user.UserType.TypeName + @" to " + newType?.TypeName, @"Change user type", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { user.UserType = newType; userRepo.Update(user); MessageContainer.DisplayInfo("Next session for this user will start with new rights", "User type has been updated"); } else { cbUserType.SelectedIndex = user.UserType.Id; } } else { _userType = true; MessageContainer.DisplayError("Changing user type for inactive user is forbidden." + Environment.NewLine + "Activate user first", "Error"); cbUserType.SelectedIndex = user.UserType.Id; } _userType = false; } }
internal static void ParseFavouriteMedia(string xmlFileName, int currentUserId) { var doc = new XmlDocument(); doc.Load(xmlFileName); var xmlChannelsNodeList = doc.SelectNodes("tv/channel"); var xmlTvShowsNodeList = doc.SelectNodes("tv/programme"); var channelRepo = new BaseRepository <Channel>(); var tvShowRepo = new BaseRepository <TvShow>(channelRepo.ContextDb); if (channelRepo.GetAll().Any() == false) { MessageContainer.DisplayInfo("You can't load favourite media. Channels table is clear!!!" + Environment.NewLine + "Load base about channels first!!!", "Error"); return; } if (tvShowRepo.GetAll().Any() == false) { MessageContainer.DisplayInfo("You can't load favourite media. TV programmers table is clear!!!" + Environment.NewLine + "Load base about TvShows first!!!", "Error"); return; } var currentUser = new BaseRepository <User>(channelRepo.ContextDb) .Get(u => u.Id == currentUserId).FirstOrDefault(); var orderChanellList = new List <OrderChannel>(); var userScheduleList = new List <UserSchedule>(); if (xmlChannelsNodeList != null || xmlTvShowsNodeList != null) { var userIdInFile = xmlChannelsNodeList?.Item(0)?.ChildNodes[2].InnerText.GetInt(); if (userIdInFile != currentUserId) { MessageContainer.DisplayError("This file include not your favourite media!!! Sorry", "Error"); return; } foreach (XmlNode chaNode in xmlChannelsNodeList) { try { if (chaNode.Attributes == null || chaNode.Attributes["favourite"].Value != "true") { MessageContainer.DisplayError("This file doesn't contains favourite media!!!", "WRONG FILE!"); return; } } catch (Exception) { MessageContainer.DisplayError("This file doesn't contains favourite media!!!", "WRONG FILE!"); return; } } //check for right format saved *.xml file (for programmers) if (xmlTvShowsNodeList != null && xmlTvShowsNodeList.Cast <XmlNode>().Any(showNode => showNode.Attributes == null || showNode.Attributes["favourite"].Value != "true")) { MessageContainer.DisplayError("This file doesn't contains favourite media!!!", "WRONG FILE!"); return; } //add saved channels to list orderChanellList.AddRange(from XmlNode channelNode in xmlChannelsNodeList select channelNode.ChildNodes[0].InnerText.GetInt() into chanOrigId select new OrderChannel { Channel = channelRepo.Get(ch => ch.OriginalId == chanOrigId).FirstOrDefault(), User = currentUser }); if (xmlTvShowsNodeList != null) { //add saved tvShows to list userScheduleList.AddRange(from XmlNode tvShowNode in xmlTvShowsNodeList select tvShowNode.ChildNodes[0].InnerText.GetInt() into showId select new UserSchedule { User = currentUser, TvShow = tvShowRepo.Get(sh => sh.Id == showId).FirstOrDefault(), //DueDate = currentOrder?.DueDate ?? DateTime.Now.AddDays(7) }); } if (orderChanellList.Any(o => o.Channel == null) || userScheduleList.Any(sc => sc.TvShow == null)) { MessageContainer.DisplayInfo("Saved channels or TV-programmers don't meet current database.", "Info"); } else { var ordChannelRepo = new BaseRepository <OrderChannel>(channelRepo.ContextDb); ordChannelRepo.AddRange(orderChanellList); var schedRepo = new BaseRepository <UserSchedule>(channelRepo.ContextDb); schedRepo.AddRange(userScheduleList); MessageContainer.DisplayInfo("Saved schedule was read good.", "Info"); } } else { MessageContainer.SomethingWrongInFileLoad(); } }
private void btMakeOrder_Click(object sender, EventArgs e) { var userRepo = new BaseRepository <User>(); var accountRepo = new BaseRepository <Account>(userRepo.ContextDb); var orderRepo = new BaseRepository <Order>(userRepo.ContextDb); var paymentRepo = new BaseRepository <Payment>(userRepo.ContextDb); var ordChanRepo = new BaseRepository <OrderChannel>(userRepo.ContextDb); var ordServicesRepo = new BaseRepository <OrderService>(userRepo.ContextDb); var user = userRepo.Get(u => u.Id == CurrentUserId).FirstOrDefault(); var balance = accountRepo.Get(b => b.User.Id == CurrentUserId).FirstOrDefault(); var notPaidChannels = ordChanRepo.Get(ch => ch.Order == null || ch.Order.IsPaid == false).ToList(); var notPaidServices = ordServicesRepo.Get(s => s.Order == null || s.Order.IsPaid == false).ToList(); if (notPaidServices.Count == 0 && notPaidChannels.Count == 0) { MessageContainer.DisplayError("No channels and/or services to pay", "Attention!!!"); return; } if (balance?.IsActiveStatus == false) { MessageContainer.DisplayError("Account is diactivated!" + Environment.NewLine + "Please connect to administrator", "Attention!!!"); return; } CurrentOrderId = GetNewOrderId(OrderPrice); //return order created in this method (by GetNewOrder) var order = orderRepo.Get(b => b.Id == CurrentOrderId).FirstOrDefault(); if (balance != null && order != null && balance.Balance >= order.TotalPrice) { if (order.IsPaid) { MessageContainer.DisplayInfo("Order is paid already", "Attention!!!"); return; } order.IsPaid = true; order.DateOrder = DateTime.Now; order.FromDate = DateTime.Now; order.DueDate = DateTime.Now.AddDays(7); order.User = user; balance.Balance -= order.TotalPrice; orderRepo.Update(order); accountRepo.Update(balance); tbTotalPrice.Text = @"0.00"; tbAccountBalance.Text = balance.Balance.ToString(CultureInfo.CurrentCulture); var payment = new Payment() { Id = 1, Date = order.DateOrder, Order = order, Summ = order.TotalPrice }; paymentRepo.Insert(payment); var payingChannels = ordChanRepo.Get(ch => ch.User.Id == CurrentUserId && ch.Order == null).ToList(); foreach (var chann in payingChannels) { chann.Order = order; chann.Channel = chann.Channel; ordChanRepo.Update(chann); } var payingServices = ordServicesRepo.Get(sr => sr.User.Id == CurrentUserId && sr.Order == null).ToList(); foreach (var serv in payingServices) { serv.Order = order; serv.AdditionalService = serv.AdditionalService; ordServicesRepo.Update(serv); } MessageContainer.DisplayInfo( $"Order #{order.Id} worth {order.TotalPrice} {lbUAH.Text}" + $" was paid succesfully. {Environment.NewLine }" + $"Total count of channels {order.OrderChannels?.Count ?? 0.00}", @"Succesfull payment"); } else { MessageContainer.DisplayError("Your balance is low!!!", "Error"); } }
public bool ValidateControls() { var errorMessage = "Error(s):" + Environment.NewLine; var isValidCardDatas = true; mtbCardNumber.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals; var cardNumber = mtbCardNumber.Text.Trim(); mtbCardNumber.TextMaskFormat = MaskFormat.IncludeLiterals; if (cardNumber == string.Empty || cardNumber.Length < 16) { errorMessage += "Card number cannot be empty or shorter than 16 digits" + Environment.NewLine; isValidCardDatas = false; } int month; int year; int.TryParse(mtbDateCard.Text.Split('.').First().Trim(), out month); int.TryParse(mtbDateCard.Text.Split('.').Last().Trim(), out year); if (month <= 0 || month > 12 || year < (DateTime.Now.Year - 2000)) { errorMessage += $"Entered wrong month - '{month}' or year - '{year}'" + Environment.NewLine; isValidCardDatas = false; } int cvvCode; int.TryParse(mtbCvvCode.Text.Trim(), out cvvCode); if (mtbCvvCode.Text.Trim() == string.Empty || mtbCvvCode.Text.Trim().Length != 3) { errorMessage += "CVV-code cannot be empty or length not than 3 digits" + Environment.NewLine; isValidCardDatas = false; } double summ; var isDouble = double.TryParse(tbSummRecharge.Text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out summ); if (isDouble == false || tbSummRecharge.Text.Trim() == string.Empty || summ <= 0.0) { errorMessage += "Summ to recharge entered incorrect" + Environment.NewLine; isValidCardDatas = false; } if (isValidCardDatas) { var accountRepo = new BaseRepository <Account>(); var userAcc = accountRepo.Get(a => a.User.Id == CurrentUserId).FirstOrDefault(); if (userAcc != null) { userAcc.Balance += summ; accountRepo.Update(userAcc); } else { var user = new BaseRepository <User>(accountRepo.ContextDb) .Get(u => u.Id == CurrentUserId).FirstOrDefault(); var newAccount = new Account { Balance = summ, Comment = "automatically created new account", IsActiveStatus = true, User = user }; accountRepo.Insert(newAccount); } MessageContainer.DisplayInfo($"Your account was succesfull charged on {summ} UAH", "Succesfull"); } else { MessageContainer.DisplayError(errorMessage, "Invalid input"); } return(isValidCardDatas); }