public void AddUser(int id, string userId) { var company = _context.Companies.Find(id); var user = _context.Users.Find(userId); company.AspNetUsers = company.AspNetUsers ?? new List <AspNetUser>(); company.AspNetUsers.Add(user); _context.SaveChanges(); }
public Trader AddTrader(TraderToAdd trader) { var newTrader = context.Traders.Add(new Trader { FirstName = trader.FirstName, LastName = trader.LastName, PhoneNumber = trader.PhoneNumber, Balance = trader.Balance }); context.SaveChanges(); return(newTrader); }
public Share AddShare(ShareToAdd share) { var newShare = context.Shares.Add(new Share { Name = share.Name, Price = share.Price, }); context.SaveChanges(); return(newShare); }
public void AddToRole(string id, string roleId) { var role = _context.Roles.Find(roleId); if (role.AspNetUsers.Any(u => u.Id == id)) { return; } var user = _context.Users.Find(id); role.AspNetUsers = role.AspNetUsers ?? new List <AspNetUser>(); role.AspNetUsers.Add(user); _context.SaveChanges(); }
public Task AddToRoleAsync(AspNetUser user, string roleName) { if (user == null) { throw new ArgumentNullException("user"); } var role = _context.Roles.FirstOrDefault(r => r.Name == roleName); if (role == null) { throw new ArgumentNullException(nameof(role)); } if (role.AspNetUsers.Any(u => u.Id == user.Id)) { return(Task.FromResult(0)); } role.AspNetUsers = role.AspNetUsers ?? new List <AspNetUser>(); role.AspNetUsers.Add(user); _context.SaveChanges(); return(Task.FromResult(0)); }
public ServerLogin Create(ServerLogin login) { if (login == null) { throw new ArgumentNullException(nameof(login)); } if (login.DepartmentId < 1) { throw new ValidationException("Missing DepartmentId"); } if (login.ServerId < 1) { throw new ValidationException("Missing ServerId"); } if (string.IsNullOrWhiteSpace(login.UserName)) { throw new ValidationException("Missing UserName"); } if (string.IsNullOrWhiteSpace(login.PasswordHash)) { throw new ValidationException("Missing Password"); } var result = new ServerLogin { ServerId = login.ServerId, DepartmentId = login.DepartmentId, UserName = login.UserName, PasswordHash = login.PasswordHash }; _context.ServerLogins.Add(result); _context.SaveChanges(); return(result); }
public void Randomize(IContextProvider context, int numberOfTraders, int numberOfShareTypes) { Random bal = new Random(); Random share = new Random(); Random shareTypes = new Random(); Random quantity = new Random(); for (int i = 1; i <= numberOfTraders; i++) { var result = context.Traders.First(t => t.TraderId == i); if (result != null) { result.Balance = bal.Next(1000, 10000); } } for (int i = 1; i <= numberOfTraders; i++) { var result = context.Traders.First(t => t.TraderId == i); if (result != null) { List <int> shareTypeList = new List <int>(); for (int j = 1; j <= shareTypes.Next(1, 11); j++) { int newShareId = share.Next(1, numberOfShareTypes + 1); if (!shareTypeList.Contains(newShareId)) { //result.Portfolio.Add(new Portfolio { TraderID = i, ShareId = newShareId, Quantity = quantity.Next(1, 11) }); context.po context.Portfolios.Add(new Portfolio { TraderID = i, ShareId = newShareId, Quantity = quantity.Next(1, 11) }); shareTypeList.Add(newShareId); } } } } try { var check = context.SaveChanges(); Console.WriteLine("Saved: " + check); } catch (Exception e) { System.Console.WriteLine("Randomization failed"); System.Console.WriteLine(e.Message); } }
public void CanAddBooking() { Guid bookingId; var makeBookingRequest = new MakeBookingRequest() { StartDate = new DateTime(2015, 01, 12), EndDate = new DateTime(2015, 01, 16) }; using (_contextProvider) { var applicationUser = _userRepository.GetById(UserIds.Application); makeBookingRequest.Vehicle = _vehicleRepository.GetById(VehicleIds.SF59QRT); makeBookingRequest.Customer = Customer.Register(new RegisterCustomerRequest() { UserId = Guid.NewGuid(), EmailAddress = "*****@*****.**", //CustomerRole = FamilyName = "Green", GivenName = "Gary", Address1 = "3 Green Road", Address2 = "Greenton", Address3 = "Greenshire", PostCode = "GN1 1AA", ApplicationUser = applicationUser }); var booking = Booking.Make(makeBookingRequest); _bookingRepository.Save(booking); bookingId = booking.Id.Value; _contextProvider.SaveChanges(); } using (_contextProvider) { var allBookings = _bookingRepository.GetAll(); var storedBooking = _bookingRepository.GetById(bookingId); Assert.AreEqual(1, allBookings.Count); Assert.IsNotNull(storedBooking.Id); Assert.IsNotNull(storedBooking.BookingNumber); Assert.AreEqual(makeBookingRequest.EndDate, storedBooking.EndDate); Assert.AreEqual(makeBookingRequest.EndDate, storedBooking.EndDate); Assert.AreEqual(makeBookingRequest.Customer.User.Id.Value, storedBooking.CreatedBy.Id.Value); Assert.AreEqual("Green", storedBooking.Customer.FamilyName); Assert.AreEqual("Gary", storedBooking.Customer.GivenName); Assert.AreEqual("Ford", storedBooking.Vehicle.Make); Assert.AreEqual("Transit", storedBooking.Vehicle.Model); } }
public async Task <ActionResult <string> > Post([FromBody] TraderToAdd trader) { Trader newTrader = null; if (trader == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } newTrader = contextProvider.Traders.Add (new Trader() { FirstName = trader.FirstName, LastName = trader.LastName, Balance = trader.Balance, PhoneNumber = trader.PhoneNumber }); if (newTrader != null) { try { contextProvider.SaveChanges(); } catch (Exception) { return(BadRequest()); } return("New trader added"); } else { return(BadRequest()); } }
public async Task <ActionResult <string> > Post([FromBody] ShareToAdd share) { Share newShare = null; if (share == null || share.Price == 0) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } newShare = contextProvider.Shares.Add (new Share() { Name = share.Name, Price = share.Price }); if (newShare != null) { try { contextProvider.SaveChanges(); } catch (Exception) { return(BadRequest()); } return("Share added"); } else { return(BadRequest()); } }
public Server Create(Server server) { if (server == null) { throw new ArgumentNullException(nameof(server)); } if (server.CompanyId < 1) { throw new ValidationException("Missing companyId"); } if (string.IsNullOrEmpty(server.ServerName)) { throw new ValidationException("Server name cannot be empty."); } var existing = GetByCompany(server.CompanyId) .FirstOrDefault(d => d.ServerName == server.ServerName); if (existing != null) { throw new ValidationException("Server with same name already exists."); } var result = new Server { ServerName = server.ServerName, CompanyId = server.CompanyId }; _context.Servers.Add(result); _context.SaveChanges(); return(result); }
public Transaction AddTransaction(int buyerId, int sellerId, int shareId, decimal sharePrice, int purchaseQuantity) { var transaction = context.Transactions.Add(new Transaction { BuyerId = buyerId, SellerId = sellerId, ShareId = shareId, PricePerShare = sharePrice, Quantity = purchaseQuantity, DateTime = DateTime.Now }); context.SaveChanges(); return(transaction); }
public Department Create(Department department) { if (department == null) { throw new ArgumentNullException(nameof(department)); } if (department.CompanyId < 1) { throw new ValidationException("Missing companyId"); } if (string.IsNullOrEmpty(department.DepartmentName)) { throw new ValidationException("Department name too long."); } var existing = GetByCompany(department.CompanyId) .FirstOrDefault(d => d.DepartmentName == department.DepartmentName); if (existing != null) { throw new ValidationException("Department with same name already exists."); } var result = new Department { DepartmentName = department.DepartmentName, CompanyId = department.CompanyId }; _context.Departments.Add(result); _context.SaveChanges(); return(result); }
public void RemovePortfolio(Portfolio portfolio) { context.Portfolios.Remove(portfolio); context.SaveChanges(); }
public void Save_Clicked(object sender, RoutedEventArgs e) { var firstLineValidation = new List <string>(); if (Validation.GetHasError(Year)) { firstLineValidation.Add("Year is not valid"); } foreach (var item in ListViewPricingPeriods.Items) { var pricingPeriodTitle = " at Pricing Period at index " + ListViewPricingPeriods.Items.IndexOf(item); var listViewItem = ListViewPricingPeriods.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem; //if(Validation.GetHasError(ControlFinder.FindChild<TextBox>(listViewItem, "StartDay"))) firstLineValidation.Add("Incorrect Start Day" + pricingPeriodTitle); //if (Validation.GetHasError(ControlFinder.FindChild<TextBox>(listViewItem, "EndDay"))) firstLineValidation.Add("Incorrect End Day" + pricingPeriodTitle); //if (Validation.GetHasError(ControlFinder.FindChild<TextBox>(listViewItem, "FridayToFridayRate"))) firstLineValidation.Add("Incorrect Friday to Friday rate" + pricingPeriodTitle); //if (Validation.GetHasError(ControlFinder.FindChild<TextBox>(listViewItem, "FridayToMondayRate"))) firstLineValidation.Add("Incorrect Friday to Monday rate" + pricingPeriodTitle); //if (Validation.GetHasError(ControlFinder.FindChild<TextBox>(listViewItem, "MondayToFridayRate"))) firstLineValidation.Add("Incorrect Monday to Friday rate" + pricingPeriodTitle); if (Validation.GetHasError(listViewItem.FindChild <TextBox>("StartDay"))) { firstLineValidation.Add("Incorrect Start Day" + pricingPeriodTitle); } if (Validation.GetHasError(listViewItem.FindChild <TextBox>("EndDay"))) { firstLineValidation.Add("Incorrect End Day" + pricingPeriodTitle); } if (Validation.GetHasError(listViewItem.FindChild <TextBox>("FridayToFridayRate"))) { firstLineValidation.Add("Incorrect Friday to Friday rate" + pricingPeriodTitle); } if (Validation.GetHasError(listViewItem.FindChild <TextBox>("FridayToMondayRate"))) { firstLineValidation.Add("Incorrect Friday to Monday rate" + pricingPeriodTitle); } if (Validation.GetHasError(listViewItem.FindChild <TextBox>("MondayToFridayRate"))) { firstLineValidation.Add("Incorrect Monday to Friday rate" + pricingPeriodTitle); } } if (firstLineValidation.Any()) { var validationDialog = new ValidationDialog(firstLineValidation); validationDialog.Owner = Window.GetWindow(this); var dialogResult = validationDialog.ShowDialog(); return; } var viewModel = DataContext as EditViewModel; var request = new EditRequest(); request.Id = viewModel.Id; request.Name = viewModel.Name; request.Description = viewModel.Description; request.Overview = viewModel.Overview; request.Details = viewModel.Details; request.DriveSide = viewModel.DriveSide; request.Berth = viewModel.Berth; request.Year = viewModel.Year; request.PricingPeriods = new List <EditPricingPeriodRequest>(); foreach (var editPricingPeriodViewModel in viewModel.PricingPeriods) { var editPricingPeriodRequest = new EditPricingPeriodRequest(); editPricingPeriodRequest.Id = editPricingPeriodViewModel.Id; editPricingPeriodRequest.StartMonth = editPricingPeriodViewModel.StartMonth; editPricingPeriodRequest.StartDay = editPricingPeriodViewModel.StartDay; editPricingPeriodRequest.EndMonth = editPricingPeriodViewModel.EndMonth; editPricingPeriodRequest.EndDay = editPricingPeriodViewModel.EndDay; editPricingPeriodRequest.FridayToFridayRate = editPricingPeriodViewModel.FridayToFridayRate; editPricingPeriodRequest.FridayToMondayRate = editPricingPeriodViewModel.FridayToMondayRate; editPricingPeriodRequest.MondayToFridayRate = editPricingPeriodViewModel.MondayToFridayRate; request.PricingPeriods.Add(editPricingPeriodRequest); } ValidationMessageCollection validationMessages; using (_contextProvider) { validationMessages = _busService.ValidateEdit(request); } if (validationMessages.Any()) { var validationDialog = new ValidationDialog(validationMessages.Select(x => x.Text)); validationDialog.Owner = Window.GetWindow(this); var dialogResult = validationDialog.ShowDialog(); return; } using (var transactionScope = new TransactionScope()) using (_contextProvider) { _busService.Edit(request); _contextProvider.SaveChanges(); transactionScope.Complete(); } MessageBox.Show(viewModel.Name + " has been updated."); }