using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.Http; using Npgsql.Web.Blog; using Yavsc.Model.Blogs; namespace Yavsc.ApiControllers { /// /// Blogs API controller. /// public class BlogsApiController : ApiController { private const string adminRoleName = "Admin"; /// /// Initialize the specified controllerContext. /// /// Controller context. protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext) { base.Initialize (controllerContext); if (!Roles.RoleExists (adminRoleName)) { Roles.CreateRole (adminRoleName); } } /// /// Tag the specified postid and tag. /// /// Postid. /// Tag. public long Tag (long postid,string tag) { BlogEntry e = BlogManager.GetPost (postid); if (!Roles.IsUserInRole ("Admin")) { string rguser = Membership.GetUser ().UserName; if (rguser != e.UserName) { throw new AccessViolationException ( string.Format ( "Vous n'avez pas le droit de tagger des billets du Blog de {0}", e.UserName)); } } return BlogManager.Tag (postid, tag); } /// /// Removes the post. /// /// User. /// Title. public void RemovePost(string user, string title) { BlogEntry e = BlogManager.GetPost (user, title); if (e == null) { throw new KeyNotFoundException ( string.Format("Aucun post portant le titre \"{0}\" pour l'utilisateur {1}", title, user)); } BlogManager.RemovePost (user, title); } /// /// Removes the tag. /// /// Tagid. public void RemoveTag(long tagid) { throw new NotImplementedException (); } } }