public ActionResult AddNewEmployee(NewEmployeeViewModel model) { if (!ModelState.IsValid) { model.TrainsIds = GetTrainsIds(); return(View(model)); } var user = new ApplicationUser() { UserName = model.Login }; var r = _userManager.Create(user, model.Password); if (!r.Succeeded) { int t = 0; foreach (var e in r.Errors) { ModelState.AddModelError("auth" + t++, e); } model.TrainsIds = GetTrainsIds(); return(View(model)); } Employee employee = new Employee() { Name = model.Name, Age = model.Age, Phone = model.Phone, TrainId = model.TrainId, UserId = user.Id, Position = model.Position }; try { _context.Employees.Add(employee); _context.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("employee", e); model.TrainsIds = GetTrainsIds(); return(View(model)); } return(RedirectToAction("Index")); }
public TableAdjustment EditTrip(LatticeData <Trip, TripRow> latticeData, TripRow tripRow) { Trip trip; if (tripRow.TripId == 0) { trip = new Trip(); _context.Trips.Add(trip); } else { trip = _context.Trips .FirstOrDefault(x => x.TripId == tripRow.TripId); } //на всякий случай(по идее, если сделано через форму то никогда не сработает) if (!_context.Routes.Any(x => x.RouteId == tripRow.RouteId)) { return(latticeData.Adjust(x => x .Message(LatticeMessage.User("failure", "Editing", $"There is no route with id {tripRow.RouteId}")) )); } if (!_context.Trains.Any(x => x.TrainId == tripRow.TrainId)) { return(latticeData.Adjust(x => x .Message(LatticeMessage.User("failure", "Editing", $"There is no train with id {tripRow.TrainId}")) )); } trip.RouteId = tripRow.RouteId; trip.TrainId = tripRow.TrainId; _context.SaveChanges(); var mpd = latticeData.Configuration.MapRange(latticeData.Source.Where(t => t.TripId == trip.TripId)); return(latticeData.Adjust(x => x .Update(mpd) .Message(LatticeMessage.User("success", "Editing", "Trip saved")) )); }
public TableAdjustment EditRoutePoint(LatticeData <RoutePoint, RoutePoint> latticeData, RoutePoint routePointRow) { RoutePoint routePoint = _context.RoutePoints .FirstOrDefault(x => x.RouteId == routePointRow.RouteId && x.StationOrder == routePointRow.StationOrder); if (routePoint == null) { routePoint = new RoutePoint { RouteId = routePointRow.RouteId, StationOrder = routePointRow.StationOrder }; _context.RoutePoints.Add(routePoint); } else { routePoint = _context.RoutePoints .FirstOrDefault(x => x.RouteId == routePointRow.RouteId && x.StationOrder == routePointRow.StationOrder); } try { routePoint.StationId = routePointRow.StationId; _context.SaveChanges(); } catch (Exception e) { return(latticeData.Adjust(x => x .Message(LatticeMessage.User("failure", "Editing", $"Save exception: {e.Message}")) )); } return(latticeData.Adjust(x => x .UpdateRow(routePointRow) .Message(LatticeMessage.User("success", "Editing", "RoutePoint saved")) )); }
public TableAdjustment EditRoute(LatticeData <Route, RouteRow> latticeData, RouteRow routeRow) { Route route; if (routeRow.RouteId == 0) { route = new Route(); _context.Routes.Add(route); } else { route = _context.Routes .FirstOrDefault(x => x.RouteId == routeRow.RouteId); } route.RouteName = routeRow.RouteName; _context.SaveChanges(); routeRow.RouteId = route.RouteId; return(latticeData.Adjust(x => x .Update(routeRow) .Message(LatticeMessage.User("success", "Editing", "Route saved")) )); }
public ActionResult Buy(int tripId, int depOrder, int arrOrder, SeatsType seatsType) { using (var transaction = _context.Database.BeginTransaction()) { try { Trip trip = _context.Trips.First(t => t.TripId == tripId); int occupiedSeats = trip .Tickets.Count(ticket => ticket.SeatsType == seatsType && ((ticket.StartStationOrder >= depOrder && ticket.StartStationOrder < arrOrder) || (ticket.EndStationOrder > depOrder && ticket.EndStationOrder <= arrOrder))); int thisTypeTrainCapacity; switch (seatsType) { //еще наверно собрать из имени enum + Capacity, //но где гарантия что при добавлении полей в enum их назовут так же case SeatsType.FirstClass: thisTypeTrainCapacity = trip.Train.FirstClassCapacity; break; case SeatsType.SecondClass: thisTypeTrainCapacity = trip.Train.SecondClassCapacity; break; default: throw new ArgumentOutOfRangeException(nameof(seatsType), seatsType, null); } if (thisTypeTrainCapacity > occupiedSeats) { Ticket ticket = new Ticket() { TripId = tripId, StartStationOrder = depOrder, EndStationOrder = arrOrder, UserId = User.Identity.GetUserId(), SeatsType = seatsType }; _context.Tickets.Add(ticket); _context.SaveChanges(); transaction.Commit(); // чет не понял как проверить результат транзакции ViewBag.Success = true; ViewBag.TicketId = ticket.TicketId; } else { transaction.Rollback(); ViewBag.Success = false; } } catch (Exception ex) { transaction.Rollback(); ViewBag.Success = false; } } return(View()); }