public ActionResult AddConnection(string stringConnection) { Connection connection = JsonConvert.DeserializeObject<Connection>(stringConnection); addConnectionModel.EndInitializationConnection(connection); if(Session["connections"] is List<Connection>) { List<Connection> connections = Session["connections"] as List<Connection>; connections.Add(connection); Session["connections"] = connections; return RedirectToAction("ListConnections", new { page = 1 }); } else { using (IEasyTrainTicketsDbEntities dbContext = unitOfWorkFactory.CreateUnitOfWork()) { dbContext.Trains.Attach(connection.Train); foreach (var part in connection.Parts) { dbContext.Routes.Attach(part.Route); } dbContext.Connections.Add(connection); dbContext.SaveChanges(); } return View("MySuccess", new ErrorViewModel() { ErrorMessage = "Udało się dodać wybrane połączenie" }); } }
public bool DeleteTicket(TicketDTO ticketDTO, IEasyTrainTicketsDbEntities dbContext) { var list = dbContext.Users.Where(u => u.Id == ticketDTO.User.Id && u.Password == ticketDTO.User.Password).ToList(); if (list.Count == 0) { return(false); } User user = list.First(); Ticket ticket = AutoMapper.Mapper.Map <TicketDTO, Ticket>(ticketDTO); string ticketString = CreateStringTicket(ticket); string userTickets = user.Tickets; int index = userTickets.IndexOf(ticketString); string newTickets; if (index < 0) { return(false); } else if (ticketString.Length == userTickets.Length) { newTickets = ""; } else if (index == userTickets.Length - ticketString.Length) { newTickets = userTickets.Remove(index - 1, ticketString.Length + 1); } else { newTickets = userTickets.Remove(index, ticketString.Length + 1); } user.Tickets = newTickets; for (int i = 0; i < ticket.Count; i++) { var tmp = ticket.ConnectionPath[i]; ticket.ConnectionPath[i] = dbContext.ConnectionParts.Where(cp => cp.Id == tmp.Id).First(); if (ticket.ConnectionPath[i].Seats == "") { ticket.ConnectionPath[i].Seats = string.Join(",", ticket.Seats[i]).Replace(',', ';'); } else { ticket.ConnectionPath[i].Seats = String.Format("{0};{1}", ticket.ConnectionPath[i].Seats, string.Join(",", ticket.Seats[i]).Replace(',', ';')); } ticket.ConnectionPath[i].FreeSeats += ticket.Seats[i].Length; } dbContext.SaveChanges(); return(true); }
public UserDTO Registration(UserDTO userDTO, IEasyTrainTicketsDbEntities dbcontext) { var record = dbcontext.Users.Where((u) => u.Login == userDTO.Login); User user = Mapper.Map <UserDTO, User>(userDTO); if (record.Count() == 1) { return(null); } dbcontext.Users.Add(user); dbcontext.SaveChanges(); userDTO = Mapper.Map <User, UserDTO>(user); return(userDTO); }
public bool DeleteTicket(Ticket ticket, IEasyTrainTicketsDbEntities dbContext, ApplicationUserManager userManager) { ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); string ticketString = ticket.StringTicket; string userTickets = user.Tickets; int index = userTickets.IndexOf(ticketString); string newTickets; if (index < 0) { return(false); } else if (ticketString.Length == userTickets.Length) { newTickets = ""; } else if (index == userTickets.Length - ticketString.Length) { newTickets = userTickets.Remove(index - 1, ticketString.Length + 1); } else { newTickets = userTickets.Remove(index, ticketString.Length + 1); } user.Tickets = newTickets; for (int i = 0; i < ticket.conPartsId.Count; i++) { int id = ticket.conPartsId[i]; ConnectionPart conPart = dbContext.ConnectionParts.Where(cp => cp.Id == id).First(); if (conPart.Seats == "") { conPart.Seats = string.Join(",", ticket.Seats[i]).Replace(',', ';'); } else { conPart.Seats = String.Format("{0};{1}", conPart.Seats, string.Join(",", ticket.Seats[i]).Replace(',', ';')); } conPart.FreeSeats += ticket.Seats[i].Length; } dbContext.SaveChanges(); userManager.Update(user); return(true); }
public ActionResult AddConnections(DateViewModel viewModel) { using(IEasyTrainTicketsDbEntities dbContext = unitOfWorkFactory.CreateUnitOfWork()) { List<Connection> connections = Session["connections"] as List<Connection>; foreach (var con in connections) { con.Train = dbContext.Trains.Where(c => c.Id == con.Train.Id).First(); dbContext.Trains.Attach(con.Train); foreach (var part in con.Parts) { part.Route = dbContext.Routes.Where(r => r.Id == part.Route.Id).First(); dbContext.Routes.Attach(part.Route); } dbContext.Connections.Add(con); } dbContext.SaveChanges(); } Session["connections"] = null; return View("MySuccess", new ErrorViewModel() { ErrorMessage = "Udało się dodać rozkład jazdy na wybrany dzień" }); }