Actually, Blog post is a very nice app for 2sxc, but it should be great to be able to register every time a user opens a blog, and then have a module to show for example 5 more viewed posts.
Basically i need to know how to update a post field (views for example) every time a user opens or see details for that post
Thanks for your help
This is an interesting idea - let me give you some minimal guidance to keep things running well...
ViewCount
with a property BlogPostId
and Count
Here's some pseudo code to do this...
var postId = post.EntityId;
var cacheId = "PostCount" + postId;
var count = Application[cacheId] ?? -1;
if(count == -1) {
// load the data
var countStates = AsList(App.Data["ViewCount"]);
var counter = countStates.FirstOrDefault(c => c.BlogPostId == postId);
if(counter != null) {
count = counter.Count;
} else {
count = 0;
}
}
// increment
count++;
// save to cache
Application[cacheId] = count;
// every 10 counts update the count in the storage / sql
// on high load, increase this to 100 or 1000
if(count % 10 == 0) {
// you'll have to figure this out yourself - see docs link below
App.Data.Update(...);
}
for the saving, check out the docs here https://docs.2sxc.org/api/dot-net/ToSic.Eav.Apps.AppData.html#ToSic_Eav_Apps_AppData_Update_System_Int32_System_Collections_Generic_Dictionary_System_String_System_Object__System_String_