Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nicolas 4 posts 85 karma points
    Mar 09, 2024 @ 20:07
    Nicolas
    0

    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:

        public class RickAndMortyDashboardController : UmbracoApiController
        {
           private readonly IRickAndMortyClient _rickAndMortyClient;
           private readonly ILogger<RickAndMortyDashboardController> _logger;
           private readonly CharacterService _characterService;
           private static List<Character> Characters { get; set; } = [];
           ...
    

    And these two controller actions:

    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!

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft