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}"));
            }
        }
Example #2
0
        public async Task <DatasetResponse> FindByIdAsync(int id)
        {
            try
            {
                var res = await _datasetRepository.FindByIdAsync(id);

                if (res == null)
                {
                    return(new DatasetResponse("Invalid dataset id."));
                }
                return(new DatasetResponse(res));
            }
            catch (Exception ex)
            {
                return(new DatasetResponse($"An error occurred when trying to get the dataset: {ex.Message}"));
            }
        }
        public async Task <UserResponse> SubscribeAsync(Subscription subscription)
        {
            try
            {
                var user = await _userRepository.FindByIdAsync((int)subscription.UserId);

                if (user == null)
                {
                    return(new UserResponse("User not found."));
                }

                if (subscription.DatasetId != null && subscription.DatasetId != 0)
                {
                    var dataset = await _datasetRepository.FindByIdAsync((int)subscription.DatasetId);

                    if (dataset == null)
                    {
                        return(new UserResponse("Dataset not found."));
                    }
                    // Send notification
                    await _notificationService.AddPublisherNotificationsAsync(dataset, dataset, dataset.Title + " - " + dataset.Publisher.Name, "En bruker '" + user.Username + "' har abonnert på ditt dataset.");
                }
                else if (subscription.CoordinationId != null && subscription.CoordinationId != 0)
                {
                    var coordination = await _coordinationRepository.FindByIdAsync((int)subscription.CoordinationId);

                    if (coordination == null)
                    {
                        return(new UserResponse("Coordination not found."));
                    }
                    // Send notification
                    await _notificationService.AddPublisherNotificationsAsync(coordination, coordination, coordination.Title + " - " + coordination.Publisher.Name, "En bruker '" + user.Username + "' har abonnert på din samordning.");
                }
                else
                {
                    return(new UserResponse("No dataset or coordination was found."));
                }

                await _userRepository.AddSubscriptionAsync(subscription);

                await _unitOfWork.CompleteAsync();

                return(new UserResponse(user));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new UserResponse($"An error occurred when saving the user: {ex.Message}"));
            }
        }
        public async Task <ApplicationResponse> SaveAsync(Application application)
        {
            try
            {
                var coordination = await _coordinationRepository.FindByIdAsync(application.CoordinationId);

                if (coordination == null)
                {
                    return(new ApplicationResponse("Coordination not found."));
                }

                if (application.DatasetId != null && application.DatasetId != 0)
                {
                    var dataset = await _datasetRepository.FindByIdAsync((int)application.DatasetId);

                    if (dataset == null)
                    {
                        return(new ApplicationResponse("Dataset not found."));
                    }
                    // Send notification
                    await _notificationService.AddPublisherNotificationsAsync(coordination, dataset, coordination.Title + " - " + coordination.Publisher.Name, "Datasettet '" + dataset.Title + "' har spurt om å være med i din samordning.");
                }
                else if (application.PublisherId != null && application.PublisherId != 0)
                {
                    var publisher = await _publisherRepository.FindByIdAsync((int)application.PublisherId);

                    if (publisher == null)
                    {
                        return(new ApplicationResponse("Publisher not found."));
                    }
                    // Send notification
                    await _notificationService.AddPublisherNotificationsAsync(publisher, coordination, coordination.Title + " - " + coordination.Publisher.Name, "En bruker ønsker at du publiserer et dataset i samordningen '" + coordination.Title + "'.");
                }
                else
                {
                    return(new ApplicationResponse("No dataset or publisher was found."));
                }

                await _applicationRepository.AddAsync(application);

                await _unitOfWork.CompleteAsync();


                return(new ApplicationResponse(application));
            }
            catch (Exception ex)
            {
                return(new ApplicationResponse($"An error occured when saving the tag: {ex.Message}"));
            }
        }