Ejemplo n.º 1
0
 public void Map(long deltaNumber, Cid deltaHash)
 {
     if (!_repository.TryGet(DeltaByNumber.BuildDocumentId(deltaNumber), out _))
     {
         _repository.Add(new DeltaByNumber(deltaNumber, deltaHash));
     }
 }
Ejemplo n.º 2
0
 public void Map(long deltaNumber, Cid deltaHash)
 {
     if (!_repository.TryGet(DeltaIndexDao.BuildDocumentId((ulong)deltaNumber), out _))
     {
         _repository.Add(new DeltaIndexDao {
             Height = (ulong)deltaNumber, Cid = deltaHash
         });
     }
 }
Ejemplo n.º 3
0
        public void TryGet_Should_Return_False_And_Null_If_Item_Does_Not_Exists(IRepository <Contact, string> repository)
        {
            Contact result;

            repository.TryGet(string.Empty, out result).ShouldBeFalse();
            result.ShouldBeNull();
        }
        void ValidateRequest(RecordRewardRequest request)
        {
            User user = userRepository.TryGet(request.Username)?.ToServiceModel();

            if (user is null)
            {
                AuthenticationException ex = new AuthenticationException("The provided user is not registered");

                logger.Error(
                    MyOperation.RecordReward,
                    OperationStatus.Failure,
                    ex,
                    new LogInfo(MyLogInfoKey.User, request.Username));

                throw ex;
            }

            bool isTokenValid = requestHmacEncoder.IsTokenValid(request.HmacToken, request, user.SharedSecretKey);

            if (!isTokenValid)
            {
                AuthenticationException ex = new AuthenticationException("The provided HMAC token is not valid");

                logger.Error(
                    MyOperation.RecordReward,
                    OperationStatus.Failure,
                    ex,
                    new LogInfo(MyLogInfoKey.User, request.Username),
                    new LogInfo(MyLogInfoKey.GiveawaysProvider, request.GiveawaysProvider),
                    new LogInfo(MyLogInfoKey.GiveawayId, request.GiveawayId));

                throw ex;
            }
        }
Ejemplo n.º 5
0
        public static Entity ToEntity(this ViewModel viewModel, IRepository repository)
        {
            Entity entity = repository.TryGet(viewModel.ID);

            if (entity == null)
            {
                entity = repository.Create();
            }
            entity.Name = viewModel.Name;
            return(entity);
        }
Ejemplo n.º 6
0
        public void TryGet_Should_Return_True_And_Item_If_Item_Exists(IRepository <Contact, string> repository)
        {
            var contact = new Contact {
                Name = "Test User", ContactTypeId = 1
            };

            repository.Add(contact);

            repository.TryGet(contact.ContactId, out Contact result).ShouldBeTrue();
            result.Name.ShouldBe(contact.Name);
            result.ContactTypeId.ShouldBe(contact.ContactTypeId);
        }
        /// <summary>
        /// MaintainListsOfOperations_ExplicitTryGetInsertUpdate.
        /// Benefit: You keep everything in the entity realm, rather than working with IDs.
        /// Benefit: Complete control over the CRUD of the entities.
        /// Benefit: CRUD operations seem neatly symmetrical and clean.
        /// Benefit: You can report exactly which operation is executed onto which entity.
        /// Downside: You do not reuse a method that is the singular form of the conversion.
        /// Downside: Performance of comparing ID's might be better than comparing entities.
        /// Downside: You might be counting on instance integrity,
        ///           while you might not have that depending on the persistence technology.
        ///           Identity integrity is more likely to be present than instance integrity.
        /// </summary>
        public static ConversionResult ConvertCollection(
            IList <ViewModel> viewModels,
            IList <Entity> existingEntities,
            IRepository repository)
        {
            var unmodifiedEntities = new List <Entity>();
            var updatedEntities    = new List <Entity>();
            var insertedEntities   = new List <Entity>();

            foreach (ViewModel viewModel in viewModels)
            {
                Entity entity = repository.TryGet(viewModel.ID);
                if (entity == null)
                {
                    entity      = repository.Create();
                    entity.Name = viewModel.Name;
                    insertedEntities.Add(entity);
                }
                else
                {
                    if (entity.Name != viewModel.Name)
                    {
                        entity.Name = viewModel.Name;
                        updatedEntities.Add(entity);
                    }
                    else
                    {
                        unmodifiedEntities.Add(entity);
                    }
                }
            }

            // Delete
            IList <Entity> entitiesToDelete = existingEntities.Except(updatedEntities)
                                              .Except(unmodifiedEntities)
                                              .ToArray();

            foreach (Entity entityToDelete in entitiesToDelete)
            {
                entityToDelete.UnlinkRelatedEntities();
                repository.Delete(entityToDelete);
            }

            return(new ConversionResult(
                       unmodifiedEntities,
                       insertedEntities,
                       updatedEntities,
                       entitiesToDelete));
        }
Ejemplo n.º 8
0
        public void Put(Cid deltaHash, TransactionReceipt[] receipts, PublicEntry[] deltaPublicEntries)
        {
            if (!_repository.TryGet(GetDocumentId(deltaHash), out _))
            {
                _repository.Add(new TransactionReceipts
                {
                    Id       = GetDocumentId(deltaHash),
                    Receipts = receipts
                });

                for (var i = 0; i < receipts.Length; i++)
                {
                    var transactionHash = deltaPublicEntries[i].GetDocumentId(_hashProvider);
                    if (_transactionToDeltaRepository.TryGet(transactionHash, out _))
                    {
                        _transactionToDeltaRepository.Delete(transactionHash);
                    }

                    _transactionToDeltaRepository.Add(new TransactionToDelta {
                        DeltaHash = deltaHash, Id = transactionHash
                    });
                }
            }
        }
        /// <summary>
        /// Benefit: You get to explicitly see which entities are new and which are not.
        /// Downside: You either have code for conversion of single entities in two places, or more ToEntity overloads or a weird way where you seem to retrieving the same entity twice.
        /// </summary>
        public static IList <Entity> ConvertCollection_MaintainListsOfSomeOperations_ExplicitTryGet(
            IList <ViewModel> viewModels,
            IList <Entity> existingEntities,
            IRepository repository)
        {
            var updatedEntities  = new List <Entity>();
            var insertedEntities = new List <Entity>();

            foreach (ViewModel viewModel in viewModels)
            {
                Entity entity = repository.TryGet(viewModel.ID);
                bool   isNew  = entity == null;

                entity = viewModel.ToEntity(repository);

                if (isNew)
                {
                    insertedEntities.Add(entity);
                }
                else
                {
                    updatedEntities.Add(entity);
                }
            }

            // Delete
            IEnumerable <Entity> entitiesToDelete = existingEntities.Except(insertedEntities)
                                                    .Except(updatedEntities);

            foreach (Entity entityToDelete in entitiesToDelete)
            {
                entityToDelete.UnlinkRelatedEntities();
                repository.Delete(entityToDelete);
            }

            return(insertedEntities);
        }
Ejemplo n.º 10
0
 /// <inheritdoc />
 public bool TryReadItem(string id)
 {
     Guard.Argument(id, nameof(id)).NotNull();
     return(_repository.TryGet(id, out _));
 }
 bool WasRewardAlreadyRecorded(Reward reward)
 {
     return(rewardRepository.TryGet(reward.Id) != null);
 }
        public void TryGet_Should_Return_True_And_Item_If_Item_Exists(IRepository<Contact, string> repository)
        {
            var contact = new Contact { Name = "Test User", ContactTypeId = 1 };
            repository.Add(contact);

            Contact result;
            repository.TryGet(contact.ContactId, out result).ShouldBeTrue();
            result.Name.ShouldEqual(contact.Name);
            result.ContactTypeId.ShouldEqual(contact.ContactTypeId);
        }
 public void TryGet_Should_Return_False_And_Null_If_Item_Does_Not_Exists(IRepository<Contact, string> repository)
 {
     Contact result;
     repository.TryGet(string.Empty, out result).ShouldBeFalse();
     result.ShouldBeNull();
 }
Ejemplo n.º 14
0
                public async Task <bool> LoadExisting()
                {
                    _existing = await _repository.TryGet(_command.ID);

                    return(_existing != null);
                }
Ejemplo n.º 15
0
 public static async Task <T> Get <T>(this IRepository <T> repository, ID id) where T : class, IPersistable
 {
     return(await repository.TryGet(id) ??
            throw new ArgumentException("An object with the given ID could not be found.", nameof(id)));
 }
Ejemplo n.º 16
0
 public bool TryGet(TKey key, out T entity)
 {
     return(Repository.TryGet(key, out entity));
 }
Ejemplo n.º 17
0
 public bool TryGetSessionId(string connectionId, out int sessionId) => sessionIdRepository.TryGet(connectionId, out sessionId);