Example #1
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;
        }
Example #2
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;
        }
Example #3
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;
        }
Example #4
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;
        }
Example #5
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;
        }