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}")); } }
private async Task <(Boolean success, String error)> idChecks(Dataset dataset) { // Make sure the publisher exists var existingPublisher = await _publisherRepository.FindByIdAsync(dataset.PublisherId); if (existingPublisher == null) { return(false, "Invalid publisher id."); } // Make sure the category exists var existingCategory = await _categoryRepository.FindByIdAsync(dataset.CategoryId); if (existingCategory == null) { return(false, "Invalid category id."); } // Make sure the coordination exists if present if (dataset.CoordinationId != null) { var existingCoordination = await _coordinationRepository.FindByIdAsync((int)dataset.CoordinationId); if (existingCoordination == null) { return(false, "Invalid coordination id."); } } return(true, "Success."); }
public async Task <CoordinationResponse> FindByIdAsync(int id) { try { var res = await _coordinationRepository.FindByIdAsync(id); if (res == null) { return(new CoordinationResponse("Invalid coordination id.")); } return(new CoordinationResponse(res)); } catch (Exception ex) { return(new CoordinationResponse($"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}")); } }