/// <inheritdoc /> /// <summary> /// Updates an existing Route /// </summary> /// <param name="id">The id of the route to update</param> /// <param name="routeToUpdate">The route fields to update</param> /// <param name="ct">Cancellation token to stop the update of the route</param> /// <returns>Returns the updated route</returns> public async Task<Route> UpdateRouteByIdAsync(Guid id, RouteUpdate routeToUpdate, CancellationToken ct) { var route = await RoutesQuery.SingleOrDefaultAsync(r => r.Id == id, ct); if (route == null) throw new ArgumentException(Resources.InvalidRouteId); if (!string.IsNullOrEmpty(routeToUpdate?.RouteName)) { route.Name = routeToUpdate.RouteName; } if (routeToUpdate?.Origin != Guid.Empty) { route.Origin = _context.Points.FirstOrDefault(point => point.Id == routeToUpdate.Origin); } if (routeToUpdate?.Destination != Guid.Empty) { route.Destination = _context.Points.FirstOrDefault(point => point.Id == routeToUpdate.Destination); } if (routeToUpdate != null) { route.Cost = routeToUpdate.Cost; route.Time = routeToUpdate.Time; } _context.Routes.Update(route); await _context.SaveChangesAsync(ct); return Mapper.Map<Route>(route); }
private RouteUpdate UpdateRoute(ApiCall call) { string url = call.GetValue("value", "url"); url = url.Replace("\\", "/"); if (!url.StartsWith("/")) { url = "/" + url; } RouteUpdate result = new RouteUpdate(); var sitedb = call.WebSite.SiteDb(); var route = sitedb.Routes.Get(call.ObjectId); if (route != null) { result.OldId = route.Id; var referredby = sitedb.Relations.GetReferredByRelations(call.ObjectId); sitedb.Routes.ChangeRoute(route.Name, url); foreach (var by in referredby) { var repofrom = sitedb.GetRepository(by.ConstTypeX); if (repofrom != null) { Sites.Helper.ChangeHelper.ChangeUrl(sitedb, repofrom, by.objectXId, route.Name, url); } } } result.NewId = Kooboo.Data.IDGenerator.GetRouteId(url); var newroute = sitedb.Routes.Get(result.NewId); result.PreviewUrl = sitedb.WebSite.BaseUrl(newroute.Name); return(result); }
private RouteUpdate UpdateExternal(ApiCall call) { RouteUpdate result = new RouteUpdate(); string value = call.GetValue("value", "url"); var sitedb = call.WebSite.SiteDb(); var external = sitedb.ExternalResource.Get(call.ObjectId); if (external != null) { result.OldId = external.Id; var referredby = sitedb.Relations.GetReferredByRelations(call.ObjectId); foreach (var by in referredby) { var repofrom = sitedb.GetRepository(by.ConstTypeX); if (repofrom != null) { Sites.Helper.ChangeHelper.ChangeUrl(sitedb, repofrom, by.objectXId, external.FullUrl, value); } } } result.NewId = Data.IDGenerator.Generate(value, ConstObjectType.ExternalResource); result.PreviewUrl = value; return(result); }
public async Task <IActionResult> UpdateRouteByIdAsync(Guid routeId, RouteUpdate routeToUpdate, CancellationToken ct) { if (!_accountsService.CanExecuteAdminRequest(User)) { return(Unauthorized()); } if (routeId == Guid.Empty) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.IdFieldRequired))); } if (string.IsNullOrEmpty(routeToUpdate?.RouteName)) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.NameFieldRequired))); } if (routeToUpdate.Origin == Guid.Empty) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidOriginPointId))); } if (routeToUpdate.Destination == Guid.Empty) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidDestinationPointId))); } if (routeToUpdate.Cost <= 0) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidCostMustBeGreaterThanZero))); } if (routeToUpdate.Time <= 0) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidTimeMustBeGreaterThanZero))); } if (!ModelState.IsValid) { return(BadRequest(new ApiError(ModelState))); } var route = await _routesService.GetRouteByIdAsync(routeId, ct); if (route == null) { return(NotFound()); } route = await _routesService.UpdateRouteByIdAsync(routeId, routeToUpdate, ct); return(Ok(route)); }