예제 #1
0
        public HttpResponseMessage PostRegisterUser([FromBody]UserDto userDto)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    if (userDto == null)
                    {
                        throw new ArgumentException("Cannot register an empty user.");
                    }

                    this.ValidateUsername(userDto.Username);
                    this.ValidateNickname(userDto.DisplayName);
                    this.ValidateAuthCode(userDto.AuthCode);

                    var db = new BlogContext();

                    var user = db.Users.Where(u =>
                        u.Username.ToLower() == userDto.Username.ToLower() || u.DisplayName.ToLower() == userDto.DisplayName.ToLower())
                        .FirstOrDefault();

                    if (user != null)
                    {
                        throw new ApplicationException("User exist.");
                    }

                    user = new User()
                    {
                        DisplayName = userDto.DisplayName,
                        Username = userDto.Username,
                        AuthCode = userDto.AuthCode
                    };

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

                    string sessionKey = this.GenerateSessionKey(user.Id);
                    user.SessionKey = sessionKey;
                    db.SaveChanges();

                    var userLogedIn = new UserLogedDto()
                    {
                        DisplayName = user.DisplayName,
                        SessionKey = sessionKey
                    };

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

                    return response;
                });

            return responseMsg;
        }
예제 #2
0
        public HttpResponseMessage PutComment([FromUri]string sessionKey, [FromUri]int postId, [FromBody]CommentDto commentDto)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    if (sessionKey.Length != SessionKeyLength)
                    {
                        throw new ArgumentException("Invalid session key.");
                    }

                    var db = new BlogContext();

                    var user = db.Users.Where(u => u.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new ApplicationException("Invalin session. Try to login.");
                    }

                    if (commentDto == null)
                    {
                        throw new ArgumentException("Comment is empty.");
                    }

                    if (commentDto.Text == null)
                    {
                        throw new ArgumentException("Comment text is empty.");
                    }

                    var postById = db.Posts.Where(p => p.Id == postId).FirstOrDefault();

                    if (postById == null)
                    {
                        throw new ApplicationException("Invalin post ID.");
                    }

                    Comment comment = new Comment() { Content = commentDto.Text, CommentDate = DateTime.Now, User = user };

                    postById.Comments.Add(comment);
                    db.SaveChanges();

                    var response = new HttpResponseMessage(HttpStatusCode.OK);
                    return response;
                });

            return responseMsg;
        }
예제 #3
0
        public HttpResponseMessage Post([FromUri]string sessionKey, [FromBody]PostDto postDto)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var db = new BlogContext();

                    if (sessionKey.Length != SessionKeyLength)
                    {
                        throw new ArgumentException("Invalid session key.");
                    }

                    var user = db.Users.Where(u => u.SessionKey == sessionKey).FirstOrDefault();

                    if (user == null)
                    {
                        throw new ApplicationException("Invalin session. Try to login.");
                    }

                    if (postDto == null)
                    {
                        throw new ArgumentException("Cannot create empty post.");
                    }

                    if (postDto.Title == null)
                    {
                        throw new ArgumentException("Cannot create post without title");
                    }

                    if (postDto.Content == null)
                    {
                        throw new ArgumentException("Cannot create post without text");
                    }

                    var post = new Post() 
                    { 
                        Title = postDto.Title, 
                        Content = postDto.Content, 
                        PostDate = DateTime.Now
                    };

                    if (postDto.Tags != null)
                    {
                        foreach (var tag in postDto.Tags)
                        {
                            post.Tags.Add(new Tag() { TagText = tag });
                        }
                    }

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

                    foreach (var tag in tagsFromTitle)
                    {
                        post.Tags.Add(new Tag() { TagText = tag });
                    }

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

                    var responsPost = new PostCreateResponseDto() { Id = post.Id, Title = post.Title };

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

                    return response;
                });

            return responseMsg;
        }
예제 #4
0
        public HttpResponseMessage LogoutUser([FromUri]string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
            () =>
            {
                if (sessionKey.Length != SessionKeyLength)
                {
                    throw new ArgumentException("Invalid session key length.");
                }

                var db = new BlogContext();

                var user = db.Users.Where(u => u.SessionKey == sessionKey).FirstOrDefault();

                if (user == null)
                {
                    throw new ArgumentException("Invalid session key.");
                }

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

                var response = new HttpResponseMessage(HttpStatusCode.OK);
                return response;
            });

            return responseMsg;
        }