-
Hi Everyone, I'm working on my API Controller for my Orchard Core microservice, and I have already implemented the What's the best practice to do it? 🤔 I have looked into some useful resources, like the What I have done so far:
Here is the Here is the model I used: using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.Media.Fields;
public class Publisher : ContentPart
{
public TextField PublisherId { get; set; } = new();
public TextField Name { get; set; } = new();
public BooleanField IsActive { get; set; } = new();
public MediaField Logo { get; set; } = new();
} public class PublisherApiModel
{
public string ContentItemId { get; set; }
[Required(ErrorMessage = $"{nameof(PublisherId)} is required.")]
public string PublisherId { get; set; }
[Required(ErrorMessage = $"{nameof(Name)} is required.")]
public string Name { get; set; }
public bool IsActive { get; set; }
public string Logo { get; set; }
} Here is the code of my method in a handler class, called by the API Controller: public async Task<ApiResult<PublisherApiModel>> UpsertPublisherAsync(PublisherApiModel model)
{
ContentItem contentItem;
bool isUpdate = true;
if (string.IsNullOrEmpty(model.ContentItemId))
{
isUpdate = false;
contentItem = await _contentManager.NewAsync("Publisher");
_logger.LogInformation("Creating new Publisher content item.");
}
else
{
contentItem = await _contentManager.GetAsync(model.ContentItemId, VersionOptions.DraftRequired);
if (contentItem == null)
{
_logger.LogError($"No Publisher found with ID: {model.ContentItemId}");
return new ApiResult<PublisherApiModel>(HttpStatusCode.NotFound, "Publisher not found.");
}
_logger.LogInformation($"Updating existing Publisher content item with ID: {model.ContentItemId}");
}
// Retrieve or create the Publisher part
var publisherPart = contentItem.As<Publisher>();
if (publisherPart == null)
{
publisherPart = new Publisher();
contentItem.Apply(nameof(Publisher), publisherPart);
}
contentItem.Alter<Publisher>(nameof(Publisher), part =>
{
part.PublisherId.Text = model.PublisherId;
part.Name.Text = model.Name;
part.IsActive.Value = model.IsActive;
part.Logo.Paths = new[] { model.Logo };
});
// Validate the content item
var validationResult = await _contentManager.ValidateAsync(contentItem);
if (!validationResult.Succeeded)
{
string allErrors = string.Join("; ", validationResult.Errors.Select(e => e.ErrorMessage));
_logger.LogError("Validation failed for Publisher content item: " + allErrors);
return new ApiResult<PublisherApiModel>(HttpStatusCode.BadRequest, allErrors);
}
// Persist changes
if (isUpdate)
{
await _contentManager.UpdateAsync(contentItem);
await _contentManager.PublishAsync(contentItem);
}
else
{
await _contentManager.CreateAsync(contentItem, VersionOptions.Published);
}
_logger.LogInformation($"{(isUpdate ? "Updated" : "Created")} Publisher content item with ID: {contentItem.ContentItemId}");
return new ApiResult<PublisherApiModel>(HttpStatusCode.OK, new PublisherApiModel
{
ContentItemId = contentItem.ContentItemId,
PublisherId = model.PublisherId,
IsActive = model.IsActive,
Name = model.Name,
Logo = model.Logo
});
} Can someone provide some guidance? 🤓 Thank you so much |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Maybe @Piedone @hishamco you have the answer 🙃 Thank you 🙏 |
Beta Was this translation helpful? Give feedback.
-
Have you checked out https://github.com/Lombiq/Orchard-Training-Demo-Module/blob/dev/Lombiq.TrainingDemo/Controllers/PersonListController.cs too? |
Beta Was this translation helpful? Give feedback.
Have you checked out https://github.com/Lombiq/Orchard-Training-Demo-Module/blob/dev/Lombiq.TrainingDemo/Controllers/PersonListController.cs too?