コード例 #1
0
        public async Task <LockTag> TryCreateAsync(LockTag link, string userId)
        {
            try
            {
                var dbEntity = ConvertToDb(link, userId);
                using (var db = Connection)
                {
                    await db.ExecuteAsync(
                        "INSERT INTO LockTags " +
                        $"({nameof(LockTagDbEntity.Id)}, {nameof(LockTagDbEntity.CreatedBy)}, {nameof(LockTagDbEntity.CreatedDate)}, " +
                        $"{nameof(LockTagDbEntity.ModifiedBy)}, {nameof(LockTagDbEntity.ModifiedDate)}, {nameof(LockTagDbEntity.LockId)}, " +
                        $"{nameof(LockTagDbEntity.TagId)}) " +
                        "VALUES " +
                        $"(@{nameof(LockTagDbEntity.Id)}, @{nameof(LockTagDbEntity.CreatedBy)}, @{nameof(LockTagDbEntity.CreatedDate)}, " +
                        $"@{nameof(LockTagDbEntity.ModifiedBy)}, @{nameof(LockTagDbEntity.ModifiedDate)}, @{nameof(LockTagDbEntity.LockId)}, " +
                        $"@{nameof(LockTagDbEntity.TagId)}) ",
                        dbEntity
                        ).ConfigureAwait(false);
                }

                return(ConvertFromDb(dbEntity));
            }
            catch (SqliteException e)
            {
                if (e.IsUniqueConstraintViolation())
                {
                    return(null);
                }
                throw;
            }
        }
コード例 #2
0
        public async Task <bool> DeleteLink(LockTag link, string userId)
        {
            var deleted = await _locksTagsRepository.DeleteLinkAsync(link, userId).ConfigureAwait(false);

            if (deleted)
            {
                await _eventPublisher.SendTagUnlinkedMessageAsync(link, userId).ConfigureAwait(false);
            }

            return(deleted);
        }
コード例 #3
0
        public async Task <LockTag> CreateLink(LockTag link, string userId)
        {
            var entity = await _locksTagsRepository.TryCreateAsync(link, userId).ConfigureAwait(false);

            if (entity != null)
            {
                await _eventPublisher.SendTagLinkedMessageAsync(entity, userId).ConfigureAwait(false);
            }

            return(entity);
        }
コード例 #4
0
ファイル: EventPublisher.cs プロジェクト: Nero0909/LuckyLock
        public Task SendTagUnlinkedMessageAsync(LockTag link, string userId)
        {
            var message = new TagUnlinkedMessage
            {
                EventCreatedDate = DateTime.UtcNow,
                LockId           = link.LockId,
                UserId           = userId,
                EventId          = Guid.NewGuid(),
                TagId            = link.TagId
            };

            return(_bus.Publish <TagUnlinked>(message));
        }
コード例 #5
0
 private LockTagDbEntity ConvertToDb(LockTag @lock, string userId)
 {
     return(new LockTagDbEntity
     {
         Id = @lock.Id,
         CreatedBy = userId,
         CreatedDate = @lock.CreatedDate,
         ModifiedBy = userId,
         ModifiedDate = DateTime.UtcNow,
         TagId = @lock.TagId,
         LockId = @lock.LockId
     });
 }
コード例 #6
0
        public async Task <IActionResult> DeleteTagLinkAsync([FromRoute] string lockid, [FromRoute] string tagid)
        {
            if (!Guid.TryParse(lockid, out var lockIdParsed))
            {
                return(BadRequest(new ErrorResponse {
                    Error = "Invalid lock id"
                }));
            }

            if (!Guid.TryParse(tagid, out var tagIdParsed))
            {
                return(BadRequest(new ErrorResponse {
                    Error = "Invalid tag id"
                }));
            }

            var userId     = _identityService.GetUserIdentity();
            var lockExists = await _locksService.CheckLockExistence(lockIdParsed, userId).ConfigureAwait(false);

            if (!lockExists)
            {
                return(UnprocessableEntity(new ErrorResponse {
                    Error = "Lock does not exist"
                }));
            }

            var tagExists = await _locksTagsService.CheckTagExistence(tagIdParsed, userId).ConfigureAwait(false);

            if (!tagExists)
            {
                return(UnprocessableEntity(new ErrorResponse {
                    Error = "Tag does not exist"
                }));
            }

            var toDelete = new LockTag
            {
                LockId = lockIdParsed,
                TagId  = tagIdParsed,
            };

            var deleted = await _locksTagsService.DeleteLink(toDelete, userId).ConfigureAwait(false);

            if (!deleted)
            {
                return(NotFound());
            }

            return(NoContent());
        }
コード例 #7
0
        public async Task <bool> DeleteLinkAsync(LockTag link, string userId)
        {
            using (var db = Connection)
            {
                var count = await db.ExecuteAsync(
                    "DELETE FROM LockTags " +
                    $"WHERE {nameof(LockTagDbEntity.TagId)} = @{nameof(LockTagDbEntity.TagId)} " +
                    $"AND {nameof(LockTagDbEntity.LockId)} = @{nameof(LockTagDbEntity.LockId)} " +
                    $"AND {nameof(LockDbEntity.CreatedBy)} = @{nameof(LockDbEntity.CreatedBy)}",
                    new { TagId = link.TagId, LockId = link.LockId, CreatedBy = userId })
                            .ConfigureAwait(false);

                return(count == 1);
            }
        }
コード例 #8
0
        public async Task <IActionResult> CreateTagLinkAsync([FromBody] TagLinkModel model, [FromRoute] string lockid)
        {
            if (!Guid.TryParse(lockid, out var id))
            {
                return(BadRequest(new ErrorResponse {
                    Error = "Invalid lock id"
                }));
            }

            var userId     = _identityService.GetUserIdentity();
            var lockExists = await _locksService.CheckLockExistence(id, userId).ConfigureAwait(false);

            if (!lockExists)
            {
                return(UnprocessableEntity(new ErrorResponse {
                    Error = "Lock does not exist"
                }));
            }

            var tagExists = await _locksTagsService.CheckTagExistence(model.TagId, userId).ConfigureAwait(false);

            if (!tagExists)
            {
                return(UnprocessableEntity(new ErrorResponse {
                    Error = "Tag does not exist"
                }));
            }

            var toCreate = new LockTag
            {
                CreatedDate = DateTime.UtcNow,
                Id          = Guid.NewGuid(),
                LockId      = id,
                TagId       = model.TagId
            };

            var link = await _locksTagsService.CreateLink(toCreate, userId).ConfigureAwait(false);

            if (link == null)
            {
                return(Conflict(new ErrorResponse {
                    Error = "Link already exists"
                }));
            }

            return(StatusCode((int)HttpStatusCode.Created));
        }
コード例 #9
0
        //
        // Lock Tag Tab
        //

        private void LockButton_Click(object sender, RoutedEventArgs e)
        {
            ShowPendingStatus("Waiting for tap");
            Command readCommand = new DetectSingleTagUid((byte)timeout.Value, DetectTagSetting.Type2Type4AandMifare);

            tappy.SendCommand(readCommand, delegate(ResponseFrame frame, Exception exc)
            {
                if (CheckForErrorsOrTimeout(frame, exc))
                {
                    return;
                }

                Tag tag = new Tag(frame.Data);

                Action Lock = () =>
                {
                    Command lockCommand = new LockTag((byte)timeout.Value, tag.UID);

                    tappy.SendCommand(lockCommand, ResponseCallback);
                };

                Dispatcher.BeginInvoke(Lock);
            });
        }