cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
https://www.cofoundry.org
MIT License
836 stars 146 forks source link

Third party data as "Widget" #404

Closed ScarlettCode closed 3 years ago

ScarlettCode commented 3 years ago

Hi,

I'm looking for some guidance in what would be the best way to integrate external data into the CMS. I have some external data and I want to create "widgets" which can then be placed anywhere on any page. My current thought is to use Page Block Types. My question is can I call out to a third party api or another database from within the MapAsync method of the IPageBlockTypeDisplayModelMapper or is there a better way of doing this?

Then following on for this, if a "Widget" was user specific can I access a users logged in state and id from within the MapAsync method so I can use that to fetch the correct data for a specific user?

Thanks

HeyJoel commented 3 years ago

You can inject anything into your IPageBlockTypeDisplayModelMapper and call anything in MapAsync, so yes you can call out to a service or run a custom database call. Some things to consider:

For accessing user state:

For anything else, you can access user data though IContentRepository e.g.

var user = _contentRepository
    .Users()
    .GetCurrent()
    .AsSummary()
    .ExecuteAsync();

Or inject IUserContextService and use that to get basic user data such as the id and go from there.