public static PostModel PostToPostModel(Post post) { PostModel postModel = new PostModel() { Content = post.Content, PostDate = post.PostDate, PostedBy = post.Author.Nickname }; double averageVote = 0; double sumVotes = 0; foreach (var vote in post.Votes) { sumVotes += vote.Value; } averageVote = sumVotes / post.Votes.Count; postModel.Rating = averageVote.ToString(); return postModel; }
public static ThreadDetailModel ThreadToThreadDetailModel(Thread thread) { var threadModel = new ThreadDetailModel() { Content = thread.Content, DateCreated = thread.DateCreated, ThreadId = thread.ThreadId, Title = thread.Title, }; foreach (var category in thread.Categories) { threadModel.Categories.Add(category.Name); } foreach (var post in thread.Posts) { var postModel = new PostModel() { Content = post.Content, PostDate = post.PostDate, PostedBy = post.Author.Nickname }; double averageVote = 0; double sumVotes = 0; foreach (var vote in post.Votes) { sumVotes += vote.Value; } averageVote = sumVotes/post.Votes.Count; postModel.Rating = averageVote.ToString(); threadModel.Posts.Add(postModel); } threadModel.CreatedBy = thread.User == null ? null : thread.User.Nickname; return threadModel; }
// POST api/posts public HttpResponseMessage Post(PostModel value, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responceMsg = this.PerformOperationAndHandleExceptions(() => { var context = new ForumDbContext(); using (context) { var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey && usr.Nickname == value.PostedBy); if (user == null) { throw new InvalidOperationException("Invalid username and password."); } Post newPost = new Post() { PostDate = DateTime.Now, PostContent = value.Content, Thread = context.Threads.FirstOrDefault(x => x.Id == value.ThreadId), User = user }; context.Posts.Add(newPost); context.SaveChanges(); //newPost.User = null; var responce = this.Request.CreateResponse(HttpStatusCode.Created); return responce; } }); return responceMsg; }
public HttpResponseMessage PostCreate(PostRegisterModel inputPost, string sessionKey) { HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions( () => { ForumContext context = new ForumContext(); using(context) { User currentUser = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); int currentCategoryId = inputPost.CurrentCategoryId; Category currentCategory = context.Categories.FirstOrDefault(cat => cat.Id == currentCategoryId); if(currentUser == null) { throw new ArgumentNullException("You should be logged or registered to create new posts."); } if(currentCategory == null) { throw new ArgumentNullException("You try to create post in non-existing category."); } Post newPost = new Post() { Author = currentUser, Category = currentCategory, Content = inputPost.Content, CreationDate = DateTime.Now, Title = inputPost.Title }; foreach(string tagName in inputPost.Tags) { Tag currentTag = context.Tags.FirstOrDefault(t => t.Name == tagName); if(currentTag == null) { currentTag = new Tag() { Name = tagName }; context.Tags.Add(currentTag); context.SaveChanges(); newPost.Tags.Add(currentTag); } else { newPost.Tags.Add(currentTag); } } context.Posts.Add(newPost); context.SaveChanges(); var resultPost = new PostModel { Id = newPost.Id, Content = newPost.Content, CategoryName = newPost.Category.Title, CategoryId = newPost.Category.Id, CreationDate = newPost.CreationDate, Tags = (from t in newPost.Tags select t.Name), Title = newPost.Title, Author = newPost.Author.Username }; HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.Created, resultPost); return response; } }); return responseMessage; }