public async Task <DatasetDto> AddPreApprovedDatasetToStudyAsync(int studyId, int datasetId) { // Run validations: (Check if both id's are valid) var studyFromDb = await _studyModelService.GetForDatasetsAsync(studyId, UserOperation.Study_AddRemove_Dataset); var datasetFromDb = await _studySpecificDatasetModelService.GetByIdWithoutPermissionCheckAsync(datasetId); if (datasetFromDb == null) { throw NotFoundException.CreateForEntity("Dataset", datasetId); } if (datasetFromDb.StudySpecific) { throw new ArgumentException($"Dataset {datasetId} is Study specific, and cannot be linked using this method."); } // Create new entry in linking table var studyDataset = new StudyDataset { Study = studyFromDb, Dataset = datasetFromDb }; await _db.StudyDatasets.AddAsync(studyDataset); await _db.SaveChangesAsync(); return(_mapper.Map <DatasetDto>(studyDataset.Dataset)); }
void CheckExistenceAndThrowIfMissing(int sandboxId, Sandbox sandbox) { if (sandbox == null) { throw NotFoundException.CreateForEntity("Sandbox", sandboxId); } }
protected async Task <CloudResource> GetInternalWithoutAccessCheckAsync(int id, bool readOnly = false, bool onlyNonDeleted = false, bool throwIfNotFound = false, bool includeAccessCheckEntities = false) { var queryable = BaseQueryable(); if (includeAccessCheckEntities) { queryable = AddAccessCheckIncludes(queryable); } queryable = queryable.Where(r => r.Id == id); if (onlyNonDeleted) { queryable = AddNotDeletedFilter(queryable); } if (readOnly) { queryable = queryable.AsNoTracking(); } var entityFromDb = await queryable.SingleOrDefaultAsync(); if (entityFromDb == null && throwIfNotFound) { throw NotFoundException.CreateForEntity("CloudResource", id); } return(entityFromDb); }
protected async Task <Dataset> GetFromQueryableThrowIfNotFound(IQueryable <Dataset> queryable, int datasetId) { var dataset = await queryable.SingleOrDefaultAsync(s => s.Id == datasetId); if (dataset == null) { throw NotFoundException.CreateForEntity("Dataset", datasetId); } return(dataset); }
async Task <CloudResourceOperation> GetFromQueryableThrowIfNotFound(IQueryable <CloudResourceOperation> queryable, int id) { var cloudResourceOperation = await queryable.SingleOrDefaultAsync(s => s.Id == id); if (cloudResourceOperation == null) { throw NotFoundException.CreateForEntity("CloudResourceOperation", id); } return(cloudResourceOperation); }
async Task <Study> GetStudyFromQueryableThrowIfNotFound(IQueryable <Study> queryable, int studyId) { var study = await queryable.SingleOrDefaultAsync(s => s.Id == studyId); if (study == null) { throw NotFoundException.CreateForEntity("Study", studyId); } return(study); }
Dataset GetStudySpecificDatasetOrThrow(Study study, int datasetId) { var studyDatasetRelation = study.StudyDatasets.FirstOrDefault(sd => sd.DatasetId == datasetId); if (studyDatasetRelation == null) { throw NotFoundException.CreateForEntity("StudyDataset", datasetId); } return(studyDatasetRelation.Dataset); }
public async Task <Sandbox> GetByIdForResourcesAsync(int sandboxId) { var sandboxQueryable = SandboxBaseQueries.SandboxWithStudyParticipantResourceAndOperations(_db).AsNoTracking(); var sandbox = await GetSandboxFromQueryableThrowIfNotFoundOrNoAccess(sandboxQueryable, sandboxId, UserOperation.Study_Read); if (sandbox.Deleted && sandbox.DeletedAt.HasValue && sandbox.DeletedAt.Value.AddMinutes(15) < DateTime.UtcNow) { throw NotFoundException.CreateForEntity("Sandbox", sandboxId); } return(sandbox); }
async Task <Sandbox> GetSandboxFromQueryableThrowIfNotFoundOrNoAccess(IQueryable <Sandbox> queryable, int sandboxId, UserOperation operation, bool readOnly) { var sandbox = await queryable.Where(sb => sb.Id == sandboxId).If(readOnly, x => x.AsNoTracking()).SingleOrDefaultAsync(); if (sandbox == null) { throw NotFoundException.CreateForEntity("Sandbox", sandboxId); } await _studyPermissionService.VerifyAccessOrThrow(sandbox.Study, operation); return(sandbox); }
protected async Task <CloudResource> GetResourceOrThrowAsync(int id) { var entityFromDb = await _db.CloudResources .Include(sr => sr.Operations) .FirstOrDefaultAsync(s => s.Id == id); if (entityFromDb == null) { throw NotFoundException.CreateForEntity("AzureResource", id); } return(entityFromDb); }
public async Task <DatasetDto> GetDatasetByStudyIdAndDatasetIdAsync(int studyId, int datasetId) { var studyFromDb = await _studyModelService.GetForDatasetsAsync(studyId); var studyDatasetRelation = studyFromDb.StudyDatasets.FirstOrDefault(sd => sd.DatasetId == datasetId); if (studyDatasetRelation == null) { throw NotFoundException.CreateForEntity("StudyDataset", datasetId); } var datasetDto = _mapper.Map <DatasetDto>(studyDatasetRelation.Dataset); await DecorateDtoStudySpecific(_userService, studyFromDb, datasetDto.Permissions); return(datasetDto); }
async Task <CloudResource> MarkAsDeletedByIdInternalAsync(int id) { var resourceEntity = await _db.CloudResources.FirstOrDefaultAsync(s => s.Id == id); if (resourceEntity == null) { throw NotFoundException.CreateForEntity("SandboxResource", id); } var user = await _userService.GetCurrentUserAsync(); MarkAsDeletedInternal(resourceEntity, user.UserName); await _db.SaveChangesAsync(); return(resourceEntity); }
public async Task <AvailableDatasets> Add(int sandboxId, int datasetId) { var sandbox = await GetSandboxForDatasetOperationsAsync(sandboxId, UserOperation.Study_Crud_Sandbox, false, true); ValidateAddOrRemoveDataset(sandbox); var datasetFromDb = await _db.Datasets.FirstOrDefaultAsync(ds => ds.Id == datasetId); if (datasetFromDb == null) { throw NotFoundException.CreateForEntity("Dataset", datasetId); } if (datasetFromDb.StudySpecific) { var studyForDataset = DatasetUtils.GetStudyFromStudySpecificDatasetOrThrow(datasetFromDb); if (datasetFromDb.StudySpecific && studyForDataset.Id != sandbox.Study.Id) { throw new ArgumentException($"Dataset {datasetId} cannot be added to Sandbox {sandboxId}. The dataset is Study specific and belongs to another Study than {sandbox.Study.Id}."); } } var sandboxDatasetRelation = await _db.SandboxDatasets.FirstOrDefaultAsync(ds => ds.SandboxId == sandboxId && ds.DatasetId == datasetId); //Is dataset allready linked to this sandbox? if (sandboxDatasetRelation != null) { throw new ArgumentException($"Dataset is allready added to Sandbox."); } // Create new entry in the relation table var sandboxDataset = new SandboxDataset { SandboxId = sandboxId, DatasetId = datasetId, Added = DateTime.UtcNow, AddedBy = (await _userService.GetCurrentUserAsync()).UserName }; await _db.SandboxDatasets.AddAsync(sandboxDataset); await _db.SaveChangesAsync(); return(MapToAvailable(sandbox)); }
protected async Task <CloudResourceOperation> GetResourceOperationOrThrowAsync(int id, bool asNoTracking = false) { var entityFromDb = await _db.CloudResourceOperations .If(asNoTracking, x => x.AsNoTracking()) .Include(o => o.DependantOnThisOperation) .Include(o => o.DependsOnOperation) .ThenInclude(o => o.Resource) .Include(o => o.Resource) .ThenInclude(r => r.Sandbox) .ThenInclude(sb => sb.Study) .FirstOrDefaultAsync(o => o.Id == id); if (entityFromDb == null) { throw NotFoundException.CreateForEntity("SandboxResourceOperation", id); } return(entityFromDb); }
async Task <StudyParticipantDto> AddDbUserAsync(int studyId, int userId, string role) { var studyFromDb = await GetStudyForParticipantOperation(studyId, role); if (RoleAllreadyExistsForUser(studyFromDb, userId, role)) { throw new ArgumentException($"Role {role} allready granted for user {userId} on study {studyFromDb.Id}"); } var userFromDb = await _userService.GetByDbIdAsync(userId); if (userFromDb == null) { throw NotFoundException.CreateForEntity("User", userId); } StudyParticipant createdStudyParticipant = null; try { createdStudyParticipant = new StudyParticipant { StudyId = studyFromDb.Id, UserId = userId, RoleName = role }; await _db.StudyParticipants.AddAsync(createdStudyParticipant); await _db.SaveChangesAsync(); return(ConvertToDto(createdStudyParticipant, userFromDb)); } catch (Exception) { await RemoveIfExist(createdStudyParticipant); throw; } }