-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve document schedule #17535
base: v15/dev
Are you sure you want to change the base?
Improve document schedule #17535
Conversation
# Conflicts: # src/Umbraco.Cms.Api.Management/Factories/DocumentPresentationFactory.cs # src/Umbraco.Cms.Api.Management/Factories/IDocumentPresentationFactory.cs
@@ -53,7 +71,9 @@ public async Task<IActionResult> ByKey(CancellationToken cancellationToken, Guid | |||
return DocumentNotFound(); | |||
} | |||
|
|||
DocumentResponseModel model = await _documentPresentationFactory.CreateResponseModelAsync(content); | |||
ContentScheduleCollection schedule = _contentService.GetContentScheduleByContentId(content.Id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not optimal to call two services from the controller. I think we should make it a single service call and move the logic to the service layer. Furthermore, when using two services, we should wrap it in a single scope, to avoid funny things that can happen outside of a transaction
async Task<Attempt<ContentPublishingResult, ContentPublishingOperationStatus>> PublishAsync( | ||
Guid key, | ||
ICollection<CulturePublishScheduleModel> culturesToPublishOrSchedule, | ||
Guid userKey) | ||
{ | ||
// todo remove default implementation when superseded method is removed in v17+ | ||
var culturesToPublishImmediately = | ||
culturesToPublishOrSchedule.Where(culture => culture.Schedule is null).Select(c => c.Culture ?? Constants.System.InvariantCulture).ToHashSet(); | ||
|
||
ContentScheduleCollection schedules = StaticServiceProvider.Instance.GetRequiredService<IContentService>().GetContentScheduleByContentId(key); | ||
|
||
foreach (CulturePublishScheduleModel cultureToSchedule in culturesToPublishOrSchedule.Where(c => c.Schedule is not null)) | ||
{ | ||
var culture = cultureToSchedule.Culture ?? Constants.System.InvariantCulture; | ||
|
||
if (cultureToSchedule.Schedule!.PublishDate is null) | ||
{ | ||
schedules.RemoveIfExists(culture, ContentScheduleAction.Release); | ||
} | ||
else | ||
{ | ||
schedules.AddOrUpdate(culture, cultureToSchedule.Schedule!.PublishDate.Value.UtcDateTime,ContentScheduleAction.Release); | ||
} | ||
|
||
if (cultureToSchedule.Schedule!.UnpublishDate is null) | ||
{ | ||
schedules.RemoveIfExists(culture, ContentScheduleAction.Expire); | ||
} | ||
else | ||
{ | ||
schedules.AddOrUpdate(culture, cultureToSchedule.Schedule!.UnpublishDate.Value.UtcDateTime, ContentScheduleAction.Expire); | ||
} | ||
} | ||
|
||
var cultureAndSchedule = new CultureAndScheduleModel | ||
{ | ||
CulturesToPublishImmediately = culturesToPublishImmediately, | ||
Schedules = schedules, | ||
}; | ||
|
||
#pragma warning disable CS0618 // Type or member is obsolete | ||
return await PublishAsync(key, cultureAndSchedule, userKey); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to forward to the actual implementation?
async Task<Attempt<ContentPublishingResult, ContentPublishingOperationStatus>> PublishAsync( | |
Guid key, | |
ICollection<CulturePublishScheduleModel> culturesToPublishOrSchedule, | |
Guid userKey) | |
{ | |
// todo remove default implementation when superseded method is removed in v17+ | |
var culturesToPublishImmediately = | |
culturesToPublishOrSchedule.Where(culture => culture.Schedule is null).Select(c => c.Culture ?? Constants.System.InvariantCulture).ToHashSet(); | |
ContentScheduleCollection schedules = StaticServiceProvider.Instance.GetRequiredService<IContentService>().GetContentScheduleByContentId(key); | |
foreach (CulturePublishScheduleModel cultureToSchedule in culturesToPublishOrSchedule.Where(c => c.Schedule is not null)) | |
{ | |
var culture = cultureToSchedule.Culture ?? Constants.System.InvariantCulture; | |
if (cultureToSchedule.Schedule!.PublishDate is null) | |
{ | |
schedules.RemoveIfExists(culture, ContentScheduleAction.Release); | |
} | |
else | |
{ | |
schedules.AddOrUpdate(culture, cultureToSchedule.Schedule!.PublishDate.Value.UtcDateTime,ContentScheduleAction.Release); | |
} | |
if (cultureToSchedule.Schedule!.UnpublishDate is null) | |
{ | |
schedules.RemoveIfExists(culture, ContentScheduleAction.Expire); | |
} | |
else | |
{ | |
schedules.AddOrUpdate(culture, cultureToSchedule.Schedule!.UnpublishDate.Value.UtcDateTime, ContentScheduleAction.Expire); | |
} | |
} | |
var cultureAndSchedule = new CultureAndScheduleModel | |
{ | |
CulturesToPublishImmediately = culturesToPublishImmediately, | |
Schedules = schedules, | |
}; | |
#pragma warning disable CS0618 // Type or member is obsolete | |
return await PublishAsync(key, cultureAndSchedule, userKey); | |
#pragma warning restore CS0618 // Type or member is obsolete | |
} | |
Task<Attempt<ContentPublishingResult, ContentPublishingOperationStatus>> PublishAsync( | |
Guid key, | |
ICollection<CulturePublishScheduleModel> culturesToPublishOrSchedule, | |
Guid userKey) => StaticServiceProvider.Instance.GetRequiredService<ContentPublishingService>() | |
.PublishAsync(key, culturesToPublishOrSchedule, userKey); |
ContentScheduleCollection GetContentScheduleByContentId(Guid contentId) | ||
{ | ||
// Todo clean up default implementation in v17 | ||
Attempt<int> idAttempt = StaticServiceProvider.Instance.GetRequiredService<IIdKeyMap>() | ||
.GetIdForKey(contentId, UmbracoObjectTypes.Document); | ||
if (idAttempt.Success is false) | ||
{ | ||
throw new ArgumentException("Invalid contentId"); | ||
} | ||
return GetContentScheduleByContentId(idAttempt.Result); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentScheduleCollection GetContentScheduleByContentId(Guid contentId) | |
{ | |
// Todo clean up default implementation in v17 | |
Attempt<int> idAttempt = StaticServiceProvider.Instance.GetRequiredService<IIdKeyMap>() | |
.GetIdForKey(contentId, UmbracoObjectTypes.Document); | |
if (idAttempt.Success is false) | |
{ | |
throw new ArgumentException("Invalid contentId"); | |
} | |
return GetContentScheduleByContentId(idAttempt.Result); | |
} | |
ContentScheduleCollection GetContentScheduleByContentId(Guid contentId) => StaticServiceProvider.Instance | |
.GetRequiredService<ContentService>().GetContentScheduleByContentId(contentId); |
Maybe?
Description
The Management API currently supports publishing OR scheduling of documents, but there is no way to cancel schedules or clear them.
This PR aims to rectify this shortcoming while still maintaining the API model/endpoint and the spirit of the endpoint.
Details
With this PR, it should be possible to
Notes
A few things came up while making the necessary changes, please keep these in mind when reviewing/testing this PR
""
and"*"
, where it makes sense (and there was no sign it would cause any issues) this has been aligned to "*" and placed in a constant for clarityTesting
You can look for inspiration in the added integration tests, but please try to think outside the box.