V14: Add optional key to template create model (#15471)
* Implement Id for request model * Make key optional * Add test for use case --------- Co-authored-by: Elitsa <elm@umbraco.dk>
This commit is contained in:
@@ -35,7 +35,8 @@ public class CreateTemplateController : TemplateControllerBase
|
||||
requestModel.Name,
|
||||
requestModel.Alias,
|
||||
requestModel.Content,
|
||||
CurrentUserKey(_backOfficeSecurityAccessor));
|
||||
CurrentUserKey(_backOfficeSecurityAccessor),
|
||||
requestModel.Key);
|
||||
|
||||
return result.Success
|
||||
? CreatedAtAction<ByKeyTemplateController>(controller => nameof(controller.ByKey), result.Result.Key)
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
|
||||
public class CreateTemplateRequestModel : TemplateModelBase
|
||||
{
|
||||
public Guid? Key { get; set; }
|
||||
}
|
||||
|
||||
@@ -83,12 +83,13 @@ public interface ITemplateService : IService
|
||||
/// <summary>
|
||||
/// Creates a new template
|
||||
/// </summary>
|
||||
/// <param name="templateKey"></param>
|
||||
/// <param name="name">Name of the new template</param>
|
||||
/// <param name="alias">Alias of the template</param>
|
||||
/// <param name="content">View content for the new template</param>
|
||||
/// <param name="userKey">Key of the user performing the Create.</param>
|
||||
/// <returns></returns>
|
||||
Task<Attempt<ITemplate, TemplateOperationStatus>> CreateAsync(string name, string alias, string? content, Guid userKey);
|
||||
Task<Attempt<ITemplate, TemplateOperationStatus>> CreateAsync(string name, string alias, string? content, Guid userKey, Guid? templateKey = null);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new template
|
||||
|
||||
@@ -94,8 +94,9 @@ public class TemplateService : RepositoryService, ITemplateService
|
||||
string name,
|
||||
string alias,
|
||||
string? content,
|
||||
Guid userKey)
|
||||
=> await CreateAsync(new Template(_shortStringHelper, name, alias) { Content = content }, userKey);
|
||||
Guid userKey,
|
||||
Guid? templateKey = null)
|
||||
=> await CreateAsync(new Template(_shortStringHelper, name, alias) { Content = content, Key = templateKey ?? Guid.NewGuid() }, userKey);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<Attempt<ITemplate, TemplateOperationStatus>> CreateAsync(ITemplate template, Guid userKey)
|
||||
|
||||
@@ -276,4 +276,21 @@ public class TemplateServiceTests : UmbracoIntegrationTest
|
||||
Assert.IsFalse(result.Success);
|
||||
Assert.AreEqual(TemplateOperationStatus.InvalidAlias, result.Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Can_Create_Template_With_Key()
|
||||
{
|
||||
var key = Guid.NewGuid();
|
||||
var result = await TemplateService.CreateAsync("Template", "template", "test", Constants.Security.SuperUserKey, key);
|
||||
Assert.IsTrue(result.Success);
|
||||
|
||||
var template = await TemplateService.GetAsync(key);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.IsNotNull(template);
|
||||
Assert.AreEqual(key, template.Key);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user