Last active
December 11, 2015 15:38
-
-
Save nul800sebastiaan/4622139 to your computer and use it in GitHub Desktop.
Web API Likes controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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