public void isValidAmountOfDatesToGet_ValidAmount_ListReturned(int amountOfDates) { //List of what the actual method returns List <DateTime> results = DateCalculator.GetNextDates(amountOfDates, region); //List of what we are expecting results to look like List <DateTime> expectedResults = GetExpectedResults(amountOfDates); //Check to see if they are the same Assert.Equal(expectedResults, results); }
public IActionResult GetSubscriberDates([FromRoute] int locationID) { //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 locations from the DB Location location = _context.Location.FirstOrDefault(l => l.locationID == locationID); int regID; //This is to check to see if the subscribers location is assigned to a region if (location.regionID != null) { //Set an int equal to the locations regionID regID = (int)location.regionID; } else { //Return OK here so we do not search and execute the rest of the code on a region that does not exist return(Ok()); } //Get all the Regions From the DB Region region = _context.Region.FirstOrDefault(r => r.regionID == regID); //Return the results of the Helper method along with OK as an indication of success return(Ok(dc.GetNextDates(3, region))); }
public async Task <IActionResult> PostRegion([FromBody] Region region) { //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)); } _context.Region.Add(region); await _context.SaveChangesAsync(); //Queries the Route table for Routes which have a region ID matching the created regions ID. //If no Routes are found AND added region is not inactive, create a Route with derived information if (_context.Route.Where(r => r.regionID == region.regionID).Count() == 0 && !region.inactive) { DateTime nextDate = dateCalculation.GetNextDates(1, region).FirstOrDefault(); Route newRoute = new Route { routeName = region.regionName + " - " + nextDate.ToString("MMMM dd, yyyy"), regionID = region.regionID, region = region, completed = false, inactive = false, routeDate = nextDate }; _context.Route.Add(newRoute); await _context.SaveChangesAsync(); } DateCalculation dc = new DateCalculation(); //Create an anonymous object that contains the nextPickupDate //Is used to send back the next collection date, to be displayed on the front end var returnRegion = new { regionID = region.regionID, regionName = region.regionName, frequency = region.frequency, firstDate = region.firstDate, nextDate = dc.GetNextDates(1, region), inactive = region.inactive }; return(CreatedAtAction("GetRegion", new { id = returnRegion.regionID }, returnRegion)); }
public IActionResult GetAllRegions() { //Checks if model state is valid - checks for errors coming from model binding and validation if (!ModelState.IsValid) { //Returns 400 status code with the model state as content return(BadRequest(ModelState)); } //Create and instance of the datecalculation DateCalculation dc = new DateCalculation(); //Gets a list of all Routes in the context table and appends a next collection date to the returned regions var dbRegions = _context.Region.Select(r => new { regionID = r.regionID, regionName = r.regionName, frequency = r.frequency, firstDate = r.firstDate, nextDate = dc.GetNextDates(1, r), inactive = r.inactive }).ToList(); //If region list is null or contains no regions, return 404 Code with "No regions Found" message if (dbRegions == null || dbRegions.Count <= 0) { return(NotFound("No regions found")); } else { //Return 200 Code with Route List as content return(Ok(dbRegions)); } }
public async Task <IActionResult> PutRegion([FromRoute] int id, [FromBody] Region region) { //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 != region.regionID) { return(BadRequest()); } DateCalculation dc = new DateCalculation(); //get the oldRegion to check if the active changed after saving changes Region oldRegion = _context.Region.AsNoTracking().Where <Region>(e => e.regionID == id).FirstOrDefault(); _context.Region.Update(region); await _context.SaveChangesAsync(); //Check to see if the region has been set back to active if (oldRegion.inactive == true && region.inactive == false) { //Get a list of all the routes associated with the region ID that are not completed List <Route> routes = _context.Route.Where <Route>(e => e.regionID == region.regionID && e.completed == false && e.inactive == false).ToList(); //if routes active incomplete is 0 Create a new route if (routes.Count == 0) { DateTime nextDate = dc.GetNextDates(1, region).Single(); Route newRoute = new Route { routeName = region.regionName + " - " + nextDate.ToString("MMMM dd, yyyy"), regionID = region.regionID, region = region, completed = false, inactive = false, routeDate = nextDate }; _context.Route.Update(newRoute); await _context.SaveChangesAsync(); } } //Create an anonymous object that contains the nextPickupDate //Is used to send back the next collection date, to be displayed on the front end var returnRegion = new { regionID = region.regionID, regionName = region.regionName, frequency = region.frequency, firstDate = region.firstDate, nextDate = dc.GetNextDates(1, region), inactive = region.inactive }; return(Ok(returnRegion)); }