Esempio n. 1
0
        private void ValidateSessionKey(string sessionKey, BlogContext context)
        {
            var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

            if (user == null)
            {
                throw new InvalidOperationException("Invalid SessionKey!");
            }
        }
Esempio n. 2
0
        public HttpResponseMessage GetPosts(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

                 return this.Request.CreateResponse(HttpStatusCode.OK, context.Posts.OrderByDescending(p => p.Date));
             });

            return responseMsg;
        }
Esempio n. 3
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();
                    using (context)
                    {
                        this.ValidateStr(model.Username, ValidUsernameCharacters, "User");
                        this.ValidateStr(model.Nickname, ValidNicknameCharacters, "Nickname");
                        this.ValidateAuthCode(model.AuthCode);
                        var usernameToLower = model.Username.ToLower();
                        var nicknameToLower = model.Nickname.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.Nickname,
                            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.AuthCode
                        };

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

            return responseMsg;
        }
Esempio n. 4
0
        public HttpResponseMessage GetPostsById(int id, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

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

                 return this.Request.CreateResponse(HttpStatusCode.OK, post);
             });

            return responseMsg;
        }
Esempio n. 5
0
        public HttpResponseMessage GetPosts(int page, int count, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

                 var postsOnPage = context.Posts
                            .OrderByDescending(p => p.Date)
                            .Skip(page * count)
                            .Take(count);

                 return this.Request.CreateResponse(HttpStatusCode.OK, postsOnPage);
             });

            return responseMsg;
        }
Esempio n. 6
0
        public HttpResponseMessage GetPostsByKeyword(string keyword, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

                 var matchingPosts = (from p in context.Posts
                                      where p.Title.Contains(keyword)
                                      select p)
                                      .OrderBy(p => p.Date)
                                      .AsQueryable();

                 return this.Request.CreateResponse(HttpStatusCode.OK, matchingPosts);
             });

            return responseMsg;
        }
Esempio n. 7
0
        public HttpResponseMessage GetPostsForTag(int tagId, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

                 var allPosts = (from t in context.Tags
                                 where t.Id == tagId
                                 select t)
                                .FirstOrDefault()
                                .Posts
                                .OrderByDescending(p => p.Date)
                                .AsQueryable();

                 return this.Request.CreateResponse(HttpStatusCode.OK, allPosts);
             });

            return responseMsg;
        }
Esempio n. 8
0
        public HttpResponseMessage GetTags(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);

                 var allTags = (from t in context.Tags
                                select new
                                {
                                    t.Id,
                                    t.Name,
                                    t.Posts.Count
                                })
                               .OrderByDescending(t => t.Id)
                               .AsQueryable();

                 return this.Request.CreateResponse(HttpStatusCode.OK, allTags);
             });

            return responseMsg;
        }
Esempio n. 9
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogContext();
                  using (context)
                  {
                      this.ValidateStr(model.Username, ValidUsernameCharacters, "User");
                      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 LoggedUserModel()
                      {
                          Nickname = user.Nickname,
                          SessionKey = user.SessionKey
                      };

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

            return responseMsg;
        }
Esempio n. 10
0
        public HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new BlogContext();

                  using (context)
                  {
                      ValidateSessionKey(sessionKey);
                      var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);

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

                      user.SessionKey = null;
                      context.Users.Attach(user);
                      var entity = context.Entry(user);
                      entity.Property(e => e.SessionKey).IsModified = true;
                      context.SaveChanges();
                  }

                  return this.Request.CreateResponse(HttpStatusCode.OK);
              });

            return responseMsg;
        }
Esempio n. 11
0
        public HttpResponseMessage PutComment(int postId, [FromBody]Comment comment, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
             () =>
             {
                 var context = new BlogContext();
                 ValidateSessionKey(sessionKey, context);
                 using (context)
                 {
                     var currPost = context.Posts.FirstOrDefault(p => p.Id == postId);

                     if (currPost == null)
                     {
                         throw new InvalidOperationException("Post does not exist!");
                     }

                     Comment newComment = new Comment()
                     {
                         Id = comment.Id,
                         Date = comment.Date,
                         Post = comment.Post,
                         Text = comment.Text,
                         User = comment.User
                     };

                     currPost.Comments.Add(newComment);
                     context.Posts.Attach(currPost);
                     context.Entry(currPost).CurrentValues.SetValues(currPost);
                     context.SaveChanges();
                 }

                 return this.Request.CreateResponse(HttpStatusCode.Created);
             });

            return responseMsg;
        }
Esempio n. 12
0
        public HttpResponseMessage PostPosts(Post post, 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("Invalid SessionKey!");
                     }

                     var newPost = new Post()
                     {
                         Id = post.Id,
                         Date = post.Date,
                         Title = post.Title,
                         Text = post.Text,
                         Tags = post.Tags,
                         Comments = post.Comments,
                         User = user
                     };

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

                     PostModel response = new PostModel()
                     {
                         Title = newPost.Title,
                         Id = newPost.Id
                     };

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

            return responseMsg;
        }