Ejemplo n.º 1
0
        public bool CreateRoute(RouteCreate model)
        {
            var route =
                new Route()
            {
                UserId     = _userId,
                RouteName  = model.RouteName,
                RouteGrade = model.RouteGrade,
                RouteNote  = model.RouteNote,
                DateSent   = DateTime.Now.Date
            };

            using (var db = new DbContext())
            {
                db.Routes.Add(route);
                return(db.SaveChanges() == 1);
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(RouteCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateRouteService();

            if (service.CreateRoute(model))
            {
                TempData["CreateResult"] = "Route added successfully.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Route could not be added.");
            return(View(model));
        }
Ejemplo n.º 3
0
		/// <inheritdoc />
		/// <summary>
		///     Inserts a new Route
		/// </summary>
		/// <param name="newRouteToCreate">The fields of the new route to create</param>
		/// <param name="ct">Cancellation token to stop the insertion of the route</param>
		/// <returns>Returns the Id of the created route</returns>
		public async Task<Guid> CreateRouteAsync(RouteCreate newRouteToCreate, CancellationToken ct)
		{
			if (newRouteToCreate == null) return Guid.Empty;

			var id = Guid.NewGuid();
			var newRoute = _context.Routes.Add(new RouteEntity()
			{
				Id = id,
				Name = newRouteToCreate?.RouteName,
				Origin = _context.Points.FirstOrDefault(point => point.Id == newRouteToCreate.Origin),
				Destination = _context.Points.FirstOrDefault(point => point.Id == newRouteToCreate.Destination),
				Cost = newRouteToCreate.Cost,
				Time = newRouteToCreate.Time
			});

			var created = await _context.SaveChangesAsync(ct);

			if (created < 1) throw new InvalidOperationException(Resources.CouldNotCreateRoute);

			return id;
		}
        public async Task <IActionResult> CreateNewRouteAsync([FromBody] RouteCreate newRoute, CancellationToken ct)
        {
            if (!_accountsService.CanExecuteAdminRequest(User))
            {
                return(Unauthorized());
            }

            if (string.IsNullOrEmpty(newRoute?.RouteName))
            {
                return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.NameFieldRequired)));
            }
            if (newRoute.Origin == Guid.Empty)
            {
                return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidOriginPointId)));
            }
            if (newRoute.Destination == Guid.Empty)
            {
                return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidDestinationPointId)));
            }
            if (newRoute.Cost <= 0)
            {
                return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidCostMustBeGreaterThanZero)));
            }
            if (newRoute.Time <= 0)
            {
                return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidTimeMustBeGreaterThanZero)));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ApiError(ModelState)));
            }

            var routeId = await _routesService.CreateRouteAsync(newRoute, ct);

            return(Created(Url.Link(nameof(GetRouteByIdAsync), new { routeId }), null));
        }