Пример #1
0
 public ActionResult ActivateRoute(int id)
 {
     DataHandler.ActivateRoute(id);
     return(RedirectToAction("Routes"));
 }
Пример #2
0
        public ActionResult CrearRuta(CreateRouteModel model)
        {
            bool valid = !string.IsNullOrWhiteSpace(model.routename);

            valid &= !string.IsNullOrWhiteSpace(model.origin);
            valid &= !string.IsNullOrWhiteSpace(model.destination);
            valid &= model.type > 0;
            valid &= !string.IsNullOrWhiteSpace(model.mapdata1);
            valid &= !string.IsNullOrWhiteSpace(model.email);

            if (valid)
            {
                bool routeWasActiveBefore = false;

                // before saving deactivate this route and delete routepieces
                if (model.id.HasValue && Request.IsAuthenticated)
                {
                    DataHandler.DeleteRoutePiecesAndDeactivate(model.id.Value);
                }

                using (DataContext context = new DataContext())
                {
                    Route route;
                    if (model.id.HasValue && Request.IsAuthenticated)
                    {
                        route = context.Routes.FirstOrDefault(x => x.ID == model.id.Value);
                    }
                    else
                    {
                        route = new Route();
                        route.ParentRouteID = model.id;
                        context.Routes.InsertOnSubmit(route);
                    }
                    route.FromName = model.origin;
                    route.ToName   = model.destination;
                    route.Name     = model.routename;

                    if (route.TypeID == (int)TypeEnum.Microbus)
                    {
                        route.SeoName = Utils.FormatSEO(route.FromName + " a " + route.ToName);
                    }
                    else
                    {
                        route.SeoName = Utils.FormatSEO(route.Name);
                    }

                    route.TypeID      = model.type;
                    route.Email       = model.email;
                    route.Description = model.description;
                    route.Status      = (int)StatusEnum.New;
                    context.SubmitChanges();

                    model.id = route.ID;

                    List <RoutePiece> pieces = new List <RoutePiece>();
                    foreach (var coords in model.Positions1)
                    {
                        var latLng     = coords.Split(',');
                        var name       = model.GetMapNameAtPosition(coords);
                        var routePiece = new RoutePiece {
                            Lat = double.Parse(latLng[0]), Lng = double.Parse(latLng[1]), RouteID = route.ID, Name = name, SeoName = Utils.FormatSEO(name)
                        };
                        pieces.Add(routePiece);
                    }
                    context.RoutePieces.InsertAllOnSubmit(pieces);
                    context.SubmitChanges();

                    if (!string.IsNullOrWhiteSpace(model.mapdata2))
                    {
                        List <RoutePiece> pieces2 = new List <RoutePiece>();
                        foreach (var coords in model.Positions2)
                        {
                            var latLng     = coords.Split(',');
                            var name       = model.GetMapNameAtPosition(coords);
                            var routePiece = new RoutePiece {
                                Lat = double.Parse(latLng[0]), Lng = double.Parse(latLng[1]), RouteID = route.ID, Name = name, SeoName = Utils.FormatSEO(name)
                            };
                            pieces2.Add(routePiece);
                        }
                        context.RoutePieces.InsertAllOnSubmit(pieces2);
                        context.SubmitChanges();

                        route.SplitRoutePieceID = pieces2.First().ID;
                        context.SubmitChanges();
                    }
                }

                if (Request.IsAuthenticated)
                {
                    if (routeWasActiveBefore)
                    {
                        DataHandler.ActivateRoute(model.id.Value);
                    }
                    return(RedirectToAction("Routes"));
                }
                else
                {
                    return(RedirectToAction("RutaRegistrada", "Home"));
                }
            }

            model.Valid    = valid;
            model.TypeList = DataHandler.GetEditableTypes();
            return(View(model));
        }