Adds template repository and an abstract file repository U4-967
This commit is contained in:
71
src/Umbraco.Core/Persistence/Repositories/FileRepository.cs
Normal file
71
src/Umbraco.Core/Persistence/Repositories/FileRepository.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using Umbraco.Core.IO;
|
||||||
|
using Umbraco.Core.Models;
|
||||||
|
using Umbraco.Core.Persistence.UnitOfWork;
|
||||||
|
|
||||||
|
namespace Umbraco.Core.Persistence.Repositories
|
||||||
|
{
|
||||||
|
internal abstract class FileRepository<TId, TEntity> : IRepository<TId, TEntity>
|
||||||
|
where TEntity : IFile
|
||||||
|
{
|
||||||
|
private IUnitOfWork _work;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
|
||||||
|
protected FileRepository(IUnitOfWork work, IFileSystem fileSystem)
|
||||||
|
{
|
||||||
|
_work = work;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the Unit of Work added to the repository
|
||||||
|
/// </summary>
|
||||||
|
protected IUnitOfWork UnitOfWork
|
||||||
|
{
|
||||||
|
get { return _work; }
|
||||||
|
}
|
||||||
|
|
||||||
|
protected IFileSystem FileSystem
|
||||||
|
{
|
||||||
|
get { return _fileSystem; }
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Implementation of IRepository<TId,TEntity>
|
||||||
|
|
||||||
|
public virtual void AddOrUpdate(TEntity entity)
|
||||||
|
{
|
||||||
|
var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content));
|
||||||
|
FileSystem.AddFile(entity.Name, stream, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(TEntity entity)
|
||||||
|
{
|
||||||
|
if (_fileSystem.FileExists(entity.Name))
|
||||||
|
{
|
||||||
|
_fileSystem.DeleteFile(entity.Name);
|
||||||
|
}
|
||||||
|
else if(_fileSystem.FileExists(entity.Path))
|
||||||
|
{
|
||||||
|
_fileSystem.DeleteFile(entity.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract TEntity Get(TId id);
|
||||||
|
|
||||||
|
public abstract IEnumerable<TEntity> GetAll(params TId[] ids);
|
||||||
|
|
||||||
|
public bool Exists(TId id)
|
||||||
|
{
|
||||||
|
return _fileSystem.FileExists(id.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetUnitOfWork(IUnitOfWork work)
|
||||||
|
{
|
||||||
|
_work = work;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using Umbraco.Core.Models;
|
||||||
|
|
||||||
|
namespace Umbraco.Core.Persistence.Repositories
|
||||||
|
{
|
||||||
|
public interface ITemplateRepository : IRepository<string, Template>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Umbraco.Core.IO;
|
using Umbraco.Core.IO;
|
||||||
@@ -12,61 +11,35 @@ namespace Umbraco.Core.Persistence.Repositories
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents the Script Repository
|
/// Represents the Script Repository
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class ScriptRepository : IScriptRepository
|
internal class ScriptRepository : FileRepository<string, Script>, IScriptRepository
|
||||||
{
|
{
|
||||||
private IUnitOfWork _work;
|
|
||||||
private readonly IFileSystem _fileSystem;
|
|
||||||
|
|
||||||
public ScriptRepository(IUnitOfWork work)
|
public ScriptRepository(IUnitOfWork work)
|
||||||
|
: base(work, FileSystemProviderManager.Current.GetFileSystemProvider("script"))
|
||||||
{
|
{
|
||||||
_work = work;
|
|
||||||
_fileSystem = FileSystemProviderManager.Current.GetFileSystemProvider("script");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the Unit of Work added to the repository
|
|
||||||
/// </summary>
|
|
||||||
protected IUnitOfWork UnitOfWork
|
|
||||||
{
|
|
||||||
get { return _work; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Implementation of IRepository<string,Script>
|
#region Implementation of IRepository<string,Script>
|
||||||
|
|
||||||
public void AddOrUpdate(Script entity)
|
public override Script Get(string id)
|
||||||
{
|
{
|
||||||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content));
|
if(!FileSystem.FileExists(id))
|
||||||
_fileSystem.AddFile(entity.Name, stream, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Delete(Script entity)
|
|
||||||
{
|
|
||||||
if (_fileSystem.FileExists(entity.Name))
|
|
||||||
{
|
|
||||||
_fileSystem.DeleteFile(entity.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Script Get(string id)
|
|
||||||
{
|
|
||||||
if(!_fileSystem.FileExists(id))
|
|
||||||
{
|
{
|
||||||
throw new Exception(string.Format("The file {0} was not found", id));
|
throw new Exception(string.Format("The file {0} was not found", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
var stream = _fileSystem.OpenFile(id);
|
var stream = FileSystem.OpenFile(id);
|
||||||
byte[] bytes = new byte[stream.Length];
|
byte[] bytes = new byte[stream.Length];
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
stream.Read(bytes, 0, (int)stream.Length);
|
stream.Read(bytes, 0, (int)stream.Length);
|
||||||
var content = Encoding.UTF8.GetString(bytes);
|
var content = Encoding.UTF8.GetString(bytes);
|
||||||
|
|
||||||
var path = _fileSystem.GetRelativePath(id);
|
var path = FileSystem.GetRelativePath(id);
|
||||||
|
|
||||||
var script = new Script(path) {Content = content};
|
var script = new Script(path) {Content = content};
|
||||||
return script;
|
return script;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Script> GetAll(params string[] ids)
|
public override IEnumerable<Script> GetAll(params string[] ids)
|
||||||
{
|
{
|
||||||
if (ids.Any())
|
if (ids.Any())
|
||||||
{
|
{
|
||||||
@@ -77,7 +50,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var files = _fileSystem.GetFiles("", "*");
|
var files = FileSystem.GetFiles("", "*");
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
yield return Get(file);
|
yield return Get(file);
|
||||||
@@ -85,16 +58,6 @@ namespace Umbraco.Core.Persistence.Repositories
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Exists(string id)
|
|
||||||
{
|
|
||||||
return _fileSystem.FileExists(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetUnitOfWork(IUnitOfWork work)
|
|
||||||
{
|
|
||||||
_work = work;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Umbraco.Core.IO;
|
||||||
|
using Umbraco.Core.Models;
|
||||||
|
using Umbraco.Core.Persistence.UnitOfWork;
|
||||||
|
|
||||||
|
namespace Umbraco.Core.Persistence.Repositories
|
||||||
|
{
|
||||||
|
internal class TemplateRepository : FileRepository<string, Template>, ITemplateRepository
|
||||||
|
{
|
||||||
|
public TemplateRepository(IUnitOfWork work)
|
||||||
|
: base(work, FileSystemProviderManager.Current.GetFileSystemProvider("template"))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Overrides of FileRepository<string,Template>
|
||||||
|
|
||||||
|
public override Template Get(string id)
|
||||||
|
{
|
||||||
|
if (!FileSystem.FileExists(id))
|
||||||
|
{
|
||||||
|
throw new Exception(string.Format("The file {0} was not found", id));
|
||||||
|
}
|
||||||
|
|
||||||
|
var stream = FileSystem.OpenFile(id);
|
||||||
|
byte[] bytes = new byte[stream.Length];
|
||||||
|
stream.Position = 0;
|
||||||
|
stream.Read(bytes, 0, (int)stream.Length);
|
||||||
|
var content = Encoding.UTF8.GetString(bytes);
|
||||||
|
|
||||||
|
var path = FileSystem.GetRelativePath(id);
|
||||||
|
|
||||||
|
var template = new Template(path) { Content = content };
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<Template> GetAll(params string[] ids)
|
||||||
|
{
|
||||||
|
if (ids.Any())
|
||||||
|
{
|
||||||
|
foreach (var id in ids)
|
||||||
|
{
|
||||||
|
yield return Get(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var files = FileSystem.GetFiles("", "*");
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
yield return Get(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -121,6 +121,7 @@
|
|||||||
<Compile Include="Persistence\Repositories\ContentTypeRepository.cs" />
|
<Compile Include="Persistence\Repositories\ContentTypeRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\DataTypeDefinitionRepository.cs" />
|
<Compile Include="Persistence\Repositories\DataTypeDefinitionRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\DictionaryRepository.cs" />
|
<Compile Include="Persistence\Repositories\DictionaryRepository.cs" />
|
||||||
|
<Compile Include="Persistence\Repositories\FileRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\IContentRepository.cs" />
|
<Compile Include="Persistence\Repositories\IContentRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\IContentTypeRepository.cs" />
|
<Compile Include="Persistence\Repositories\IContentTypeRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\IDataTypeDefinitionRepository.cs" />
|
<Compile Include="Persistence\Repositories\IDataTypeDefinitionRepository.cs" />
|
||||||
@@ -133,6 +134,7 @@
|
|||||||
<Compile Include="Persistence\Relators\TabPropertyTypeRelator.cs" />
|
<Compile Include="Persistence\Relators\TabPropertyTypeRelator.cs" />
|
||||||
<Compile Include="Persistence\Repositories\IScriptRepository.cs" />
|
<Compile Include="Persistence\Repositories\IScriptRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\IRepository.cs" />
|
<Compile Include="Persistence\Repositories\IRepository.cs" />
|
||||||
|
<Compile Include="Persistence\Repositories\ITemplateRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\LanguageRepository.cs" />
|
<Compile Include="Persistence\Repositories\LanguageRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\MacroRepository.cs" />
|
<Compile Include="Persistence\Repositories\MacroRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\PetaPocoRepositoryBase.cs" />
|
<Compile Include="Persistence\Repositories\PetaPocoRepositoryBase.cs" />
|
||||||
@@ -140,6 +142,7 @@
|
|||||||
<Compile Include="Persistence\Repositories\RelationTypeRepository.cs" />
|
<Compile Include="Persistence\Repositories\RelationTypeRepository.cs" />
|
||||||
<Compile Include="Persistence\Repositories\RepositoryBase.cs" />
|
<Compile Include="Persistence\Repositories\RepositoryBase.cs" />
|
||||||
<Compile Include="Persistence\Repositories\ScriptRepository.cs" />
|
<Compile Include="Persistence\Repositories\ScriptRepository.cs" />
|
||||||
|
<Compile Include="Persistence\Repositories\TemplateRepository.cs" />
|
||||||
<Compile Include="Persistence\RepositoryResolver.cs" />
|
<Compile Include="Persistence\RepositoryResolver.cs" />
|
||||||
<Compile Include="Persistence\TransactionType.cs" />
|
<Compile Include="Persistence\TransactionType.cs" />
|
||||||
<Compile Include="Persistence\UnitOfWork\FileUnitOfWork.cs" />
|
<Compile Include="Persistence\UnitOfWork\FileUnitOfWork.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user