U4-8361 - implement repository

This commit is contained in:
Stephan
2016-06-12 15:27:54 +02:00
parent a7759c6175
commit 79bbd6a70c
9 changed files with 304 additions and 76 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using System.Runtime.Serialization;
using Umbraco.Core.Models.EntityBase;
@@ -13,10 +14,48 @@ namespace Umbraco.Core.Models
CreateDateUtc = DateTime.UtcNow;
}
public int ContentId { get; set; }
private static readonly PropertyInfo ContentIdSelector = ExpressionHelper.GetPropertyInfo<RedirectUrl, int>(x => x.ContentId);
private static readonly PropertyInfo CreateDateUtcSelector = ExpressionHelper.GetPropertyInfo<RedirectUrl, DateTime>(x => x.CreateDateUtc);
private static readonly PropertyInfo UrlSelector = ExpressionHelper.GetPropertyInfo<RedirectUrl, string>(x => x.Url);
public DateTime CreateDateUtc { get; set; }
private int _contentId;
private DateTime _createDateUtc;
private string _url;
public string Url { get; set; }
public int ContentId
{
get { return _contentId; }
set
{
SetPropertyValueAndDetectChanges(o =>
{
return _contentId = value;
}, _contentId, ContentIdSelector);
}
}
public DateTime CreateDateUtc
{
get { return _createDateUtc; }
set
{
SetPropertyValueAndDetectChanges(o =>
{
return _createDateUtc = value;
}, _createDateUtc, CreateDateUtcSelector);
}
}
public string Url
{
get { return _url; }
set
{
SetPropertyValueAndDetectChanges(o =>
{
return _url = value;
}, _url, UrlSelector);
}
}
}
}

View File

@@ -7,7 +7,14 @@ using Umbraco.Core.Models;
namespace Umbraco.Core.Persistence.Repositories
{
interface IRedirectUrlRepository : IRepositoryQueryable<int, IRedirectUrl>
public interface IRedirectUrlRepository : IRepositoryQueryable<int, IRedirectUrl>
{
void Delete(int id);
void DeleteAll();
void DeleteContentUrls(int contentId);
IRedirectUrl GetMostRecentRule(string url);
IEnumerable<RedirectUrl> GetContentUrls(int contentId);
IEnumerable<RedirectUrl> GetAllUrls(long pageIndex, int pageSize, out long total);
IEnumerable<RedirectUrl> GetAllUrls(int rootContentId, long pageIndex, int pageSize, out long total);
}
}

View File

@@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Persistence.Repositories
{
class RedirectUrlRepository : PetaPocoRepositoryBase<int, IRedirectUrl>, IRedirectUrlRepository
{
public RedirectUrlRepository(IDatabaseUnitOfWork work, CacheHelper cache, ILogger logger, ISqlSyntaxProvider sqlSyntax)
: base(work, cache, logger, sqlSyntax)
{ }
protected override int PerformCount(IQuery<IRedirectUrl> query)
{
throw new NotSupportedException("This repository does not support this method.");
}
protected override bool PerformExists(int id)
{
return PerformGet(id) != null;
}
protected override IRedirectUrl PerformGet(int id)
{
var sql = GetBaseQuery(false).Where<RedirectUrlDto>(x => x.Id == id);
var dto = Database.Fetch<RedirectUrlDto>(sql).FirstOrDefault();
return dto == null ? null : Map(dto);
}
protected override IEnumerable<IRedirectUrl> PerformGetAll(params int[] ids)
{
if (ids.Length > 2000)
throw new NotSupportedException("This repository does not support more than 2000 ids.");
var sql = GetBaseQuery(false).WhereIn<RedirectUrlDto>(x => x.Id, ids);
var dtos = Database.Fetch<RedirectUrlDto>(sql);
return dtos.WhereNotNull().Select(Map);
}
protected override IEnumerable<IRedirectUrl> PerformGetByQuery(IQuery<IRedirectUrl> query)
{
throw new NotSupportedException("This repository does not support this method.");
}
protected override Sql GetBaseQuery(bool isCount)
{
var sql = new Sql();
sql.Select(isCount ? "COUNT(*)" : "*").From<RedirectUrlDto>(SqlSyntax);
return sql;
}
protected override string GetBaseWhereClause()
{
return "id = @Id";
}
protected override IEnumerable<string> GetDeleteClauses()
{
var list = new List<string>
{
"DELETE FROM umbracoRedirectUrl WHERE id = @Id"
};
return list;
}
protected override Guid NodeObjectTypeId
{
get { throw new NotImplementedException(); }
}
protected override void PersistNewItem(IRedirectUrl entity)
{
var dto = Map(entity);
Database.Insert(dto);
entity.Id = dto.Id;
}
protected override void PersistUpdatedItem(IRedirectUrl entity)
{
var dto = Map(entity);
Database.Update(dto);
}
private static RedirectUrlDto Map(IRedirectUrl redirectUrl)
{
if (redirectUrl == null) return null;
return new RedirectUrlDto
{
Id = redirectUrl.Id,
ContentId = redirectUrl.ContentId,
CreateDateUtc = redirectUrl.CreateDateUtc,
Url = redirectUrl.Url
};
}
private static IRedirectUrl Map(RedirectUrlDto dto)
{
if (dto == null) return null;
return new RedirectUrl
{
Id = dto.Id,
ContentId = dto.ContentId,
CreateDateUtc = dto.CreateDateUtc,
Url = dto.Url
};
}
public void DeleteAll()
{
Database.Execute("DELETE FROM umbracoContentUrl");
}
public void DeleteContentUrls(int contentId)
{
Database.Execute("DELETE FROM umbracoContentUrl WHERE contentId=@contentId", new { contentId });
}
public void Delete(int id)
{
Database.Delete<RedirectUrlDto>(id);
}
public IRedirectUrl GetMostRecentRule(string url)
{
var dtos = Database.Fetch<RedirectUrlDto>("SELECT * FROM umbracoContentUrlRule WHERE url=@url ORDER BY createDateUtc DESC;",
new { url });
var dto = dtos.FirstOrDefault();
return dto == null ? null : Map(dto);
}
public IEnumerable<RedirectUrl> GetContentUrls(int contentId)
{
var dtos = Database.Fetch<RedirectUrlDto>("SELECT * FROM umbracoContentUrlRule WHERE contentId=@id ORDER BY createDateUtc DESC;",
new { id = contentId });
return dtos.Select(x => new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
});
}
public IEnumerable<RedirectUrl> GetAllUrls(long pageIndex, int pageSize, out long total)
{
var sql = GetBaseQuery(false).OrderByDescending<RedirectUrlDto>(x => x.CreateDateUtc, SqlSyntax);
var result = Database.Page<RedirectUrlDto>(pageIndex + 1, pageSize, sql);
total = Convert.ToInt32(result.TotalItems);
var rules = result.Items.Select(x => new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
});
return rules;
}
public IEnumerable<RedirectUrl> GetAllUrls(int rootContentId, long pageIndex, int pageSize, out long total)
{
var sql = GetBaseQuery(false)
.InnerJoin<NodeDto>(SqlSyntax).On<NodeDto, RedirectUrlDto>(SqlSyntax, left => left.NodeId, right => right.ContentId)
.Where("umbracoNode.path LIKE @path", new { path = "%," + rootContentId + ",%" })
.OrderByDescending<RedirectUrlDto>(x => x.CreateDateUtc, SqlSyntax);
var result = Database.Page<RedirectUrlDto>(pageIndex + 1, pageSize, sql);
total = Convert.ToInt32(result.TotalItems);
var rules = result.Items.Select(x => new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
});
return rules;
}
}
}

View File

@@ -25,7 +25,7 @@ namespace Umbraco.Core.Persistence.Repositories
protected override int PerformCount(IQuery<IServerRegistration> query)
{
throw new NotSupportedException("This repository does not support this method");
throw new NotSupportedException("This repository does not support this method.");
}
protected override bool PerformExists(int id)
@@ -55,7 +55,7 @@ namespace Umbraco.Core.Persistence.Repositories
protected override IEnumerable<IServerRegistration> PerformGetByQuery(IQuery<IServerRegistration> query)
{
throw new NotSupportedException("This repository does not support this method");
throw new NotSupportedException("This repository does not support this method.");
}
protected override Sql GetBaseQuery(bool isCount)

View File

@@ -333,5 +333,14 @@ namespace Umbraco.Core.Persistence
_logger, _sqlSyntax,
containerObjectType);
}
public IRedirectUrlRepository CreateRedirectUrlRepository(IDatabaseUnitOfWork uow)
{
return new RedirectUrlRepository(
uow,
_cacheHelper,
_logger,
_sqlSyntax);
}
}
}

View File

@@ -5,20 +5,22 @@ namespace Umbraco.Core.Services
{
public interface IRedirectUrlService : IService
{
void Save(RedirectUrl redirectUrl);
void Save(IRedirectUrl redirectUrl);
void DeleteContentUrls(int contentId);
void Delete(IRedirectUrl redirectUrl);
void Delete(int id);
void DeleteContentRules(int contentId);
void DeleteAll();
RedirectUrl GetMostRecentRule(string url);
IRedirectUrl GetMostRecentRule(string url);
IEnumerable<RedirectUrl> GetRules(int contentId);
IEnumerable<IRedirectUrl> GetContentUrls(int contentId);
IEnumerable<RedirectUrl> GetAllRules(long pageIndex, int pageSize, out long total);
IEnumerable<IRedirectUrl> GetAllUrls(long pageIndex, int pageSize, out long total);
IEnumerable<RedirectUrl> GetAllRules(int rootContentId, long pageIndex, int pageSize, out long total);
IEnumerable<IRedirectUrl> GetAllUrls(int rootContentId, long pageIndex, int pageSize, out long total);
}
}

View File

@@ -15,41 +15,46 @@ namespace Umbraco.Core.Services
: base(provider, repositoryFactory, logger, eventMessagesFactory)
{ }
public void Save(RedirectUrl redirectUrl)
public void Save(IRedirectUrl redirectUrl)
{
// check if the url already exists
// the url actually is a primary key?
// though we might want to keep the history?
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
var dto = new RedirectUrlDto
{
Id = redirectUrl.Id,
ContentId = redirectUrl.ContentId,
CreateDateUtc = redirectUrl.CreateDateUtc,
Url = redirectUrl.Url
};
uow.Database.InsertOrUpdate(dto);
repo.AddOrUpdate(redirectUrl);
uow.Commit();
}
}
public void Delete(IRedirectUrl redirectUrl)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
repo.Delete(redirectUrl);
uow.Commit();
redirectUrl.Id = dto.Id;
}
}
public void Delete(int id)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
uow.Database.Execute("DELETE FROM umbracoContentUrlRule WHERE id=@id", new { id = id });
repo.Delete(id);
uow.Commit();
}
}
public void DeleteContentRules(int contentId)
public void DeleteContentUrls(int contentId)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
uow.Database.Execute("DELETE FROM umbracoContentUrlRule WHERE contenId=@id", new { id = contentId });
repo.DeleteContentUrls(contentId);
uow.Commit();
}
}
@@ -57,85 +62,52 @@ namespace Umbraco.Core.Services
public void DeleteAll()
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
uow.Database.Execute("DELETE FROM umbracoContentUrlRule;");
repo.DeleteAll();
uow.Commit();
}
}
public RedirectUrl GetMostRecentRule(string url)
public IRedirectUrl GetMostRecentRule(string url)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
var ruleDtos = uow.Database.Fetch<RedirectUrlDto>("SELECT * FROM umbracoContentUrlRule WHERE url=@url ORDER BY createDateUtc DESC;",
new { url });
var ruleDto = ruleDtos.FirstOrDefault();
var rule = ruleDto == null ? null : new RedirectUrl
{
Id = ruleDto.Id,
ContentId = ruleDto.ContentId,
CreateDateUtc = ruleDto.CreateDateUtc,
Url = ruleDto.Url
};
var rule = repo.GetMostRecentRule(url);
uow.Commit();
return rule;
}
}
public IEnumerable<RedirectUrl> GetRules(int contentId)
public IEnumerable<IRedirectUrl> GetContentUrls(int contentId)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
var ruleDtos = uow.Database.Fetch<RedirectUrlDto>("SELECT * FROM umbracoContentUrlRule WHERE contentId=@id ORDER BY createDateUtc DESC;",
new { id = contentId });
var rules = ruleDtos.Select(x=> new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
});
var rules = repo.GetContentUrls(contentId);
uow.Commit();
return rules;
}
}
public IEnumerable<RedirectUrl> GetAllRules(long pageIndex, int pageSize, out long total)
public IEnumerable<IRedirectUrl> GetAllUrls(long pageIndex, int pageSize, out long total)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
var ruleDtos = uow.Database.Fetch<RedirectUrlDto>("SELECT * FROM umbracoContentUrlRule ORDER BY createDateUtc DESC;");
var rules = ruleDtos.Select(x => new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
}).ToArray();
total = rules.Length;
var rules = repo.GetAllUrls(pageIndex, pageSize, out total);
uow.Commit();
return rules;
}
}
public IEnumerable<RedirectUrl> GetAllRules(int rootContentId, long pageIndex, int pageSize, out long total)
public IEnumerable<IRedirectUrl> GetAllUrls(int rootContentId, long pageIndex, int pageSize, out long total)
{
using (var uow = UowProvider.GetUnitOfWork())
using (var repo = RepositoryFactory.CreateRedirectUrlRepository(uow))
{
var path = "%," + rootContentId + ",%";
var ruleDtos = uow.Database.Fetch<RedirectUrlDto>(@"SELECT * FROM umbracoContentUrlRule
JOIN umbracoNode ON umbracoNode.id=umbracoContentUrlRule.contentId
WHERE umbracoNode.path LIKE @path
ORDER BY createDateUtc DESC;", new { path });
var rules = ruleDtos.Select(x => new RedirectUrl
{
Id = x.Id,
ContentId = x.ContentId,
CreateDateUtc = x.CreateDateUtc,
Url = x.Url
}).ToArray();
total = rules.Length;
var rules = repo.GetAllUrls(rootContentId, pageIndex, pageSize, out total);
uow.Commit();
return rules;
}

View File

@@ -1,5 +1,4 @@
using System;
using log4net;
using Umbraco.Core.Logging;
using System.IO;
using System.Linq;
@@ -7,7 +6,6 @@ using Umbraco.Core.IO;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using umbraco.interfaces;
using Umbraco.Core.Events;
namespace Umbraco.Core.Services
@@ -64,6 +62,7 @@ namespace Umbraco.Core.Services
private Lazy<IMemberGroupService> _memberGroupService;
private Lazy<INotificationService> _notificationService;
private Lazy<IExternalLoginService> _externalLoginService;
private Lazy<IRedirectUrlService> _redirectUrlService;
/// <summary>
/// public ctor - will generally just be used for unit testing all items are optional and if not specified, the defaults will be used
@@ -93,6 +92,7 @@ namespace Umbraco.Core.Services
/// <param name="publicAccessService"></param>
/// <param name="externalLoginService"></param>
/// <param name="migrationEntryService"></param>
/// <param name="redirectUrlService"></param>
public ServiceContext(
IContentService contentService = null,
IMediaService mediaService = null,
@@ -118,7 +118,8 @@ namespace Umbraco.Core.Services
IMacroService macroService = null,
IPublicAccessService publicAccessService = null,
IExternalLoginService externalLoginService = null,
IMigrationEntryService migrationEntryService = null)
IMigrationEntryService migrationEntryService = null,
IRedirectUrlService redirectUrlService = null)
{
if (migrationEntryService != null) _migrationEntryService = new Lazy<IMigrationEntryService>(() => migrationEntryService);
if (externalLoginService != null) _externalLoginService = new Lazy<IExternalLoginService>(() => externalLoginService);
@@ -145,6 +146,7 @@ namespace Umbraco.Core.Services
if (taskService != null) _taskService = new Lazy<ITaskService>(() => taskService);
if (macroService != null) _macroService = new Lazy<IMacroService>(() => macroService);
if (publicAccessService != null) _publicAccessService = new Lazy<IPublicAccessService>(() => publicAccessService);
if (redirectUrlService != null) _redirectUrlService = new Lazy<IRedirectUrlService>(() => redirectUrlService);
}
/// <summary>
@@ -310,6 +312,8 @@ namespace Umbraco.Core.Services
if (_memberGroupService == null)
_memberGroupService = new Lazy<IMemberGroupService>(() => new MemberGroupService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_redirectUrlService == null)
_redirectUrlService = new Lazy<IRedirectUrlService>(() => new RedirectUrlService(provider, repositoryFactory, logger, eventMessagesFactory));
}
/// <summary>
@@ -516,5 +520,13 @@ namespace Umbraco.Core.Services
{
get { return _externalLoginService.Value; }
}
/// <summary>
/// Gets the RedirectUrlService.
/// </summary>
public IRedirectUrlService RedirectUrlService
{
get { return _redirectUrlService.Value; }
}
}
}

View File

@@ -479,6 +479,7 @@
<Compile Include="Persistence\Repositories\Interfaces\ITaskTypeRepository.cs" />
<Compile Include="Persistence\Repositories\MigrationEntryRepository.cs" />
<Compile Include="Persistence\Repositories\PublicAccessRepository.cs" />
<Compile Include="Persistence\Repositories\RedirectUrlRepository.cs" />
<Compile Include="Persistence\Repositories\TaskRepository.cs" />
<Compile Include="Persistence\Repositories\TaskTypeRepository.cs" />
<Compile Include="PropertyEditors\DecimalValidator.cs" />