public async Task <Response> Handle(Command command, CancellationToken cancel) { var existing = await _salesChannelRepository .Get(SalesChannelSpecs.WithSalesChannelId(command.SalesChannelId), cancel); if (existing is not null) { existing.ChangeLocations(command.Locations); _salesChannelRepository.Update(existing); } else { var salesChannel = new SalesChannel(command.SalesChannelId, command.Locations); _salesChannelRepository.Add(salesChannel); } await _uow.Commit(cancel); return(new Response()); }
private async Task ThrowIfNoLocation(Command command, CancellationToken cancel) { // todo move this method into ISalesChannelRepository var locations = command .Stocks .Select(stock => stock.Location) .ToHashSet(StringComparer.OrdinalIgnoreCase); var spec = SalesChannelSpecs.InLocations(locations.ToArray()); var channels = await _salesChannelRepository.FindAll(spec, cancel); var haveSalesChannelsInLocations = channels .SelectMany(x => x.Locations) .ToHashSet() .IsSupersetOf(locations); if (!haveSalesChannelsInLocations) { throw new NotFoundException(); } }