public async Task <TaskModel> NotifyReviewed(AuthorizedDto <ReviewTaskDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { TaskEntity task = await db.GetRepo <TaskEntity>().Get(dto.Data.Id.Value); if (task.reviewer_account_id != dto.Session.UserId) { throw new NotPermittedException($"Reviewer of task #{task.id}"); } if (dto.Data.ReviewResult.Value) { task.is_reviewed = true; if (task.AssociatedFault != null) { task.AssociatedFault.is_fixed = true; } } else { task.is_done = false; } await db.Save(); return task.ToModel <TaskModel>(); } })); }
public async Task <CompanyModel> HireMember(AuthorizedDto <FireHireDto> model) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { AccountEntity account = await db.GetRepo <AccountEntity>().Get(model.Data.AccountId.Value); CompanyEntity company = await db.GetRepo <CompanyEntity>().Get(model.Data.CompanyId.Value); if (account == null) { throw new NotFoundException("Account"); } if (account.company_id.HasValue) { throw new AlreadyHiredException(); } account.company_id = model.Data.CompanyId; IList <RoleEntity> defaultRoles = await db.GetRepo <RoleEntity>().Get(role => role.is_default); account.Roles.Remove(defaultRoles.FirstOrDefault(role => role.name == DefaultRoles.AUTHORIZED.ToName())); account.Roles.Add(defaultRoles.FirstOrDefault(role => role.name == DefaultRoles.HIRED.ToName())); account.Bosses.Add(company.Owner); await db.Save(); await RoleService.CreateCompanyWorkerRole(account.id); return await GetCompany(account.company_id.Value); } })); }
public async Task Scrap(AuthorizedDto <PipelineDto> dto) { await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { PipelineEntity pipeline = await db.GetRepo <PipelineEntity>().FirstOrDefault( p => p.id == dto.Data.Id.Value ); foreach (PipelineItemEntity pipelineItem in pipeline.PipelineItems) { pipelineItem.pipeline_id = null; pipelineItem.InputPipelineItems.Clear(); pipelineItem.OutputPipelineItems.Clear(); pipelineItem.InputStorageCells.Clear(); pipelineItem.OutputStorageCells.Clear(); } foreach (StorageCellEntity storageCell in pipeline.StorageCells) { storageCell.pipeline_id = null; } await db.Save(); } }); }
public async Task <DetectorModel> Create(AuthorizedDto <DetectorDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detectorEntity = dto.Data.ToModel <DetectorModel>().ToEntity <DetectorEntity>(); DetectorPrefabEntity detectorPrefabEntity = await db.GetRepo <DetectorPrefabEntity>().Get(detectorEntity.detector_prefab_id); detectorEntity.DetectorSettingsValues = null; detectorEntity.DetectorFaultPrefabs = detectorEntity.DetectorFaultPrefabs.Select( faultPrefab => detectorPrefabEntity.DetectorFaultPrefabs.FirstOrDefault(fp => fp.id == faultPrefab.id) ).Where(fp => fp != null).ToList(); await db.GetRepo <DetectorEntity>().Create(detectorEntity); await db.Save(); RoleEntity ownerRole = await RoleService.GetCompanyOwnerRole(detectorPrefabEntity.company_id, db); ownerRole.DetectorPermissions.Add(detectorEntity); RoleEntity creatorRole = await RoleService.GetCompanyWorkerRole(dto.Session.UserId, db); creatorRole?.DetectorPermissions.Add(detectorEntity); await db.Save(); return detectorEntity.ToModel <DetectorModel>(); } })); }
public async Task <WorkerModel> RemoveBoss(AuthorizedDto <SetRemoveBossDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IRepo <AccountEntity> accountRepo = db.GetRepo <AccountEntity>(); AccountEntity initiator = await accountRepo.Get(dto.Session.UserId); AccountEntity sub = await accountRepo.Get(dto.Data.SubAccountId.Value); AccountEntity boss = await accountRepo.Get(dto.Data.BossAccountId.Value); if (!boss.Bosses.Contains(initiator)) { throw new NotPermittedException($"Boss for Account #{boss.id}"); } boss.Subs.Remove(sub); sub.Bosses.Remove(boss); await db.Save(); return sub.ToModel <WorkerModel>(); } })); }
public async Task <CompanyModel> CreateCompany(AuthorizedDto <CompanyDto> model) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { CompanyEntity company = model.Data.ToModel <CompanyModel>().ToEntity <CompanyEntity>(); company.Owner = await db.GetRepo <AccountEntity>().Get(model.Session.UserId); IList <RoleEntity> defaultRoles = await db.GetRepo <RoleEntity>().Get(role => role.is_default); company.Owner.Roles.Remove(defaultRoles.FirstOrDefault(role => role.name == DefaultRoles.AUTHORIZED.ToName())); company.Owner.Roles.Add(defaultRoles.FirstOrDefault(role => role.name == DefaultRoles.OWNER.ToName())); CompanyEntity created = await db.GetRepo <CompanyEntity>().Create(company); company.Owner.Company = created; await db.Save(); await RoleService.CreateCompanyOwnerRole(company.owner_id); return await GetCompany(created.owner_id); } })); }
public async Task <PipelineItemModel> Create(AuthorizedDto <PipelineItemDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { PipelineItemEntity pipelineItemEntity = dto.Data.ToModel <PipelineItemModel>().ToEntity <PipelineItemEntity>(); pipelineItemEntity.PipelineItemSettingsValues = null; await db.GetRepo <PipelineItemEntity>().Create(pipelineItemEntity); await db.Save(); PipelineItemPrefabEntity prefab = await db.GetRepo <PipelineItemPrefabEntity>().Get(pipelineItemEntity.pipeline_item_prefab_id); RoleEntity ownerRole = await RoleService.GetCompanyOwnerRole(prefab.company_id, db); ownerRole.PipelineItemPermissions.Add(pipelineItemEntity); RoleEntity creatorRole = await RoleService.GetCompanyWorkerRole(dto.Session.UserId, db); creatorRole?.PipelineItemPermissions.Add(pipelineItemEntity); await db.Save(); return pipelineItemEntity.ToModel <PipelineItemModel>(); } })); }
public async Task <WorkerModel> UnGrantRole(AuthorizedDto <GrantUngrantRoleDto> role) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IRepo <AccountEntity> accountRepo = db.GetRepo <AccountEntity>(); AccountEntity initiator = await accountRepo.Get(role.Session.UserId); AccountEntity grantedTo = await accountRepo.Get(role.Data.GrantToAccountId.Value); RoleEntity roleEntity = await db.GetRepo <RoleEntity>().Get(role.Data.RoleId.Value); if (!grantedTo.Bosses.Contains(initiator)) { throw new NotPermittedException($"Boss for Account #{grantedTo}"); } if (!IsRolesPermissionsIncludeRolePermissions(initiator.Roles.ToArray(), roleEntity)) { throw new NotPermittedException($"Granted permissions must be presented for Initiator"); } grantedTo.Roles.Remove(roleEntity); await db.Save(); return grantedTo.ToModel <WorkerModel>(); } })); }
public async Task <AccountModel[]> GetFreeAccounts(AuthorizedDto <IdDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IList <AccountEntity> freeAccounts = await db.GetRepo <AccountEntity>().Get(acc => acc.company_id == null); return ModelEntityMapper.Mapper.Map <IList <AccountModel> >(freeAccounts).ToArray(); } })); }
public async Task <WorkerModel> Get(AuthorizedDto <AccountDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { AccountEntity account = await db.GetRepo <AccountEntity>().FirstOrDefault(acc => acc.id == dto.Data.Id.Value); return account.ToModel <WorkerModel>(); } })); }
public async Task <PipelineModel> Get(AuthorizedDto <PipelineDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { PipelineEntity pipeline = await db.GetRepo <PipelineEntity>().Get(dto.Data.Id.Value); return ToPipelineModel(pipeline); } })); }
public async Task UnsetDetector(AuthorizedDto <DetectorDto> dto) { await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault(d => d.id == dto.Data.DetectorId.Value); detector.pipeline_item_id = null; await db.Save(); } }); }
public async Task <ICollection <PipelineItemModel> > Get(AuthorizedDto <CompanyDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IList <PipelineItemEntity> pipelineItems = await db.GetRepo <PipelineItemEntity>().Get( p => p.PipelineItemPrefab.company_id == dto.Data.CompanyId.Value ); return ModelEntityMapper.Mapper.Map <IList <PipelineItemModel> >(pipelineItems); } })); }
public async Task <PipelineModel[]> Get(AuthorizedDto <CompanyDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IList <PipelineEntity> pipelines = await db.GetRepo <PipelineEntity>().Get( p => p.company_id == dto.Data.Id.Value ); return pipelines.Select(pipeline => ToPipelineModel(pipeline)).ToArray(); } })); }
public async Task <ICollection <DetectorModel> > Get(AuthorizedDto <CompanyDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IList <DetectorEntity> detectors = await db.GetRepo <DetectorEntity>().Get( s => s.DetectorPrefab.company_id == dto.Data.CompanyId.Value ); return ModelEntityMapper.Mapper.Map <IList <DetectorModel> >(detectors); } })); }
public async Task <StorageCellPrefabModel> CreateStorageCellPrefab(AuthorizedDto <StorageCellPrefabDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { StorageCellPrefabEntity storageCellPrefabEntity = dto.Data.ToModel <StorageCellPrefabModel>().ToEntity <StorageCellPrefabEntity>(); await db.GetRepo <StorageCellPrefabEntity>().Create(storageCellPrefabEntity); await db.Save(); return storageCellPrefabEntity.ToModel <StorageCellPrefabModel>(); } })); }
public async Task <DetectorPrefabModel> CreateDetectorPrefab(AuthorizedDto <DetectorPrefabDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorPrefabEntity detectorPrefabEntity = dto.Data.ToModel <DetectorPrefabModel>().ToEntity <DetectorPrefabEntity>(); await db.GetRepo <DetectorPrefabEntity>().Create(detectorPrefabEntity); await db.Save(); return detectorPrefabEntity.ToModel <DetectorPrefabModel>(); } })); }
public async Task <PipelineItemPrefabModel> CreatePipelineItemPrefab(AuthorizedDto <PipelineItemPrefabDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { PipelineItemPrefabEntity pipelineItemPrefabEntity = dto.Data.ToModel <PipelineItemPrefabModel>().ToEntity <PipelineItemPrefabEntity>(); await db.GetRepo <PipelineItemPrefabEntity>().Create(pipelineItemPrefabEntity); await db.Save(); return pipelineItemPrefabEntity.ToModel <PipelineItemPrefabModel>(); } })); }
public async Task <TaskModel[]> GetMyTasks(AuthorizedDto <IdDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IList <TaskEntity> tasks = await db.GetRepo <TaskEntity>().Get( task => task.assignee_account_id == dto.Session.UserId || task.reviewer_account_id == dto.Session.UserId || task.creator_account_id == dto.Session.UserId ); return ModelEntityMapper.Mapper.Map <IList <TaskModel> >(tasks).ToArray(); } })); }
public async Task <TaskModel> Create(AuthorizedDto <TaskDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { TaskEntity task = dto.Data.ToModel <TaskModel>().ToEntity <TaskEntity>(); task.creator_account_id = dto.Session.UserId; await db.GetRepo <TaskEntity>().Create(task); await db.Save(); return task.ToModel <TaskModel>(); } })); }
public async Task <CompanyModel> UpdateCompany(AuthorizedDto <CompanyDto> model) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { CompanyEntity modifying = await db.GetRepo <CompanyEntity>().Get(model.Data.Id.Value); modifying.name = model.Data.Name; modifying.plan_image_url = model.Data.PlanImageUrl; await db.Save(); return await GetCompany(modifying.owner_id); } })); }
public async Task <CompanyModel> FireMember(AuthorizedDto <FireHireDto> model) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { AccountEntity account = await db.GetRepo <AccountEntity>().Get(model.Data.AccountId.Value); if (account == null) { throw new NotFoundException("Account"); } if (!account.company_id.HasValue || account.company_id != model.Data.CompanyId) { throw new NotHiredException(); } if (account.company_id.Value == account.id) //OWNER { throw new NotPermittedException(); } account.company_id = null; account.Roles.Clear(); account.Bosses.Clear(); account.Subs.Clear(); foreach (TaskEntity task in account.AssignedTasks) { task.assignee_account_id = null; } foreach (TaskEntity task in account.TasksToReview) { task.reviewer_account_id = null; } IList <RoleEntity> defaultRoles = await db.GetRepo <RoleEntity>().Get(role => role.is_default); account.Roles.Add(defaultRoles.FirstOrDefault(role => role.name == DefaultRoles.AUTHORIZED.ToName())); await db.Save(); return await GetCompany(model.Data.CompanyId.Value); } })); }
public async Task <ICollection <DetectorFaultEventModel> > GetActualFaults(AuthorizedDto <DetectorDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault( d => d.id == dto.Data.DetectorId.Value ); DetectorFaultEventEntity[] actualFaults = detector.detector_fault_events.Where( faultEvent => !faultEvent.is_fixed ).ToArray(); return ModelEntityMapper.Mapper.Map <ICollection <DetectorFaultEventModel> >(actualFaults); } })); }
public async Task <ICollection <DetectorFaultEventModel> > GetAllFaults(AuthorizedDto <GetDetectorDataDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault( d => d.id == dto.Data.DetectorId.Value ); DetectorFaultEventEntity[] faults = detector.detector_fault_events.Where( faultEvent => faultEvent.timespan >= dto.Data.DateSince && faultEvent.timespan <= dto.Data.DateUntil ).ToArray(); return ModelEntityMapper.Mapper.Map <ICollection <DetectorFaultEventModel> >(faults); } })); }
public async Task <PipelineItemModel> SetupDetector(AuthorizedDto <SetupDetectorDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault(d => d.id == dto.Data.DetectorId.Value); detector.pipeline_item_id = dto.Data.PipelineItemId.Value; await db.Save(); PipelineItemEntity pipelineItemEntity = await db.GetRepo <PipelineItemEntity>().FirstOrDefault( p => p.id == dto.Data.PipelineItemId.Value ); return pipelineItemEntity.ToModel <PipelineItemModel>(); } })); }
public async Task <TaskModel> NotifyDone(AuthorizedDto <TaskDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { TaskEntity task = await db.GetRepo <TaskEntity>().Get(dto.Data.Id.Value); if (task.assignee_account_id != dto.Session.UserId) { throw new NotPermittedException($"Assignee of task #{task.id}"); } task.is_done = true; await db.Save(); return task.ToModel <TaskModel>(); } })); }
public async Task <PipelineModel> Create(AuthorizedDto <PipelineDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { CompanyEntity company = await db.GetRepo <CompanyEntity>().FirstOrDefault( c => c.owner_id == dto.Data.CompanyId.Value ); int[] pipelineItemIds = dto.Data.PipelineItemPlacements.Select(p => p.PipelineItemId.Value).ToArray(); int[] storageCellIds = dto.Data.StorageCellPlacements.Select(s => s.StorageCellId.Value).ToArray(); IList <PipelineItemEntity> pipelineItems = await db.GetRepo <PipelineItemEntity>().Get( pipelineItem => pipelineItem.PipelineItemPrefab.company_id == company.owner_id ); IList <StorageCellEntity> storageCells = await db.GetRepo <StorageCellEntity>().Get( storageCell => storageCell.StorageCellPrefab.company_id == company.owner_id ); PipelineItemEntity[] updatedPipelineItems = pipelineItems.Where(p => pipelineItemIds.Contains(p.id)).ToArray(); StorageCellEntity[] updatedStorageCells = storageCells.Where(s => storageCellIds.Contains(s.id)).ToArray(); this.ValidatePipelineItems(updatedPipelineItems, null); this.ValidateStorageCells(updatedStorageCells, null); this.UpdatePipelineItemPlacements(company, updatedPipelineItems, dto.Data.PipelineItemPlacements); this.UpdateStorageCellPlacements(company, updatedStorageCells, dto.Data.StorageCellPlacements); PipelineEntity pipeline = new PipelineEntity(); pipeline.company_id = company.owner_id; await db.GetRepo <PipelineEntity>().Create(pipeline); this.UpdatePipelineConnections(updatedPipelineItems, updatedStorageCells, dto.Data, pipeline); await db.Save(); return ToPipelineModel(pipeline); } })); }
public async Task <AccountModel> UpdateProfile(AuthorizedDto <AccountDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { if (dto.Data.Id.Value != dto.Session.UserId) { throw new NotPermittedException($"UPDATE Account #{dto.Session.UserId}"); } AccountEntity account = await db.GetRepo <AccountEntity>().FirstOrDefault(acc => acc.id == dto.Data.Id.Value); account.first_name = dto.Data.FirstName; account.last_name = dto.Data.LastName; await db.Save(); return account.ToModel <AccountModel>(); } })); }
public async Task <DetectorDataModel> GetData(AuthorizedDto <GetDetectorDataDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().FirstOrDefault( d => d.id == dto.Data.DetectorId.Value ); DetectorDataEntity[] datas = detector.DetectorDatas.Where( data => data.timespan >= dto.Data.DateSince && data.timespan <= dto.Data.DateUntil ).ToArray(); return new DetectorDataModel() { Detector = detector.ToModel <DetectorModel>(), Data = ModelEntityMapper.Mapper.Map <ICollection <DetectorDataItemModel> >(datas) }; } })); }
public async Task <RoleModel> CreateRole(AuthorizedDto <RoleDto> role) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { AccountEntity account = await db.GetRepo <AccountEntity>().Get(role.Session.UserId); RoleEntity roleEntity = new RoleEntity(); roleEntity.name = role.Data.Name; roleEntity.company_id = role.Data.CompanyId.Value; roleEntity.ManufactoryPermissions = account.Roles.SelectMany(r => r.ManufactoryPermissions).Where( manufactory => role.Data.ManufactoryIds.Contains(manufactory.id) ).ToList(); roleEntity.PipelineItemPermissions = account.Roles.SelectMany(r => r.PipelineItemPermissions).Where( pi => role.Data.PipelineItemIds.Contains(pi.id) ).ToList(); roleEntity.StorageCellPermissions = account.Roles.SelectMany(r => r.StorageCellPermissions).Where( storageCell => role.Data.StorageCellIds.Contains(storageCell.id) ).ToList(); roleEntity.DetectorPermissions = account.Roles.SelectMany(r => r.DetectorPermissions).Where( detector => role.Data.DetectorIds.Contains(detector.id) ).ToList(); roleEntity.DbPermissions = account.Roles.SelectMany(r => r.DbPermissions).Where( dbPermission => role.Data.DbPermissionIds.Contains(dbPermission.id) ).ToList(); await db.GetRepo <RoleEntity>().Create(roleEntity); await db.Save(); return roleEntity.ToModel <RoleModel>(); } })); }