Exemplo n.º 1
0
        public void isRouteNameInvalid_InvalidRouteName_ErrorReturnedRouteNotCreated(string routeName)
        {
            DateTime regionNextDate = dateCalculation.GetOneDate(rOne);

            Route routeOne = new Route()
            {
                routeName = routeName,
                regionID  = rOne.regionID,
                region    = rOne,
                completed = false,
                routeDate = regionNextDate
            };

            List <ValidationResult> results = ClassValidator <Route> .Validate(routeOne);

            Assert.Single(results);

            switch (routeOne.routeName.Length)
            {
            case 0:
                Assert.Equal("Route Name cannot be empty", results[0].ToString());
                break;

            case int n when n > 0 && n < 2:
                Assert.Equal("Route Name must be at least 2 characters", results[0].ToString());
                break;

            case int n when n > 40:
                Assert.Equal("Route Name cannot be greater than 40 characters", results[0].ToString());
                break;
            }
        }
Exemplo n.º 2
0
        public async void IsRouteCreatedAfterRouteCompletion_RouteSetToComplete_RouteIsCreated()
        {
            _webApp.Load(_webApp.DBContext);

            Route updateRoute = _webApp.DBContext.Route.FirstOrDefault(r => !r.completed);   //Getting the first Incomplete Route

            Assert.NotNull(updateRoute);
            //Region region = _webApp.DBContext.Region.Where(reg => reg.regionID == updateRoute.regionID).First();    //Get the Region the previous Route corresponds to
            Region region = updateRoute.region;

            updateRoute.completed = true;       //Set the Route to Completed

            //convert the Route to a string for posting to the database
            var StringContent = new StringContent(JsonConvert.SerializeObject(updateRoute), Encoding.UTF8, "application/json");

            //send a PUT request to API to update the Route to completed
            HttpResponseMessage response = await _client.PutAsync("api/Routes/" + updateRoute.routeID, StringContent);

            List <Route> newRoute = _webApp.DBContext.Route.Where(r => r.regionID == region.regionID && !r.completed).ToList();

            DateCalculation dateCalculation = new DateCalculation();

            Assert.Single(newRoute);
            Assert.Equal(updateRoute.routeName, newRoute[0].routeName);
            Assert.Equal(updateRoute.regionID, newRoute[0].regionID);
            Assert.False(newRoute[0].completed);
            Assert.False(newRoute[0].inactive);
            Assert.Equal(dateCalculation.GetOneDate(region), newRoute[0].routeDate);
        }
        public async Task <IActionResult> DeleteRoute([FromRoute] int id)
        {
            //If we failed the Authorize claims check, the response code will be set to 401
            if (this.Response.StatusCode == 401)
            {
                return(Unauthorized());
            }

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

            Route result = (Route)await _context.Route.FindAsync(id);

            if (result == null)
            {
                return(BadRequest("Archiving Failed - Route ID was not found"));
            }

            if (!result.inactive)
            {
                result.inactive = true;
                _context.Route.Update(result);
                await _context.SaveChangesAsync();
            }

            if (_context.Route.AsNoTracking().Where(r => r.routeID == id).Single().inactive)
            {
                //Get Region associated with archived Route
                Region region = _context.Region.AsNoTracking().Where(re => re.regionID == result.regionID).Single();

                //If the Region is ACTIVE and there are no INCOMPLETE Routes associated with the Region
                if (!region.inactive && _context.Route.AsNoTracking().Where(r => r.regionID == region.regionID && !r.inactive && !r.completed).Count() == 0)
                {
                    DateCalculation calc     = new DateCalculation();
                    Route           newRoute = new Route
                    {
                        //routeID = default(int),
                        routeName = result.routeName,
                        regionID  = region.regionID,
                        completed = false,
                        inactive  = false,
                        routeDate = calc.GetOneDate(region)
                    };
                    await _context.Route.AddAsync(newRoute);
                }

                await _context.SaveChangesAsync();

                return(Ok("Route " + result.routeName + " was archived"));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemplo n.º 4
0
        public async void IsRouteCreatedForRegion_RegionIsCreated_RouteIsCreated()
        {
            _webApp.Load(_webApp.DBContext);

            DateTime today = DateTime.Now;  //Get Today's date

            //New Region object to add to DB
            Region newRegion = new Region
            {
                regionName = "Candyland",
                frequency  = 10,
                firstDate  = today.AddDays(1.0),
                inactive   = false
            };

            //convert the Region to a string for posting to the database
            var StringContent = new StringContent(JsonConvert.SerializeObject(newRegion), Encoding.UTF8, "application/json");

            //send a post request to the api with the Subscriber object in JSON form
            HttpResponseMessage response = await _client.PostAsync("api/Regions", StringContent);

            //get the response from the api and read it into a string
            string responseString = await response.Content.ReadAsStringAsync();

            //deserialize the response into a Region object
            Region postRegion = JsonConvert.DeserializeObject <Region>(responseString);

            //created status code is 201
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);


            int    regionID          = postRegion.regionID;                                              //Get returned region's ID
            string dateString        = dateCalculation.GetOneDate(postRegion).ToString("MMMM dd, yyyy"); //Get returned region's next pickup date in format MMMM dd, yyyy
            string expectedRouteName = postRegion.regionName + " - " + dateString;                       //Concatenate region name and date string togethe "RegionName - NextPickupDate"

            Route newRoute = _webApp.DBContext.Route.Where(r => r.regionID == regionID && r.completed == false).Single();

            Assert.Equal(expectedRouteName, newRoute.routeName);    //Check Route Name is as expected "RegionName - NextPickupDate"
            Assert.Equal(regionID, newRoute.regionID);              //Check Route's Region ID matches the added Region's
            Assert.False(newRoute.completed);                       //Check Route's completed status is false
            Assert.False(newRoute.inactive);                        //Check Route's inactive status is false
        }
        public IActionResult GetSubscriberDates([FromRoute] int regionID)
        {
            //If we failed the Authorize claims check, the response code will be set to 401
            if (this.Response.StatusCode == 401)
            {
                return(Unauthorized());
            }

            //Declare The Util method so we can call it later
            DateCalculation dc = new DateCalculation();

            //Get all the Regions From the DB
            Region region = _context.Region.FirstOrDefault(r => r.regionID == regionID);



            //Return the results of the Helper method along with OK as an indication of success
            return(Ok(dc.GetOneDate(region)));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Helper Method for loading Routes into context
        /// </summary>
        /// <param name="context"></param>
        private List <Route> loadRoutes(List <Region> regions)
        {
            //DateTime date = DateTime.Today; //Gets todays date. Later used to ensure dates are in the future

            //List<Region> regions = context.Region.AsNoTracking().ToList<Region>(); //Gets Regions from context. Needed to assign required regionID and region properties

            List <Route> routes = new List <Route>()  //Creating route objects. Dates are generated based on region's first pickup date. Set it up to generate the next three upcoming sets of route dates
            {
                //Week 1 Routes
                new Route()
                {
                    routeName = "S90 A Past Route", regionID = regions[8].regionID, region = regions[8], completed = false, routeDate = regions[8].firstDate, optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Logan's Loop", regionID = regions[0].regionID, region = regions[0], completed = false, routeDate = regions[8].firstDate.AddDays(1.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Nathan's Circuit", regionID = regions[1].regionID, region = regions[1], completed = false, routeDate = regions[8].firstDate.AddDays(2.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Original Name 3", regionID = regions[2].regionID, region = regions[2], completed = false, routeDate = regions[8].firstDate.AddDays(3.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "S90 Active Route", regionID = regions[6].regionID, region = regions[6], completed = false, routeDate = regions[8].firstDate.AddDays(4.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "S90 Inactive Route One", regionID = regions[7].regionID, region = regions[7], completed = false, routeDate = regions[8].firstDate.AddDays(5.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "S90 Inactive Route Two", regionID = regions[9].regionID, region = regions[9], completed = false, routeDate = regions[8].firstDate.AddDays(5.0), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Way to the New World", regionID = regions[10].regionID, region = regions[10], completed = false, routeDate = regions[8].firstDate, optoutLocationRouteList = new List <LocationRoute>()
                },
                //Week 2 Routes
                new Route()
                {
                    routeName = "Logan's Loop", regionID = regions[0].regionID, region = regions[0], completed = true, routeDate = dateCalculation.GetOneDate(regions[0]).AddDays(7.0 * regions[0].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Nathan's Circuit", regionID = regions[1].regionID, region = regions[1], completed = true, routeDate = dateCalculation.GetOneDate(regions[1]).AddDays(7.0 * regions[1].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Original Name 3", regionID = regions[2].regionID, region = regions[2], completed = true, routeDate = dateCalculation.GetOneDate(regions[2]).AddDays(7.0 * regions[2].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },

                //Week 3 Routes
                new Route()
                {
                    routeName = "Logan's Loop", regionID = regions[0].regionID, region = regions[0], completed = true, routeDate = dateCalculation.GetOneDate(regions[0]).AddDays(7.0 * 2.0 * regions[0].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Nathan's Circuit", regionID = regions[1].regionID, region = regions[1], completed = true, routeDate = dateCalculation.GetOneDate(regions[1]).AddDays(7.0 * 2.0 * regions[1].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Original Name 3", regionID = regions[2].regionID, region = regions[2], completed = true, routeDate = dateCalculation.GetOneDate(regions[2]).AddDays(7.0 * 2.0 * regions[2].frequency), optoutLocationRouteList = new List <LocationRoute>()
                },


                //Story 87 routes
                new Route()
                {
                    routeName = "Route Z", regionID = regions[3].regionID, region = regions[3], completed = true, routeDate = new DateTime(2020, 2, 20), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Route Z", regionID = regions[3].regionID, region = regions[3], completed = true, routeDate = new DateTime(2020, 2, 3), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Route Z", regionID = regions[3].regionID, region = regions[3], completed = true, routeDate = new DateTime(2020, 1, 25), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Route Z", regionID = regions[3].regionID, region = regions[3], completed = true, routeDate = new DateTime(2020, 1, 15), optoutLocationRouteList = new List <LocationRoute>()
                },
                new Route()
                {
                    routeName = "Route Z", regionID = regions[3].regionID, region = regions[3], completed = true, routeDate = new DateTime(2020, 1, 5), optoutLocationRouteList = new List <LocationRoute>()
                }
            };

            return(routes);
        }
        public async Task <IActionResult> PutRoute([FromRoute] int id, [FromBody] Route route)
        {
            //If we failed the Authorize claims check, the response code will be set to 401
            if (this.Response.StatusCode == 401)
            {
                return(Unauthorized());
            }

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

            if (id != route.routeID)
            {
                return(BadRequest());
            }

            //Get the date of the corresponding Route in the DB. Using "AsNoTracking" to prevent the entity from being tracked as information is being read, not edited
            DateTime dbRouteDate = _context.Route.AsNoTracking().Where(r => r.routeID == id).Single().routeDate;


            //If the incoming Route's date is in the past, and the date has been changed, return an error
            if (route.routeDate.Date < DateTime.Now.Date && !route.routeDate.ToShortDateString().Equals(dbRouteDate.ToShortDateString()))
            {
                //Return a Bad Request is the incoming Route's date was chnaged to a date in the past
                return(BadRequest(new ValidationResult("Invalid Date", new[] { "routeDate" })));
            }
            else
            {
                _context.Route.Update(route);
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RouteExists(id))
                {
                    return(NotFound("Route was not found"));
                }
                else
                {
                    throw;
                }
            }

            CompareModels cmpRoutes = new CompareModels();
            //Route dbRoute = (Route) await _context.Route.FindAsync(id);
            Route dbRoute = _context.Route.AsNoTracking().Where(r => r.routeID == id).Single();

            dbRoute.region = _context.Region.AsNoTracking().Single(reg => reg.regionID == dbRoute.regionID);

            //Route dbRoute = (Route)await _context.Route.FindAsync(id);

            if (cmpRoutes.CompareRoutes(dbRoute, route) == 0)

            {
                //If original route was incomplete and updated Route is complete,
                //no incomplete Routes exist for the Region
                //and the Region is Active, Create a new Route
                if (dbRoute.completed &&
                    _context.Route.AsNoTracking().Where(r => r.regionID == route.regionID && !r.inactive && !r.completed).Count() == 0 &&
                    _context.Region.AsNoTracking().Where(r => r.regionID == route.regionID).FirstOrDefault().inactive == false)
                {
                    Region          dbRegion = _context.Region.AsNoTracking().Where(r => r.regionID == route.regionID).FirstOrDefault(); //Get the associated Region for the Route
                    DateCalculation calc     = new DateCalculation();
                    //Create a new Route object with values taken from old Route

                    Route newRoute = new Route
                    {
                        routeName = route.routeName,
                        regionID  = dbRegion.regionID,
                        region    = dbRegion,
                        completed = false,
                        inactive  = false,
                        routeDate = calc.GetOneDate(dbRegion)
                    };

                    //Add the new Route to DB and save it
                    await _context.Route.AddAsync(newRoute);
                }
                await _context.SaveChangesAsync();

                //Return resulting updated Route
                return(Ok(dbRoute));
            }
            else
            {
                return(BadRequest());
            }
        }