public AssetLocation([NotNull] Data.Models.AssetLocation other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } Id = other.Id; Location = other.Location; Type = (LocationType)(int)other.Type; }
public async Task <IActionResult> BulkAddLocations([Required, FromBody] IEnumerable <AssetAndLocation> updates) { var errorsToReport = new List <string>(); var groupsOfAssetsToUpdate = updates.GroupBy(upd => upd.AssetId); foreach (var assetToUpdate in groupsOfAssetsToUpdate) { var asset = await _context.Assets .Include(a => a.Locations) .Where(a => a.Id == assetToUpdate.Key) .SingleOrDefaultAsync(); if (asset == null) { errorsToReport.Add($"The asset with id '{assetToUpdate.Key}' was not found."); continue; } foreach (var assetNewLocations in assetToUpdate) { var assetLocation = new Data.Models.AssetLocation { Location = assetNewLocations.Location, Type = (Data.Models.LocationType)assetNewLocations.LocationType, }; // If asset location is already in the asset skip to next asset location if (asset.Locations != null && asset.Locations.Any(existing => existing.Location.Equals(assetLocation.Location, StringComparison.OrdinalIgnoreCase) && existing.Type == assetLocation.Type)) { continue; } asset.Locations = asset.Locations ?? new List <Data.Models.AssetLocation>(); asset.Locations.Add(assetLocation); _context.Assets.Update(asset); } } if (errorsToReport.Any()) { return(NotFound(new ApiError("Error adding asset locations.", errorsToReport))); } await _context.SaveChangesAsync(); return(StatusCode((int)HttpStatusCode.Created)); }
public async Task <IActionResult> AddAssetLocationToAsset(int assetId, [Required] string location, [Required] LocationType assetLocationType) { var assetLocation = new Data.Models.AssetLocation { Location = location, Type = (Maestro.Data.Models.LocationType)assetLocationType, }; Maestro.Data.Models.Asset asset = await _context.Assets .Include(a => a.Locations) .Where(a => a.Id == assetId) .SingleOrDefaultAsync(); if (asset == null) { return(NotFound(new ApiError($"The asset with id '{assetId}' was not found."))); } // If asset location is already in asset, nothing to do if (asset.Locations != null && asset.Locations.Any(existing => existing.Location.Equals(assetLocation.Location, StringComparison.OrdinalIgnoreCase) && existing.Type == assetLocation.Type)) { return(StatusCode((int)HttpStatusCode.NotModified)); } asset.Locations = asset.Locations ?? new List <Data.Models.AssetLocation>(); asset.Locations.Add(assetLocation); await _context.SaveChangesAsync(); return(CreatedAtRoute( new { action = "GetAsset", id = assetLocation.Id }, new AssetLocation(assetLocation))); }