Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Last active December 11, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nul800sebastiaan/4622139 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/4622139 to your computer and use it in GitHub Desktop.
Web API Likes controller
//StartupEventHandler.cs
using System.Linq;
using System.Web.Http;
using System.Web.Routing;
using Umbraco.Core;
using Umbraco.Web;
namespace Training.Site.EventHandlers
{
public class StartupEventHandler : IApplicationEventHandler
{
public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public void OnApplicationInitialized(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{ }
public void OnApplicationStarted(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{ }
}
}
//LikesController.cs (call like: /api/Likes?statusId=1234
using System.Linq;
using System.Web.Http;
namespace Training.Site.BaseExtensions
{
public class LikesController : ApiController
{
public int Get(int statusId)
{
var cs = Umbraco.Core.ApplicationContext.Current.Services.ContentService;
var post = cs.GetById(statusId);
int numberOfLikes;
var currentLikes = post.GetValue("likes").ToString();
int.TryParse(currentLikes, out numberOfLikes);
numberOfLikes = numberOfLikes + 1;
post.SetValue("likes", numberOfLikes);
cs.SaveAndPublish(post, 0);
return numberOfLikes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment