예제 #1
0
        public IQueryable<TagModel> GetAll(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BlogSystemContext();

                var user = context.Users.FirstOrDefault(
                    usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid sessionKey");
                }

                var tagEntities = context.Tags;
                var models =
                    (from tagEntity in tagEntities
                     select new TagModel()
                     {
                         Id = tagEntity.Id,
                         Name = tagEntity.Name,
                         Posts = tagEntity.Posts.Count
                     });
                return models.OrderBy(t => t.Name);
            });

            return responseMsg;
        }
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(UsersController))
            {
                var context = new BlogSystemContext();
                var repository = new DbUserRepository(context);

                return new UsersController(repository);
            }
            else if (serviceType == typeof(PostsController))
            {
                var context = new BlogSystemContext();
                var repository = new DbPostRepository(context);

                return new PostsController(repository);
            }
            else if (serviceType == typeof(TagsController))
            {
                var context = new BlogSystemContext();
                var repository = new DbTagRepository(context);

                return new TagsController(repository);
            }
            else
            {
                return null;
            }
        }
예제 #3
0
        public IQueryable<TagModel> GetAll(
             [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = ExceptionHandler(
            () =>
            {
                var context = new BlogSystemContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                }

                var tags = this.tagRepository.All();

                var models =
                    (from t in tags
                     select new TagModel
                     {
                         Id = t.Id,
                         Name = t.Name,
                         Posts = t.Posts.Count
                     });

                return models.OrderBy(t => t.Name);
            });

            return responseMsg;
        }
 public object GetService(Type serviceType)
 {
     if (serviceType == typeof(UsersController))
     {
         var context = new BlogSystemContext();
         var repository = new EfRepository<User>(context);
         return new UsersController(repository);
     }
     else if (serviceType == typeof(PostsController))
     {
         var context = new BlogSystemContext();
         var postsRepository = new EfRepository<Post>(context);
         var usersRepository = new EfRepository<User>(context);
         var tagsRepository = new EfRepository<Tag>(context);
         var commentsRepository = new EfRepository<Comment>(context);
         return new PostsController(postsRepository, usersRepository, tagsRepository, commentsRepository);
     }
     else if (serviceType == typeof(TagsController))
     {
         var context = new BlogSystemContext();
         var tagsRepository = new EfRepository<Tag>(context);
         var usersRepository = new EfRepository<User>(context);
         var postsRepository = new EfRepository<Post>(context);
         return new TagsController(tagsRepository, usersRepository, postsRepository);
     }
     else
     {
         return null;
     }
 }
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {

                var context = new BlogSystemContext();

                using (context)
                {
                    this.ValidateUserName(model.UserName);
                    this.ValidateDisplayName(model.DisplayName);
                    this.ValidateAuthCode(model.AuthCode);

                    var userNameToLower = model.UserName.ToLower();
                    var displayNameToLower = model.DisplayName.ToLower();

                    var user = context.Users.FirstOrDefault(
                        usr => usr.UserName == userNameToLower &&
                            usr.DisplayName == usr.DisplayName);

                    if (user != null)
                    {
                        throw new InvalidOperationException("User exists!");
                    }

                    user = new User()
                    {
                        UserName = userNameToLower,
                        DisplayName = model.DisplayName,
                        AuthCode = model.AuthCode
                    };

                    context.Users.Add(user);
                    context.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    context.SaveChanges();

                    var loggedModel = new UserLoggedModel()
                    {
                        DisplayName = user.DisplayName,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                        loggedModel);

                    return response;
                }
            });

            return responseMsg;
        }
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(UsersController))
            {
                return new UsersController(this.UsersRepository as IRepository<User>);
            }
            else if (serviceType == typeof(PostsController))
            {
                var context = new BlogSystemContext();
                this.postsRepository = new EfRepository<Post>(context);
                this.usersRepository = new EfRepository<User>(context);
                this.tagsRepository = new EfRepository<Tag>(context);
                return new PostsController(postsRepository, usersRepository, tagsRepository, commentsRepository);
            }

            return null;
        }
예제 #7
0
        public IQueryable<PostModel> GetPosts(int tagId,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new BlogSystemContext();

                var user = context.Users.FirstOrDefault(
                    usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid sessionKey");
                }

                var tag = context.Tags.FirstOrDefault(t => t.Id == tagId);

                if (tag == null)
                {
                    throw new InvalidOperationException("Invalid tagId");
                }

                var postEntities = tag.Posts;
                var models =
                    (from postEntity in postEntities
                     select new PostModel()
                     {
                         Id = postEntity.Id,
                         Title = postEntity.Title,
                         PostDate = postEntity.PostDate,
                         Text = postEntity.Text,
                         PostedBy = postEntity.User.Nickname,
                         Tags = (from tagEntity in postEntity.Tags
                                 select tagEntity.Name),
                         Comments = (from commentEntity in postEntity.Comments
                                     select new CommentModel()
                                     {
                                         Text = commentEntity.Text,
                                         PostDate = commentEntity.PostDate,
                                         CommentedBy = commentEntity.User.Nickname
                                     })
                     });
                return models.OrderByDescending(p => p.PostDate);
            });

            return responseMsg.AsQueryable();
        }
예제 #8
0
        public IQueryable<PostModel> GetAll(
             [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = ExceptionHandler(
            () =>
            {
                var context = new BlogSystemContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                }

                var posts = this.postRepository.All();

                var models =
                    (from p in posts
                     select new PostModel
                     {
                         Id = p.Id,
                         Title = p.Title,
                         PostDate = p.PostDate,
                         PostedBy = p.User.DisplayName,
                         Text = p.Text,
                         Tags = (from t in p.Tags
                                 select t.Name),
                         Comments = (from c in p.Comments
                                     select new CommendModel
                                     {
                                         Text = c.Content,
                                         CommentedBy = c.User.DisplayName,
                                         PostDate = c.CommentDate
                                     })
                     });

                return models.OrderByDescending(d => d.PostDate);
            });

            return responseMsg;
        }
예제 #9
0
        public ICollection<PostModel> GetPostsForTag(int tagId,
             [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = ExceptionHandler(
            () =>
            {
                var context = new BlogSystemContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                }

                var tag = this.tagRepository.Get(tagId);

                var models =
                    (from t in tag.Posts
                     select new PostModel
                     {
                         Id = t.Id,
                         Title = t.Title,
                         PostDate = t.PostDate,
                         PostedBy = t.User.DisplayName,
                         Text = t.Text,
                         Tags = (from p in t.Tags
                                 select p.Name),
                         Comments = (from c in t.Comments
                                     select new CommendModel
                                     {
                                         Text = c.Content,
                                         CommentedBy = c.User.DisplayName,
                                         PostDate = c.CommentDate
                                     })
                     });

                return models.OrderBy(p => p.PostDate);
            });

            return responseMsg.ToList();
        }
예제 #10
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogSystemContext();
                  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("Invalid username or password");
                      }

                      if (user.SessionKey == null)
                      {
                          user.SessionKey = this.GenerateSessionKey(user.Id);
                          context.SaveChanges();
                      }

                      var loggedModel = new UserLoggedModel()
                      {
                          Nickname = user.Nickname,
                          SessionKey = user.SessionKey
                      };

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.Created,
                                          loggedModel);
                      return response;
                  }
              });

            return responseMsg;
        }
        public IQueryable<PostModel> GetAll(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);

                var context = new BlogSystemContext();

                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.User.DisplayName,
                         PostDate = postEntity.DatePosted,
                         Text = postEntity.Text,
                         Tags = (from tagEntity in postEntity.Tags
                                       select tagEntity.Name),
                         Comments = (from commentEntity in postEntity.Comments
                                  select new CommentModel()
                                  {
                                      Text = commentEntity.Text,
                                      CommentedBy = commentEntity.User.DisplayName,
                                      PostDate = commentEntity.DateCommented
                                  }),
                     });

                return models.OrderByDescending(post => post.PostDate);
            });

            return responseMsg;
        }
        public IQueryable<IEnumerable<TagModel>> GetAll(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);

                var context = new BlogSystemContext();

                var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password");
                }

                var postEntities = context.Posts;
                var postsModels =
                    (from postEntity in postEntities
                     select new PostTagsModel()
                     {
                         Tags =
                         (from tagEntity in postEntity.Tags
                          select new TagModel()
                          {
                              Id = postEntity.Id,
                              Name = tagEntity.Name,
                              Posts = tagEntity.Posts.Count
                          })
                     });

                var tagModels = postsModels.Select(p => p.Tags);

                return tagModels;
            });

            return responseMsg;
        }
예제 #13
0
        public HttpResponseMessage PostPost(CreatePostModel model,
             [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = ExceptionHandler(
            () =>
            {
                User user;
                var context = new BlogSystemContext();
                using (context)
                {
                    user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                }

                Dictionary<string, Tag> tags = new Dictionary<string, Tag>();

                foreach (var tag in model.Tags)
                {
                    var tagToLower = tag.ToLower();
                    if (!tags.ContainsKey(tagToLower))
                    {
                        Tag newTag = new Tag { Name = tagToLower };
                        tags.Add(tagToLower, newTag);
                    }
                }

                var titleTags = model.Title.Split(' ');

                foreach (var tag in titleTags)
                {
                    var tagToLower = tag.ToLower();
                    if (!tags.ContainsKey(tagToLower))
                    {
                        Tag newTag = new Tag { Name = tagToLower };
                        tags.Add(tagToLower, newTag);
                    }
                }

                List<Tag> filtTags = new List<Tag>();

                foreach (var tag in tags.Values)
                {
                    filtTags.Add(tag);
                }

                var newPost = new Post
                {
                    Title = model.Title,
                    Text = model.Text,
                    Tags = filtTags,
                    PostDate = DateTime.Now,
                    User = user,
                };

                var dbPost = this.postRepository.Add(newPost);

                var postMode = new CreatedPostModel
                {
                    Id = dbPost.Id,
                    Title = dbPost.Title
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, postMode);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = postMode.Id }));

                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage PutCommentPost(int id, [FromBody]CommentModelSimple comment,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);
                var context = new BlogSystemContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }

                    var post = context.Posts.FirstOrDefault(p => p.Id == id);

                    post.Comments.Add(new Comment()
                    {
                        Text = comment.Text,
                        DateCommented = DateTime.Now,
                        User = user
                    });

                    context.SaveChanges();
                    
                    var response = this.Request.CreateResponse(HttpStatusCode.OK);

                    return response;
                }
            });

            return responseMsg;
        }
예제 #15
0
 public UsersController()
 {
     var dbContext = new BlogSystemContext();
     this.userRepository = new DbUserRepository(dbContext);
 }
예제 #16
0
        public HttpResponseMessage PutLogoutUser(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogSystemContext();
                  using (context)
                  {
                      var user = context.Users.FirstOrDefault(
                          usr => usr.SessionKey == sessionKey);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid sessionKey");
                      }

                      user.SessionKey = null;
                      context.SaveChanges();

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.OK);
                      return response;
                  }
              });

            return responseMsg;
        }
예제 #17
0
        public HttpResponseMessage PutComment(int postId, CommentNewModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogSystemContext();
                  using (context)
                  {
                      var user = context.Users.FirstOrDefault(
                          usr => usr.SessionKey == sessionKey);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid sessionKey");
                      }

                      var post = context.Posts.FirstOrDefault(p => p.Id == postId);

                      if (post == null)
                      {
                          throw new InvalidOperationException("Invalid postId");
                      }

                      if (model.Text == null)
                      {
                          throw new ArgumentNullException("Comment text cannot be null");
                      }

                      var comment = new Comment()
                      {
                          Text = model.Text,
                          User = user,
                          PostDate = DateTime.Now,
                          Post = post
                      };

                      context.Comments.Add(comment);
                      context.SaveChanges();

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.OK, "NULL");
                      return response;
                  }
              });

            return responseMsg;
        }
예제 #18
0
        public HttpResponseMessage PostNewPost(PostNewModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogSystemContext();
                  using (context)
                  {
                      var user = context.Users.FirstOrDefault(
                          usr => usr.SessionKey == sessionKey);
                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid sessionKey");
                      }

                      if (model.Title == null || model.Text == null)
                      {
                          throw new ArgumentNullException("Post title or post text cannot be null");
                      }

                      string[] titleWords = model.Title.Split(
                          new char[] { ' ', ',', '.', '!', '?', '\'', '(', ')' },
                          StringSplitOptions.RemoveEmptyEntries);

                      IList<Tag> tags = new List<Tag>();
                      if (model.Tags != null)
                      {
                          foreach (var item in model.Tags)
                          {
                              var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower());
                              if (tag == null)
                              {
                                  tag = new Tag()
                                  {
                                      Name = item.ToLower()
                                  };

                                  context.Tags.Add(tag);
                                  context.SaveChanges();
                              }

                              tags.Add(tag);
                          }
                      }

                      foreach (var item in titleWords)
                      {
                          var tag = context.Tags.FirstOrDefault(t => t.Name == item.ToLower());
                          if (tag == null)
                          {
                              tag = new Tag()
                              {
                                  Name = item.ToLower()
                              };

                              context.Tags.Add(tag);
                              context.SaveChanges();
                          }

                          tags.Add(tag);
                      }

                      var post = new Post()
                      {
                          Title = model.Title,
                          Text = model.Text,
                          PostDate = DateTime.Now,
                          User = user,
                          Tags = tags
                      };

                      context.Posts.Add(post);
                      context.SaveChanges();

                      var createdModel = new PostCreatedModel()
                      {
                          Id = post.Id,
                          Title = post.Title
                      };

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.Created,
                                          createdModel);
                      return response;
                  }
              });

            return responseMsg;
        }
예제 #19
0
        public HttpResponseMessage AddComent(CreateCommentModel model, int postId,
             [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = ExceptionHandler(
            () =>
            {
                User user;
                var context = new BlogSystemContext();
                using (context)
                {
                    user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid username or password");
                    }
                }

                var selectedPost = this.postRepository.Get(postId);

                var newComment = new Comment
                {
                    Content = model.Text,
                    CommentDate = DateTime.Now,
                    Post = selectedPost,
                    //User = user,
                };

                selectedPost.Comments.Add(newComment);
                this.postRepository.Update(selectedPost, postId, true);

                var response = this.Request.CreateResponse(HttpStatusCode.OK);

                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage PostCreatePost(PostModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                this.ValidateSessionKey(sessionKey);

                if (model == null)
                {
                    throw new InvalidOperationException("Model cannot be null");
                }

                var context = new BlogSystemContext();
                using (context)
                {
                    User user = context.Users.Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();
                    if (user == null)
                    {
                        throw new InvalidOperationException("Invalid sessionkey");
                    }

                    var titleTags = model.Title.Split(
                        new char[] { '.', ',', ';', '!', '?', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var newTags = new HashSet<Tag>();
                    foreach (var tagName in titleTags)
                    {
                        Tag newtag = new Tag()
                        {
                            Name = tagName.ToLower()
                        };

                        if (!newTags.Contains(newtag))
                        {
                            newTags.Add(newtag);
                        }
                    }

                    foreach (var tagName in model.Tags)
                    {
                        Tag newtag = new Tag()
                        {
                            Name = tagName.ToLower()
                        };

                        if (!newTags.Contains(newtag))
                        {
                            newTags.Add(newtag);
                        }
                    }

                    var newDbTags = new HashSet<Tag>();
                    foreach (var tag in newTags)
                    {
                        var tagDb = context.Tags.FirstOrDefault(tg => tg.Name == tag.Name);
                        if (tagDb == null)
                        {
                            newDbTags.Add(tag);
                        }
                        else
                        {
                            newDbTags.Add(tagDb);
                        }
                    }

                    var post = new Post()
                    {
                        Title = model.Title,
                        Text = model.Text,
                        DatePosted = DateTime.Now,
                        User = user,
                        Tags = newDbTags
                    };

                    user.Posts.Add(post);
                    context.SaveChanges();

                    var responseModel = new CreatePostResponseModel()
                    {
                        Title = post.Title,
                        Id = post.Id
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);

                    return response;
                }
            });

            return responseMsg;
        }
예제 #21
0
 public PostsController()
 {
     var dbContext = new BlogSystemContext();
     this.postRepository = new DbPostRepository(dbContext);
 }
예제 #22
0
 public TagsController()
 {
     var dbContext = new BlogSystemContext();
     this.tagRepository = new DbTagRepository(dbContext);
 }