public async Task <DistributionResponse> SaveAsync(Distribution distribution) { try { /* * Notice here we have to check if the dataset ID is valid before adding the distribution, to avoid errors. * You can create a method into the DatasetService class to return the dataset and inject the service here if you prefer, but * it doesn't matter given the API scope. */ var existingDataset = await _datasetRepository.FindByIdAsync(distribution.DatasetId); if (existingDataset == null) { return(new DistributionResponse("Invalid dataset.")); } await _distributionRepository.AddAsync(distribution); // Send notification await _notificationService.AddUserNotificationsAsync(existingDataset, existingDataset, existingDataset.Title + " - " + existingDataset.Publisher.Name, "Datasettet '" + existingDataset.Title + "' har lagt til en ny distribusjon."); await _notificationService.AddPublisherNotificationsAsync(existingDataset, existingDataset, existingDataset.Title + " - " + existingDataset.Publisher.Name, "Datasettet ditt '" + existingDataset.Title + "' har lagt til en ny distribusjon."); await _unitOfWork.CompleteAsync(); return(new DistributionResponse(distribution)); } catch (Exception ex) { // Do some logging stuff return(new DistributionResponse($"An error occurred when saving the distribution: {ex.Message}")); } }
// Add a distribution in a graph to the database public async Task <List <Distribution> > AddDistribution(Graph g, int datasetId) { // Find the distribution subject uri IUriNode dcatDistribution = g.CreateUriNode("dcat:Distribution"); String[] distributionUris = findSubjectUri(g, dcatDistribution).Split(","); List <Distribution> distributions = new List <Distribution>(); foreach (String distributionUri in distributionUris) { Dictionary <string, string> attributes = getAttributesFromSubject(g, distributionUri); //Parse file format EFileFormat fileFormat = EFileFormat.annet; String[] fileFormatString = attributes.GetValueOrDefault("format", "annet").Split(","); try { fileFormat = (EFileFormat)Enum.Parse(typeof(EFileFormat), fileFormatString[0], true); } catch (Exception ex) { Console.WriteLine(ex.Message); } // Add relevant attributes to a new distribution Distribution distribution = new Distribution { Title = (string)attributes.GetValueOrDefault("title", attributes.GetValueOrDefault("description", "")), Uri = (string)attributes.GetValueOrDefault("accessURL", ""), FileFormat = fileFormat, DatasetId = datasetId }; // Add the dataset to the distribution await _distributionRepository.AddAsync(distribution); await _unitOfWork.CompleteAsync(); distributions.Add(distribution); } return(distributions); }