Ejemplo n.º 1
0
        public IEnumerable <TEntity> Get(Where where = null)
        {
            var func = new Func <LiteDatabase, dynamic>((db) =>
            {
                logger.LogInformation($"Processing 'Get' request");

                IEnumerable <TEntity> results = null;
                var dbCollection = db.GetCollection <TEntity>();

                if (where == null)
                {
                    results = dbCollection.FindAll().ToList();
                }
                else
                {
                    results = dbCollection.Find(QueryExtension.EQ(where)).ToList();
                }

                logger.LogInformation($"Returning {results?.Count()} {typeof(TEntity).Name}");
                return(results);
            });

            var result = dbLite.DoDatabaseAction(func);

            return(result);
        }
Ejemplo n.º 2
0
        public void Upsert(TEntity entity, Where where = null)
        {
            var action = new Func <LiteDatabase, dynamic>((db) =>
            {
                logger.LogInformation($"Processing 'Upsert' request");

                var dbCollection = db.GetCollection <TEntity>();

                if (where != null)
                {
                    var existingEntities = dbCollection.Find(QueryExtension.EQ(where));

                    if (existingEntities.Count() == 1)
                    {
                        entity.Id = existingEntities.First().Id;
                    }
                }

                bool inserted = dbCollection.Upsert(entity);

                logger.LogInformation($"{(inserted ? "Inserted" : "Updated")} {typeof(TEntity).Name} with Id {entity.Id}");
                return(0);
            });

            dbLite.DoDatabaseAction(action);
        }
Ejemplo n.º 3
0
        public void Delete(Where where)
        {
            var action = new Func <LiteDatabase, dynamic>((db) =>
            {
                logger.LogInformation($"Processing 'Delete' request");

                var dbCollection = db.GetCollection <TEntity>();
                var deleteCount  = 0;

                if (where != null)
                {
                    deleteCount = dbCollection.Delete(QueryExtension.EQ(where));
                }

                logger.LogInformation($"Deleted {deleteCount} {typeof(TEntity).Name}");
                return(0);
            });

            dbLite.DoDatabaseAction(action);
        }