/// <summary>
        /// Update an existing route package record using the specified <see cref="RoutePackage"/> instance.
        /// </summary>
        /// <param name="package">Instance of <see cref="RoutePackage"/> used to update database record.</param>
        /// <returns>The updated <see cref="RoutePackage"/> record.</returns>
        public async Task <RoutePackage> UpdateRoutePackageAsync(RoutePackage package)
        {
            using (var db = new SubrouteContext())
            {
                // Try to get the user ID for auditing purposes.
                var userId = Thread.CurrentPrincipal.GetUserId();

                var existingPackage = await db.RoutePackages.FirstOrDefaultAsync(rs => rs.Id == package.Id);

                if (existingPackage == null)
                {
                    throw new NotFoundException($"No route package exists with ID '{package.Id}'.");
                }

                existingPackage.Version       = package.Version;
                existingPackage.UserSpecified = package.UserSpecified;
                existingPackage.UpdatedOn     = DateTimeOffset.Now;
                existingPackage.UpdatedBy     = userId;

                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(existingPackage).State = EntityState.Detached;

                return(existingPackage);
            }
        }
        /// <summary>
        /// Update an existing route setting record using the specified <see cref="RouteSetting"/> instance.
        /// </summary>
        /// <param name="setting">Instance of <see cref="RouteSetting"/> used to update database record.</param>
        /// <returns>The updated <see cref="RouteSetting"/> that includes the generated route setting ID.</returns>
        public async Task <RouteSetting> UpdateRouteSettingAsync(RouteSetting setting)
        {
            using (var db = new SubrouteContext())
            {
                // Try to get the user ID for auditing purposes.
                var userId = Thread.CurrentPrincipal.GetUserId();

                var existingSetting = await db.RouteSettings.FirstOrDefaultAsync(rs => rs.Name == setting.Name);

                if (existingSetting == null)
                {
                    throw new NotFoundException($"No route setting exists with name '{setting.Name}'.");
                }

                existingSetting.Value     = setting.Value;
                existingSetting.UpdatedOn = DateTimeOffset.Now;
                existingSetting.UpdatedBy = userId;

                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(existingSetting).State = EntityState.Detached;

                return(existingSetting);
            }
        }
        public async Task <Request> UpdateRequestAsync(Request request)
        {
            using (var db = new SubrouteContext())
            {
                // Do not allow adding a new entry. Entry must already exist.
                if (request.Id == 0)
                {
                    throw new NotFoundException($"Request cannot be updated because it hasn't yet been created.");
                }

                // Calculate and store the response length if a response payload is available.
                if (request.ResponsePayload != null)
                {
                    request.ResponseLength = request.ResponsePayload.LongLength;
                }

                db.Requests.Attach(request);
                db.Entry(request).State = EntityState.Modified;

                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(request).State = EntityState.Detached;

                return(request);
            }
        }
Пример #4
0
        public async Task <Route> CreateRouteAsync(Route route, string userId = null)
        {
            using (var db = new SubrouteContext())
            {
                // Ensure we are only creating a route for specified user.
                if (route.UserId != userId)
                {
                    throw new NotFoundException("Route does not belong to specified user, and cannot be created.");
                }

                // Set auto property values.
                route.CreatedOn = DateTimeOffset.Now;
                route.CreatedBy = "SYSTEM"; // Todo: Use Authenticated Username.
                route.UpdatedOn = route.CreatedOn;
                route.UpdatedBy = "SYSTEM"; // Todo: Use Authenticated Username.

                db.Routes.Add(route);
                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(route).State = EntityState.Detached;

                return(route);
            }
        }
Пример #5
0
        public async Task <Route> UpdateRouteAsync(Route route, string userId = null)
        {
            using (var db = new SubrouteContext())
            {
                // Do not allow adding a new entry. Entry must already exist.
                if (route.Id == 0)
                {
                    throw new NotFoundException("Route cannot be updated because it hasn't yet been created.");
                }

                // Ensure route belongs to user.
                if (userId != null && route.UserId != userId)
                {
                    throw new NotFoundException("Route does not belong to specified user, and cannot be modified.");
                }

                db.Routes.Attach(route);
                db.Entry(route).State = EntityState.Modified;

                route.UpdatedOn = DateTimeOffset.Now;
                route.UpdatedBy = Thread.CurrentPrincipal.GetUserId();

                await db.SaveChangesAsync();

                return(route);
            }
        }
        /// <summary>
        /// Delete the route setting record using the passed instance.
        /// </summary>
        /// <param name="setting">Instance of <see cref="RouteSetting"/> that represents the record to delete.</param>
        /// <returns>Returns a Task, essentially void when using async syntax.</returns>
        public async Task DeleteRouteSettingAsync(RouteSetting setting)
        {
            using (var db = new SubrouteContext())
            {
                // Mark route for deletion so the context knowns to delete it.
                db.Entry(setting).State = EntityState.Deleted;

                await db.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Delete the route setting record with the specified identifier.
        /// </summary>
        /// <param name="identifier"><see cref="RouteIdentifier"/> for the requested route.</param>
        /// <param name="name">String that identifies the route setting record to delete.</param>
        /// <returns>Returns a Task, essentially void when using async syntax.</returns>
        public async Task DeleteRouteSettingAsync(RouteIdentifier identifier, string name)
        {
            using (var db = new SubrouteContext())
            {
                var route = await GetRouteSettingByNameAsync(identifier, name);

                // Mark route for deletion so the context knowns to delete it.
                db.Entry(route).State = EntityState.Deleted;

                await db.SaveChangesAsync();
            }
        }
Пример #8
0
        public async Task DeleteRouteByIdentifierAsync(RouteIdentifier identifier, string userId = null)
        {
            using (var db = new SubrouteContext())
            {
                var route = await GetRouteByIdentifierAsync(identifier);

                // Ensure route belongs to specified user.
                if (userId != null && route.UserId != userId)
                {
                    throw new NotFoundException("Route does not belong to specified user, and cannot be deleted.");
                }

                // Mark route for deletion so the context knowns to delete it.
                db.Entry(route).State = EntityState.Deleted;

                await db.SaveChangesAsync();
            }
        }
        public async Task <Request> CreateRequestAsync(Request request)
        {
            using (var db = new SubrouteContext())
            {
                request.OccurredOn = DateTimeOffset.UtcNow;

                // Calculate and store the request length if a request payload is available.
                if (request.RequestPayload != null)
                {
                    request.RequestLength = request.RequestPayload.LongLength;
                }

                db.Requests.Add(request);
                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(request).State = EntityState.Detached;

                return(request);
            }
        }
        /// <summary>
        /// Uses the specified <see cref="RouteSetting"/> instance to create a new route setting record in the database.
        /// </summary>
        /// <param name="setting">Instance of <see cref="RouteSetting"/> used to create the database record.</param>
        /// <returns>Returns instance of <see cref="RouteSetting"/>.</returns>
        public async Task <RouteSetting> CreateRouteSettingAsync(RouteSetting setting)
        {
            using (var db = new SubrouteContext())
            {
                // Try to get the user ID for auditing purposes.
                var userId = Thread.CurrentPrincipal.GetUserId();

                // Set auto property values.
                setting.CreatedOn = DateTimeOffset.Now;
                setting.CreatedBy = userId;
                setting.UpdatedOn = setting.CreatedOn;
                setting.UpdatedBy = userId;

                db.RouteSettings.Add(setting);
                await db.SaveChangesAsync();

                // Detach entry so its changes won't be tracked.
                db.Entry(setting).State = EntityState.Detached;

                return(setting);
            }
        }