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}"));
            }
        }
        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}"));
            }
        }
        public async Task <CoordinationResponse> UpdateAsync(int id, Coordination coordination)
        {
            var existingCoordination = await _coordinationRepository.FindByIdAsync(id);

            if (existingCoordination == null)
            {
                return(new CoordinationResponse("Coordination not found."));
            }

            try
            {
                (Boolean success, String error)check = await idChecks(coordination);

                if (!check.success)
                {
                    return(new CoordinationResponse(check.error));
                }
                // Set last updated to current time
                existingCoordination.DateLastUpdated = DateTime.Now;

                // Update attributes
                existingCoordination.Title             = coordination.Title;
                existingCoordination.Description       = coordination.Description;
                existingCoordination.PublisherId       = coordination.PublisherId;
                existingCoordination.UnderCoordination = coordination.UnderCoordination;
                existingCoordination.StatusDescription = coordination.StatusDescription;
                existingCoordination.CategoryId        = coordination.CategoryId;
                existingCoordination.TagsIds           = coordination.TagsIds;
                existingCoordination.AccessLevel       = coordination.AccessLevel;

                existingCoordination.CoordinationTags.Clear();
                await addTags(existingCoordination);

                _coordinationRepository.Update(existingCoordination);

                // Send notifications
                await _notificationService.AddUserNotificationsAsync(existingCoordination, existingCoordination, existingCoordination.Title + " - " + existingCoordination.Publisher.Name, "Samordningen '" + existingCoordination.Title + "' har blitt oppdatert.");

                await _notificationService.AddPublisherNotificationsAsync(existingCoordination, existingCoordination, existingCoordination.Title + " - " + existingCoordination.Publisher.Name, "Samordningen din '" + existingCoordination.Title + "' har blitt oppdatert.");

                await _unitOfWork.CompleteAsync();

                if (coordination.GitlabProjectId == null)
                {
                    var createCoordinationTask = Task.Run(() => {
                        return(existingCoordination);
                    });
                    await CreateGitLabProject(createCoordinationTask, existingCoordination);
                }
                await _gitlabService.UpdateProject(existingCoordination);

                return(new CoordinationResponse(existingCoordination));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new CoordinationResponse($"An error occurred when updating the coordination: {ex.Message}"));
            }
        }
Exemple #5
0
        public async Task <DatasetResponse> UpdateAsync(int id, Dataset dataset)
        {
            var existingDataset = await _datasetRepository.FindByIdAsync(id);

            if (existingDataset == null)
            {
                return(new DatasetResponse("Dataset not found."));
            }

            try
            {
                (Boolean success, String error)check = await idChecks(dataset);

                if (!check.success)
                {
                    return(new DatasetResponse(check.error));
                }
                // Set last updated to current time
                existingDataset.DateLastUpdated = DateTime.Now;

                // Update attributes
                existingDataset.Title                = dataset.Title;
                existingDataset.Description          = dataset.Description;
                existingDataset.PublisherId          = dataset.PublisherId;
                existingDataset.PublicationStatus    = dataset.PublicationStatus;
                existingDataset.DatePlannedPublished = dataset.DatePlannedPublished;
                existingDataset.AccessLevel          = dataset.AccessLevel;
                existingDataset.TagsIds              = dataset.TagsIds;
                existingDataset.CategoryId           = dataset.CategoryId;
                existingDataset.CoordinationId       = dataset.CoordinationId;
                existingDataset.InterestCounter      = dataset.InterestCounter;

                existingDataset.DatasetTags.Clear();
                await addTags(existingDataset);

                _datasetRepository.Update(existingDataset);

                // Send notifications
                await _notificationService.AddUserNotificationsAsync(existingDataset, existingDataset, existingDataset.Title + " - " + existingDataset.Publisher.Name, "Datasettet '" + existingDataset.Title + "' har blitt oppdatert.");

                await _notificationService.AddPublisherNotificationsAsync(existingDataset, existingDataset, existingDataset.Title + " - " + existingDataset.Publisher.Name, "Datasettet ditt '" + existingDataset.Title + "' har blitt oppdatert.");

                await _unitOfWork.CompleteAsync();

                if (dataset.GitlabProjectId == null)
                {
                    var createDatasetTask = Task.Run(() => {
                        return(existingDataset);
                    });
                    await CreateGitLabProject(createDatasetTask, existingDataset);
                }
                await _gitlabService.UpdateProject(existingDataset);

                return(new DatasetResponse(existingDataset));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new DatasetResponse($"An error occurred when updating the dataset: {ex.Message}"));
            }
        }