public HttpResponseMessage Get([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid) { var context = new ExamContext(); IsUserLoged(sessionKey, context); var tags = from t in context.Tags select new TagsDTO() { Id = t.Id, Name = t.Name, Posts = t.Posts.Count, }; var orderdTags = tags.OrderBy(x => x.Name); var response = this.Request.CreateResponse(HttpStatusCode.OK, orderdTags); return response; } else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
public HttpResponseMessage Get([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, int tagId) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid) { var context = new ExamContext(); IsUserLoged(sessionKey, context); var allPosts = (from ps in context.Posts where ps.Tags.Any(x => x.Id == tagId) select new PostsForATagDTO() { Id = ps.Id, Text = ps.Text, PostDate = ps.PostDate, Title = ps.Title, PostedBy = ps.PostedBy.Username, Comments = (from comment in ps.Comments select new CommentsByPostsDTO() { Text = comment.Text, CommentBy = comment.CommentedBy.Username, PostDate = comment.PostDate, }), Tags = (from tag in ps.Tags select tag.Name).AsEnumerable(), }); var orderdTags = allPosts.OrderBy(x => x.PostDate); var response = this.Request.CreateResponse(HttpStatusCode.OK, orderdTags); return response; } else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
public HttpResponseMessage GetAllThreads([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { IQueryable<PostsDTO> allPosts; var context = new ExamContext(); IsUserLoged(sessionKey, context); allPosts = FindAllPosts(context); var response = this.Request.CreateResponse(HttpStatusCode.OK, allPosts.OrderByDescending(x => x.PostDate)); return response; // return allPosts.OrderByDescending(x=>x.PostDate); }); return responseMsg; }
public HttpResponseMessage GetPage( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, string keyword) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { IEnumerable<PostsDTO> allPosts; var context = new ExamContext(); IsUserLoged(sessionKey, context); allPosts = FindAllPostsByKeyword(context, keyword); var response = this.Request.CreateResponse(HttpStatusCode.OK, allPosts); return response; }); return responseMsg; }
public HttpResponseMessage GetPage( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, int page, int count) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { IQueryable<PostsDTO> allPosts; var context = new ExamContext(); IsUserLoged(sessionKey, context); allPosts = FindAllPosts(context); //not very right but .. var postsOnPage = allPosts.OrderByDescending(x=>x.PostDate).Skip(page * count).Take(count); var response = this.Request.CreateResponse(HttpStatusCode.OK, postsOnPage.OrderByDescending(x => x.PostDate)); return response; }); return responseMsg; }
public HttpResponseMessage PostLoginUser(LoginDTO userJSON) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid) { var context = new ExamContext(); using (context) { var foundUser = context.Users.FirstOrDefault(u => u.Username == userJSON.Username && u.AuthCode == userJSON.AuthCode ); if (foundUser == null) { throw new ArgumentOutOfRangeException("Wrong username or password"); } else { string sessionKey = AddSessionKey(context, foundUser); //crateREsponse LoginResponseDTO user = CreateResponseLogin(foundUser, sessionKey); var response = this.Request.CreateResponse(HttpStatusCode.Created, user); return response; } }//end using }//end validstate else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
private static void IsUserLoged(string sessionKey, ExamContext context) { Users user = (from u in context.Users where u.SessionKey == sessionKey select u).FirstOrDefault(); if (user == null) { throw new ArgumentException("User is not found in database"); } }
public HttpResponseMessage PostAddPost( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, CreatePostDTO value) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { var context = new ExamContext(); IsUserLoged(sessionKey, context); Posts newPost = CreatePost(value, context); CreatePostResponse responsJson = new CreatePostResponse() { Id = newPost.Id, Title = newPost.Title }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responsJson); return response; }); return responseMsg; }
private Comments CreateComment(ExamContext context, AddCommentDTO commentJson, Users user) { Comments newComment = new Comments() { Text = commentJson.Text, }; newComment.PostDate = DateTime.Now; newComment.CommentedBy = user; context.Comments.Add(newComment); context.SaveChanges(); return newComment; }
private static Users FindUsernameForComment(string sessionKey, ExamContext context) { Users user = (from u in context.Users where u.SessionKey == sessionKey select u).FirstOrDefault(); if (user == null) { throw new ArgumentException("User is not found in database"); } return user; }
private static Users CreateNewUSer(UserRegisterDTO userJSON, ExamContext context) { // when the user is created Users createdUser = new Users() { DisplayName = userJSON.DisplayName, Username = userJSON.Username, AuthCode = userJSON.AuthCode, }; //we can make sessionkey context.Users.Add(createdUser); context.SaveChanges(); return createdUser; }
private static Posts CreatePost(CreatePostDTO value, ExamContext context) { Posts newPost = new Posts() { Title = value.Title, Text = value.Text, }; newPost.PostDate = DateTime.Now; ICollection<Tags> allTags = CreateOrLoadTags(value, context); newPost.Tags = allTags; context.Posts.Add(newPost); context.SaveChanges(); return newPost; }
private static IQueryable<PostsDTO> FindAllPosts(ExamContext context) { IQueryable<PostsDTO> allPosts; allPosts = from ps in context.Posts select new PostsDTO() { Id = ps.Id, Text = ps.Text, PostDate = ps.PostDate, Title = ps.Title, PostedBy = ps.PostedBy.Username, Comments = (from comment in ps.Comments select new CommentsByPostsDTO() { Text = comment.Text, CommentBy = comment.CommentedBy.Username, PostDate = comment.PostDate, }), Tags = (from tag in ps.Tags select tag.Name).AsEnumerable(), }; return allPosts; }
private static ICollection<Tags> CreateOrLoadTags(CreatePostDTO value, ExamContext context) { ICollection<Tags> allTags = new List<Tags>(); foreach (var tag in value.Tags) { var existingTag = context.Tags.FirstOrDefault(t => t.Name == tag); if (existingTag == null) { context.Tags.Add(new Tags() { Name = tag }); context.SaveChanges(); } allTags.Add(existingTag); } return allTags; }
public HttpResponseMessage PutComment( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey, int postId, AddCommentDTO commentJson) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { var context = new ExamContext(); Users user = FindUsernameForComment(sessionKey, context); var post = context.Posts.FirstOrDefault(x => x.Id == postId); if (post == null) { throw new ArgumentNullException("Invalid post"); } Comments newComment = CreateComment(context, commentJson, user); post.Comments.Add(newComment); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; }
public HttpResponseMessage PostRegisterUser(UserRegisterDTO userJSON) { var responseMsg = base.PerformOperationAndHandleExceptions( () => { if (ModelState.IsValid && userJSON != null) { var context = new ExamContext(); using (context) { this.ValidateUsername(userJSON.Username); this.ValidateDisplayName(userJSON.DisplayName); this.ValidAuthCode(userJSON.AuthCode); IsUserExisting(userJSON, context); Users createdUser = CreateNewUSer(userJSON, context); string sessionKey = this.GenerateSessionKey(createdUser.Id); createdUser.SessionKey = sessionKey; context.Entry(createdUser).State = System.Data.EntityState.Modified; context.SaveChanges(); UserRegisterResponseDTO user = new UserRegisterResponseDTO() { DisplaName = userJSON.DisplayName, Sessionkey = sessionKey, }; var response = this.Request.CreateResponse(HttpStatusCode.Created, user); return response; } } else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
private static void IsUserExisting(UserRegisterDTO userJSON, ExamContext context) { //if there is already such user Users foundUser = context.Users.FirstOrDefault(x => x.Username == userJSON.Username || x.DisplayName == userJSON.DisplayName); if (foundUser != null) { throw new ArgumentException("User already exists"); } }
private static IQueryable<PostsDTO> FindAllPostsByKeyword(ExamContext context, string keyword) { IQueryable<PostsDTO> allPosts; allPosts = from ps in context.Posts where ps.Title.Contains(keyword) select new PostsDTO() { Id = ps.Id, Text = ps.Text, PostDate = ps.PostDate, Title = ps.Title, PostedBy = ps.PostedBy.Username, Comments = (from comment in ps.Comments select new CommentsByPostsDTO() { Text = comment.Text, CommentBy = comment.CommentedBy.Username, PostDate = comment.PostDate, }), Tags = (from tag in ps.Tags select tag.Name).AsEnumerable(), }; // var searchedPosts = allPosts.Where(x => x.Title.Split(new char[] { ' ' }).All(y => y.ToLower() == keyword)); return allPosts; }
public HttpResponseMessage PutLogoutUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = base.PerformOperationAndHandleExceptions(() => { if (ModelState.IsValid) { var context = new ExamContext(); using (context) { if (sessionKey == null) { throw new ArgumentNullException("Missing sessionKey"); } var user = context.Users.FirstOrDefault(x => x.SessionKey == sessionKey); if (user == null) { throw new ArgumentNullException("Not found user with that sessionkey"); } user.SessionKey = null; context.Entry(user).State = System.Data.EntityState.Modified; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }//end using }//end validstate else { var errors = String.Join(" ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)); var errorMessage = string.Format("User input validation failed. Errors: {0}", errors); throw new ArgumentException(errorMessage); } }); return responseMsg; }
private string AddSessionKey(ExamContext context, Users foundUser) { string sessionKey = this.GenerateSessionKey(foundUser.Id); foundUser.SessionKey = sessionKey; context.Entry(foundUser).State = System.Data.EntityState.Modified; context.SaveChanges(); return sessionKey; }