public IQueryable<TagModel> GetAll(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BlogContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var tagEntities = context.Tags; var models = (from tagEntity in tagEntities select new TagModel() { Id = tagEntity.Id, Name = tagEntity.Text, Posts = tagEntity.Posts.Count }); return models.OrderBy(t => t.Name); }); return responseMsg; }
//GET api/posts public IQueryable<PostModel> GetAll(string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { var postsModel = context.Posts.Select(p => new PostModel() { Id = p.Id, Title = p.Title, PostedBy = p.User.DisplayName, PostDate = p.PostDate, Text = p.Text, Tags = p.Tags.Select(t => t.Name), Comments = p.Comments.Select(c => new CommentModel() { Text = c.Text, CommentedBy = c.User.DisplayName, PostDate = c.PostDate }) }); return postsModel.OrderByDescending(p => p.PostDate); }); return responseMsg; }
public static IEnumerable<Tag> GetTags(PostModel post, BlogContext context) { var result = new List<Tag>(); if (post != null) { post.Tags.ForEach(pt => GetTag(context, pt, result)); var matches = Regex.Matches(post.Title, @"\w+"); matches.Cast<Match>().Select(m => m.Value).ForEach(pt => GetTag(context, pt, result)); } return result; }
public HttpResponseMessage PostRegisterUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateNickname(model.DisplayName); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var nicknameToLower = model.DisplayName.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower || usr.Nickname.ToLower() == nicknameToLower ); if (user != null) { throw new InvalidOperationException("Users exists"); } user = new User() { Username = usernameToLower, Nickname = model.DisplayName, AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedModel = new LoggedUserModel() { Nickname = user.Nickname, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
//GET api/tags public IQueryable<TagModel> GetAll(string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { var tagsModel = context.Tags.Select(p => new TagModel() { Id = p.Id, Name = p.Name, Posts = p.Posts.Count() }); return tagsModel.OrderBy(p => p.Name); }); return responseMsg; }
private static void GetTag(BlogContext context, string pt, List<Tag> result) { if (!string.IsNullOrEmpty(pt)) { var tag = context.Tags.FirstOrDefault(t => t.Name == pt); if (tag == null) { tag = new Tag() { Name = pt.ToLower() }; context.Tags.Add(tag); context.SaveChanges(); } result.Add(tag); } }
public IQueryable<PostDetailsModel> GetByTag(int tagId) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var context = new BlogContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var postEntities = context.Posts; var models = (from postEntity in postEntities select new PostDetailsModel() { Id = postEntity.Id, Title = postEntity.Title, PostedBy = postEntity.User.Nickname, PostDate = postEntity.PostDate, Text = postEntity.Text, Tags = (from tagsEntity in postEntity.Tags where tagsEntity.Id.Equals(tagId) select tagsEntity.Text), Comments = (from commentsEntity in postEntity.Comments select new CommentModel() { Text = commentsEntity.Text, CommentedBy = commentsEntity.User.Nickname, PostDate = commentsEntity.CommentDate }), }); return models.OrderByDescending(pst=>pst.PostDate); }); return responseMsg; }
//GET api/tags/{tagId}/posts public FullTagModel GetPostsByTagId(int tagId, string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { if (tagId < 1) { throw new InvalidOperationException("Invalid tagId"); } var tag = context.Tags.FirstOrDefault(p => p.Id == tagId); if (tag != null) { var fullTagModel = new FullTagModel() { Name = tag.Name, Id = tag.Id, PostsModels = tag.Posts.OrderByDescending(p => p.PostDate).Select(p => new PostModel() { Id = p.Id, Title = p.Title, PostedBy = p.User.DisplayName, PostDate = p.PostDate, Text = p.Text, Comments = p.Comments.Select(c => new CommentModel() { Text = c.Text, CommentedBy = c.User.DisplayName, PostDate = c.PostDate }) }) }; return fullTagModel; } else { throw new InvalidOperationException("Tag with id " + tagId + " do not exist"); } }); return responseMsg; }
public IQueryable<PostModel> GetAllPostsByTagId([FromUri]int tagId, [FromUri]string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BlogContext(); var tag = context.Tags.FirstOrDefault(t => t.Id == tagId); if (tag == null) { throw new InvalidOperationException("This tag doesn't exists."); } PostsController postsController = new PostsController(); var posts = postsController.GetByTags(tag.Text, sessionKey); return posts.OrderByDescending(p => p.PostDate); }); return responseMsg; }
public HttpResponseMessage PostLoginUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower && usr.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Users doesn't exists"); } string generateSessionKey = this.GenerateSessionKey(user.Id); user.SessionKey = generateSessionKey; context.SaveChanges(); var loggedModel = new LoggedUserModel() { DisplayName = user.DisplayName, SessionKey = generateSessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
//POST api/posts/create public HttpResponseMessage PostCreate(PostModel post, string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { HttpResponseMessage response; var newPost = new Post(); using (var tran = new TransactionScope()) { newPost.Title = post.Title; newPost.Text = post.Text; newPost.PostDate = DateTime.Now; newPost.User = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); var tags = Helpers.GetTags(post, context); tags.ForEach(t => newPost.Tags.Add(t)); context.Posts.Add(newPost); context.SaveChanges(); tran.Complete(); } if (newPost.Id > 0) { response = this.Request.CreateResponse(HttpStatusCode.Created, new ResponsePostModel() { Id = newPost.Id, Title = newPost.Title }); } else { response = this.Request.CreateResponse(HttpStatusCode.InternalServerError); } return response; }); return responseMsg; }
public IQueryable<PostModel> GetAll(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BlogContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var postEntities = context.Posts; var models = (from postEntity in postEntities select new PostModel() { Id = postEntity.Id, Title = postEntity.Title, PostedBy = postEntity.PostedBy.DisplayName, PostDate = postEntity.PostDate, Text = postEntity.Text, Tags = (from tagEntity in postEntity.Tags select tagEntity.Text), Comments = (from commentEntity in postEntity.Comments select new CommentModel() { Text = commentEntity.Text, CommentedBy = commentEntity.CommentedBy.DisplayName, PostDate = commentEntity.PostDate }) }); return models.OrderByDescending(p => p.PostDate); }); return responseMsg; }
private static Tag CreateOrLoadTag(BlogContext context, string tagName) { Tag existingTag = (from t in context.Tags where t.Text == tagName select t).FirstOrDefault(); if (existingTag != null) { return existingTag; } Tag newTag = new Tag(); newTag.Text = tagName; context.Tags.Add(newTag); context.SaveChanges(); return newTag; }
//PUT api/posts/{postId}/comment public HttpResponseMessage PutComment(int postId, CommentModel comment, string sessionKey) { var context = new BlogContext(); var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () => { if (comment != null && string.IsNullOrEmpty(comment.Text)) { throw new InvalidOperationException("Comment text cannot be empty"); } var post = context.Posts.FirstOrDefault(p => p.Id == postId); if (post != null) { post.Comments.Add(new Comment() { Text = comment.Text, User = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey), PostDate = DateTime.Now }); context.SaveChanges(); } else { throw new InvalidOperationException("Post with id " + postId + " do not exist"); } var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; }
public HttpResponseMessage PutNewComment([FromUri] int postId, [FromUri] string sessionKey, [FromBody] CommentModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Users doesn't exists"); } var post = context.Posts.FirstOrDefault(p => p.Id == postId); if (post == null) { throw new InvalidOperationException("This post doesn't exists"); } Comment newComment = new Comment(); newComment.Text = model.Text; newComment.PostDate = DateTime.Now; newComment.CommentedBy = user; newComment.Post = post; post.Comments.Add(newComment); context.Comments.Add(newComment); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMsg; }
//api/posts?tags=web,webapi public IQueryable<PostDetailsModel> GetByTags(string tags) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var context = new BlogContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } string[] keywords = tags.Split(','); var postEntities = context.Posts; //where keywords.Any(val => tagsEntity.Text.Contains(val)) //var models = // (from postEntity in postEntities // select new PostDetailsModel() // { // Id = postEntity.Id, // Title = postEntity.Title, // PostedBy = postEntity.User.Nickname, // PostDate = postEntity.PostDate, // Text = postEntity.Text, // Tags = (from tagsEntity in postEntity.Tags // //where tagsEntity==null // where keywords.Any(val => tagsEntity.Text.Contains(val)) // select tagsEntity.Text), // // Comments = (from commentsEntity in postEntity.Comments // select new CommentModel() // { // Text = commentsEntity.Text, // CommentedBy = commentsEntity.User.Nickname, // PostDate = commentsEntity.CommentDate // // }), // // }); // var models = this.GetAll(); foreach (var item in keywords) { models = this.GetAll().Where(x => x.Tags.Any(y => y == item)); } return models; }); return responseMsg; }
public HttpResponseMessage PostPost(CreatePostModel model) { var responseMessage = this.PerformOperationAndHandleExceptions(() => { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var dbContext = new BlogContext(); // dbContext.Configuration.ProxyCreationEnabled = false; using (dbContext) { var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged when create a new post!"); } List<Tag> newTags = new List<Tag>(); if (!(model.Tags==null)) { foreach (var tagItem in model.Tags) { var tagEntity = new Tag() { Text = tagItem.ToLower() }; newTags.Add(tagEntity); } } //split title and add to the tags string[] wordsIntitle = model.Title.Split(new char[] { ' ', ',','.','!','?' }); foreach (var word in wordsIntitle) { if (!(word == string.Empty)) { var tagEntity = new Tag() { Text = word.ToLower().Trim() }; newTags.Add(tagEntity); } } var newPost = new Post() { Title = model.Title, Text = model.Text, PostDate = DateTime.Now, User = user, Tags=newTags }; dbContext.Posts.Add(newPost); dbContext.SaveChanges(); var postCreatedModel = new CreatedPostModel() { Title = newPost.Title, Id = newPost.Id }; var ret = Request.CreateResponse(HttpStatusCode.Created, postCreatedModel); // var ret = Request.CreateResponse(HttpStatusCode.Created); return ret; } }); return responseMessage; }
public HttpResponseMessage PutCommentOnPost(int postId, CreateCommentModel model) { var responseMessage = this.PerformOperationAndHandleExceptions(() => { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var dbContext = new BlogContext(); // dbContext.Configuration.ProxyCreationEnabled = false; using (dbContext) { var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Users must be logged when create a new comment!"); } var post = dbContext.Posts.FirstOrDefault(pst => pst.Id == postId); var newComment = new Comment() { Text = model.Text, CommentDate = DateTime.Now, User = user, Post=post }; dbContext.Comments.Add(newComment); dbContext.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMessage; }
public HttpResponseMessage PutLogoutUser(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Users doesn't exists"); } user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } }); return responseMsg; }
public HttpResponseMessage PutLogoutUser() { var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey"); var responseMessage = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user.SessionKey == null) { throw new ArgumentNullException("User is already logout!"); } user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } } ); return responseMessage; }
public HttpResponseMessage PostNewPost(PostModel model, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BlogContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Users doesn't exists"); } if (model.Title == null) { throw new InvalidOperationException("Title cannot be null"); } if (model.Text == null) { throw new InvalidOperationException("Text cannot be null"); } Post post = new Post(); post.Title = model.Title; post.Text = model.Text; post.PostDate = DateTime.Now; post.PostedBy = user; foreach (var tag in model.Tags) { Tag currentTag = CreateOrLoadTag(context, tag.ToLower()); post.Tags.Add(currentTag); } string[] titleTags = Regex.Split(model.Title, @"[,!\. ;?']+"); foreach (var titleTagName in titleTags) { Tag titleTag = CreateOrLoadTag(context, titleTagName.ToLower()); post.Tags.Add(titleTag); } context.Posts.Add(post); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, new { Title = post.Title, Id = post.Id }); return response; } }); return responseMsg; }
public HttpResponseMessage PutLogoutUser(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { ValidateSessionKey(sessionKey); var context = new BlogContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user != null) { user.SessionKey = null; context.SaveChanges(); } } var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; }