https://discord.umbraco.com logo
#help-with-umbraco
Content Delivery API and partial classes
# help-with-umbraco
k

krebil

02/07/2024, 7:55 AM
In "traditional" Umbraco I like to create partial classes when I need to join strings or do some simple calculations on a models properties. That way I can reuse it easily everywhere and I only have to update it this one place. I can't seems to figure out how to include these in the delivery API however.
For example:
Copy code
namespace Umbraco.Cms.Web.Common.PublishedModels;

public partial class Bus
{
    public int Capacity => SeatRows * SeatsPrRow;
}
s

Sebastiaan

02/07/2024, 8:11 AM
So you have content, 3 properties called Bus, SeatRows and SeatsPrRow? And you want the Delivery API to output:
Capacity: 58
(which is not a property on your content item)?
k

krebil

02/07/2024, 8:31 AM
Exactly, capacity is not neccessarily something I need the editors to see. So I would rather not put it in a label that updates on a save event.
Well Bus is the Content type, the properties are SeatRows and SeatsPrRow
s

Sebastiaan

02/07/2024, 9:26 AM
Right now the answer is: do it on the client. 🙂 You could cheat, but it's clunky. What I would actually recommend in that case to have a proper property editor for it, whether or not the editor gets to see the capacity doesn't matter, but you can store the calculated value in your property editor and then it will be calculated only once and cached for the Delivery API.
k

krebil

02/07/2024, 9:32 AM
Thanks for the suggestions, I'm not a big fan of any of the options so I'll probably try my at luck at hijacking the response. Which might be worse, but I guess I'll find out.
s

Sebastiaan

02/07/2024, 9:36 AM
The cheat is: - add a dummy property - create a custom property value editor for that property and let it calculate the value from the other properties - delivery api picks it up and emits that value for you The tradeoff: calculation happens on each request to delivery api
k

krebil

02/07/2024, 10:30 AM
Yes, I'm just not digging the idea of having to put the dummy property on all the content types where I need it
I'm really looking for a way to easily define these compound properties and having it be picked up automatically.