public IActionResult Put([FromBody] SeriesDTO dto, [FromRoute] Guid id) { var cars = dto.CarIds.Count == 0 ? new List <Car>() :_context.Cars.Where(c => dto.CarIds.Contains(c.Id)).ToList(); if (cars.Count != dto.CarIds.Count) { return(BadRequest("Unable to find all the car ids for the series")); } var existing = _context.Series .Include(s => s.SeriesCars).ThenInclude(s => s.Car).FirstOrDefault(s => s.Id == id); if (existing == null) { return(NotFound($"Series with id {id} was not found")); } existing.Name = dto.Name; existing.IsArchived = dto.IsArchived; existing.LastUpdated = DateTime.UtcNow; existing.LogoImgUrl = dto.LogoImgUrl; existing.Description = dto.Description; existing.IsLeague = dto.IsLeague; _context.SaveChanges(); if (cars.Count > 0) { var existingSeriesCars = _context.SeriesCars.Where(s => s.SeriesId == id).Include(c => c.Car); var seriesCars = new List <SeriesCar>(); var carIds = dto.CarIds; foreach (var seriesCar in existingSeriesCars) { if (!carIds.Contains(seriesCar.CarId)) { _context.SeriesCars.Remove(seriesCar); } else { seriesCars.Add(seriesCar); } } var newCarIds = carIds.Where(c => existingSeriesCars.Where(s => s.CarId == c).Count() == 0).ToList(); foreach (var car in newCarIds) { SeriesCar seriesCar = new SeriesCar { Series = existing, SeriesId = existing.Id, Car = cars.First(c => c.Id == car), CarId = car }; _context.SeriesCars.Add(seriesCar); seriesCars.Add(seriesCar); } _context.SaveChanges(); existing.SeriesCars = seriesCars; } return(Ok(new SeriesDTO(existing))); }
public IActionResult Post([FromBody] SeriesDTO dto) { var cars = dto.CarIds.Count == 0 ? new List <Car>() :_context.Cars.Where(c => dto.CarIds.Contains(c.Id)).ToList(); if (cars.Count != dto.CarIds.Count) { return(BadRequest("Unable to find all the car ids for the series")); } Series series = new Series() { Name = dto.Name, IsTeam = dto.IsTeam, IsArchived = dto.IsArchived, LastUpdated = DateTime.UtcNow, LogoImgUrl = dto.LogoImgUrl, Description = dto.Description, IsLeague = dto.IsLeague }; _context.Series.Add(series); _context.SaveChanges(); if (cars.Count > 0) { var list = new List <SeriesCar>(); foreach (var car in cars) { SeriesCar seriesCar = new SeriesCar { Series = series, SeriesId = series.Id, Car = car, CarId = car.Id }; _context.SeriesCars.Add(seriesCar); list.Add(seriesCar); } series.SeriesCars = list; _context.SaveChanges(); } return(Ok(new SeriesDTO(series))); }