public IActionResult PublishCharacter(int Id)
{
var character = Characters[--Id];
_characterService.PublishCharacterById(character);
return Content("Character created successfully.");
}
public IActionResult MassPublishCharacters()
{
Task.Run(() => _characterService.PublishAllCharacters(Characters))
.ContinueWith(task =>
{
if (task.IsFaulted)
{
// The task threw an exception
_logger.LogError(task.Exception, "An error occurred while trying to publish Rick and Morty characters.");
_hubContext.Clients.All.SendAsync("Error", "An error occurred while trying to publish Rick and Morty characters.");
}
else
{
// The task completed successfully
_logger.LogInformation("Rick and Morty characters published successfully.");
_hubContext.Clients.All.SendAsync("Success", "Rick and Morty characters published successfully!");
}
});
_hubContext.Clients.All.SendAsync("Info", "Character creation started successfully.");
return Content("Character creation started successfully.");
}
And here's the character service:
public IContent? PublishCharacterById(Character character)
{
var characterListingPage = GetCharacterListingPage() ?? throw new Exception("Character listing page not found.");
var characterContent = CreateOrUpdateCharacterContent(character, characterListingPage.Id);
if (characterContent is null) return null;
_contentService.SaveAndPublish(characterContent);
return characterContent;
}
public void PublishAllCharacters(List<Character> characters)
{
var characterListingPage = GetCharacterListingPage() ?? throw new Exception("Character listing page not found.");
foreach (var character in characters)
{
var characterContent = CreateOrUpdateCharacterContent(character, characterListingPage.Id);
if (characterContent is null) continue;
_contentService.SaveAndPublish(characterContent);
}
}
Finally, the CreateOrUpdateCharacterContent method:
public IContent? CreateOrUpdateCharacterContent(Character character, int parentId)
{
if (character.Name is null) return null;
var characterPages = _contentService.GetPagedChildren(parentId, 0, int.MaxValue, out _)
.Where(child => child.ContentType.Alias == "characterPage")
.ToList();
var existingContent = characterPages.FirstOrDefault(x => x.Name == character.Name);
var characterContent = existingContent is null
? _contentService.Create(character.Name, parentId, "CharacterPage")
: _contentService.GetById(existingContent.Id);
if (characterContent is null) return null;
characterContent.SetValue("characterId", character.Id);
characterContent.SetValue("characterName", character.Name);
characterContent.SetValue("status", character.Status);
characterContent.SetValue("species", character.Species);
characterContent.SetValue("type", character.Type);
characterContent.SetValue("gender", character.Gender);
if (character.Origin is not null && character.Origin.Name is not null && character.Origin.Url is not null)
{
characterContent.SetValue("origin", CreateExternalLink(character.Origin.Name, character.Origin.Url));
}
if (character.Location is not null && character.Location.Name is not null && character.Location.Url is not null)
{
characterContent.SetValue("location", CreateExternalLink(character.Location.Name, character.Location.Url));
}
characterContent.SetValue("image", character.Image);
if (character.Episode is not null)
{
characterContent.SetValue("episode", string.Join(Environment.NewLine, character.Episode));
}
characterContent.SetValue("url", character.Url);
characterContent.SetValue("created", character.Created);
return characterContent;
}
When creating a Character by id, I get the correct information in my character page properties.
However, when mass creating characters, the ID (characterId), the Location, Origin & Episode data is not matching up. The rest of the data does match up.
I'm a bit confused as to what could be going wrong. Any help would be appreciated!
Different behaviour when publishing by id vs mass publishing info coming from an API.
Hi,
I'm new to Umbraco so I'm trying to figure out creating data that comes from an API.
I have the following setup on my controller:
And these two controller actions:
And here's the character service:
Finally, the CreateOrUpdateCharacterContent method:
When creating a Character by id, I get the correct information in my character page properties.
However, when mass creating characters, the ID (characterId), the Location, Origin & Episode data is not matching up. The rest of the data does match up.
I'm a bit confused as to what could be going wrong. Any help would be appreciated!
Thanks!
is working on a reply...