Exemple #1
0
        public async Task <IActionResult> AssignLocations([FromRoute] int regID, [FromBody] List <Location> locations)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Checks if the regionID is greater than 0 and if it cannot be found
            if (regID > 0 && _context.Region.Find(regID) == null)
            {
                return(BadRequest("Invalid Region ID"));
            }

            foreach (Location loc in locations)
            {
                if (regID == 0)
                {
                    loc.regionID = null;
                }
                else
                {
                    if (regID < 0)
                    {
                        return(BadRequest("Invalid Region ID"));
                    }
                    else
                    {
                        loc.regionID = regID;
                    }
                }
            }

            _context.UpdateRange(locations);
            _context.SaveChanges();
            return(Ok(locations));
        }