Esempio n. 1
0
        public virtual T Get(string name)
        {
            T   result;
            var sql = $"Select {_tableName}.*, Files.* From {_tableName} " +
                      $"INNER JOIN Files_Objects ON {_tableName}.Id = Files_Objects.ObjectId" +
                      $"INNER JOIN Files ON Files.FileId = Files_Objects.FileId" +
                      $"WHERE Files_Objects.ObjectType = @objectType AND {_tableName}.Name = @name";

            try
            {
                result = _connectionString.Query <T, File, T>(sql,
                                                              (element, file) =>
                {
                    element.Files = element.Files.Append(file);
                    return(element);
                }
                                                              , new { objectType = ObjectTypeProvider.For(typeof(T)), name })
                         .Single();
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(null);
            }

            using (var fileObjRepo = new FileObjectRepository(_transaction, _logger))
            {
                result.Files = fileObjRepo.GetAllFilesForObject(result);
            }

            return(result);
        }
        public void DeleteAllFilesFromObject <T>(int objectId)
        {
            var sql = "Delete From Files_Objects " +
                      "Where ObjectId = @objectId and ObjectType = @objectType";

            var result = _connectionString.Execute(sql, new { objectId, objectType = ObjectTypeProvider.For(typeof(T)) }, _transaction);

            if (result <= 0)
            {
                _logger.LogError($"FileObject not removed: {objectId}, {typeof(T)}");
            }
        }
Esempio n. 3
0
        public virtual IEnumerable <T> GetAll()
        {
            IEnumerable <T> result = Enumerable.Empty <T>();
            var             sql    = $"Select {_tableName}.*, Files.* From {_tableName} " +
                                     $"INNER JOIN Files_Objects ON {_tableName}.Id = Files_Objects.ObjectId" +
                                     $"INNER JOIN Files ON Files.FileId = Files_Objects.FileId" +
                                     $"WHERE Files_Objects.ObjectType = @objectType";

            result = _connectionString.Query <T, File, T>(sql,
                                                          (elemet, file) =>
            {
                elemet.Files = elemet.Files.Append(file);
                return(elemet);
            }
                                                          , new { objectType = ObjectTypeProvider.For(typeof(T)) });
            if (result == null)
            {
                _logger.LogError($"{_tableName} not find");
            }

            return(result);
        }
        public IEnumerable <File> GetAllFilesForObject(BaseEntity item)
        {
            var sql = "Select Files.* From  Files INNER JOIN Files_Objects ON Files_Objects.FileId = Files.FileId " +
                      "Where Files_Objects.ObjectId = @objectId And Files_Objects.ObjectType = @objectType";

            IEnumerable <File> result = _connectionString.Query <File>(sql, new { objectId = item.Id, objectType = (int)ObjectTypeProvider.For(item.GetType()) });

            return(result);
        }
        public void DeleteFileFromObject(BaseEntity item, int fileId)
        {
            var sql = "Delete From Files_Objects " +
                      "Where ObjectId = @objectId and ObjectType = @objectType and FileId = @fileId";

            var result = _connectionString.Execute(sql, new { objectId = item.Id, objectType = ObjectTypeProvider.For(item.GetType()), fileId }, _transaction);

            if (result <= 0)
            {
                _logger.LogError($"FileObject not removed: {item.Id},{fileId} ,{item.GetType()}");
            }
        }
        public void AddFileToObject(BaseEntity item, int fileId)
        {
            var sql = "INSERT INTO Files_Objects(FileId, ObjectId, ObjectType)" +
                      "Values(@fileId, @objectId, @objectType)";

            var result = _connectionString.Execute(sql, new { fileId, objectId = item.Id, objectType = ObjectTypeProvider.For(item.GetType()) }, _transaction);

            if (result <= 0)
            {
                _logger.LogError($"File not added: {item.Name}, {fileId}");
            }
        }