示例#1
0
        /// <summary>
        /// UpdateMany with filter
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="filter"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public async Task <Result <TEntity> > UpdateMany <TEntity>(FilterDefinition <TEntity> filter, UpdateDefinition <TEntity> update) where TEntity : class, new()
        {
            var result = new Result <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var updateRes  = await collection.UpdateManyAsync(filter, update);

                if (updateRes.ModifiedCount < 1)
                {
                    var ex = new Exception();
                    result.Message = HelperService.NotifyException("UpdateMany", "ERROR: updateRes.ModifiedCount < 1 for entities: " + typeof(TEntity).Name + "s", ex);
                    return(result);
                }
                result.Success = true;
                result.Message = "OK";
                return(result);
            }
            catch (Exception ex)
            {
                result.Message = HelperService.NotifyException("UpdateMany", "Exception updating entities: " + typeof(TEntity).Name + "s", ex);
                return(result);
            }
        }
示例#2
0
        /// <summary>
        /// A generic delete many method
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task <Result <TEntity> > DeleteMany <TEntity>(FilterDefinition <TEntity> filter) where TEntity : class, new()
        {
            var result = new Result <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var deleteRes  = await collection.DeleteManyAsync(filter);

                if (deleteRes.DeletedCount < 1)
                {
                    var ex = new Exception();
                    result.Message = HelperService.NotifyException("DeleteMany", "Some " + typeof(TEntity).Name + "s could not be deleted.", ex);
                    return(result);
                }
                result.Success = true;
                result.Message = "OK";
                return(result);
            }
            catch (Exception ex)
            {
                result.Message = HelperService.NotifyException("DeleteMany", "Some " + typeof(TEntity).Name + "s could not be deleted.", ex);
                return(result);
            }
        }
示例#3
0
        /// <summary>
        /// A generic get many method
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task <GetManyResult <TEntity> > GetMany <TEntity>(IEnumerable <string> ids) where TEntity : class, new()
        {
            try
            {
                var collection = GetCollection <TEntity>();
                var filter     = Builders <TEntity> .Filter.Eq("Id", ids);

                return(await GetManyAsync <TEntity>(filter));
            }
            catch (Exception ex)
            {
                var res = new GetManyResult <TEntity>();
                res.Message = HelperService.NotifyException("GetMany", "Exception getting many " + typeof(TEntity).Name + "s", ex);
                return(res);
            }
        }
示例#4
0
        /// <summary>
        /// GetAndUpdateOne with filter
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="filter"></param>
        /// <param name="update"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task <GetOneResult <TEntity> > GetAndUpdateOne <TEntity>(FilterDefinition <TEntity> filter, UpdateDefinition <TEntity> update, FindOneAndUpdateOptions <TEntity, TEntity> options) where TEntity : class, new()
        {
            var result = new GetOneResult <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                result.Entity = await collection.FindOneAndUpdateAsync(filter, update, options);

                result.Success = true;
                result.Message = "OK";
                return(result);
            }
            catch (Exception ex)
            {
                result.Message = HelperService.NotifyException("GetAndUpdateOne", "Exception getting and updating entity: " + typeof(TEntity).Name, ex);
                return(result);
            }
        }
示例#5
0
        /// <summary>
        /// A generic delete one method
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <Result <TEntity> > DeleteOne <TEntity>(FilterDefinition <TEntity> filter) where TEntity : class, new()
        {
            var result = new Result <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var deleteRes  = await collection.DeleteOneAsync(filter);

                result.Success = true;
                result.Message = "OK";
                return(result);
            }
            catch (Exception ex)
            {
                result.Message = HelperService.NotifyException("DeleteOne", "Exception deleting one " + typeof(TEntity).Name, ex);
                return(result);
            }
        }
示例#6
0
        public Result <TEntity> AddOne <TEntity>(TEntity item) where TEntity : class, new()
        {
            var res = new Result <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                collection.InsertOne(item);
                res.Entity  = item;
                res.Success = true;
                res.Message = "OK";
                return(res);
            }
            catch (Exception ex)
            {
                res.Message = HelperService.NotifyException("AddOne", "Exception adding one " + typeof(TEntity).Name, ex);
                return(res);
            }
        }
示例#7
0
        /// <summary>
        /// A generic get all method
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <returns></returns>
        public GetManyResult <TEntity> GetAll <TEntity>() where TEntity : class, new()
        {
            var res = new GetManyResult <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var entities   = collection.Find(new BsonDocument()).ToList();
                if (entities != null)
                {
                    res.Entities = entities;
                }
                res.Success = true;
                return(res);
            }
            catch (Exception ex)
            {
                res.Message = HelperService.NotifyException("GetAll", "Exception getting all " + typeof(TEntity).Name + "s", ex);
                return(res);
            }
        }
示例#8
0
        /// <summary>
        /// A generic get many method with filter
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task <GetManyResult <TEntity> > GetManyAsync <TEntity>(FilterDefinition <TEntity> filter) where TEntity : class, new()
        {
            var res = new GetManyResult <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var entities   = await collection.Find(filter).ToListAsync();

                if (entities != null)
                {
                    res.Entities = entities;
                }
                res.Success = true;
                return(res);
            }
            catch (Exception ex)
            {
                res.Message = HelperService.NotifyException("GetMany", "Exception getting many " + typeof(TEntity).Name + "s", ex);
                return(res);
            }
        }
示例#9
0
        public GetOneResult <TEntity> GetOne <TEntity>(FilterDefinition <TEntity> filter) where TEntity : class, new()
        {
            var res = new GetOneResult <TEntity>();

            try
            {
                var collection = GetCollection <TEntity>();
                var entity     = collection.Find(filter).FirstOrDefault();

                if (entity != null)
                {
                    res.Entity = entity;
                }

                res.Success = true;
                return(res);
            }
            catch (Exception ex)
            {
                res.Message = HelperService.NotifyException("GetOne", "Exception getting one " + typeof(TEntity).Name, ex);
                return(res);
            }
        }