public async Task TryInteract(DetectorInteractionDto dto) { await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { DetectorEntity detector = await db.GetRepo <DetectorEntity>().Get(dto.DetectorId.Value); AccountEntity account = await db.GetRepo <AccountEntity>().Get(dto.AccountId.Value); DetectorInteractionEventEntity detectorEvent = new DetectorInteractionEventEntity() { account_id = account.id, detector_id = detector.id, timespan = DateTime.Now }; NotPermittedException ex = null; if (account.Roles.SelectMany(r => r.DetectorPermissions).Any(m => m.id == detector.id)) { detectorEvent.log = $"Interaction with Detector #{detector.id} by Account #{account.id}: SUCCESS"; } else { detectorEvent.log = $"Interaction with Detector #{detector.id} by Account #{account.id}: ACCESS DENIED"; ex = new NotPermittedException(detectorEvent.log); } await db.GetRepo <DetectorInteractionEventEntity>().Create(detectorEvent); await db.Save(); if (ex != null) { throw ex; } } }); }
public async Task <HttpResponseMessage> TryInteract([FromBody] DetectorInteractionDto dto) { return(await Execute(d => DetectorService.TryInteract(d), dto)); }