Ejemplo n.º 1
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();
                    using (context)
                    {
                        this.ValidateUsername(model.Username);
                        this.ValidateNickname(model.DisplayName);
                        this.ValidateAuthCode(model.AuthCode);
                        var usernameToLower = model.Username.ToLower();
                        var nicknameToLower = model.DisplayName.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.DisplayName,
                            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.SessionKey
                        };

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

            return responseMsg;
        }
Ejemplo n.º 2
0
 private static void GetTag(BlogContext context, string pt, List<Tag> result)
 {
     if (!string.IsNullOrEmpty(pt))
     {
         var tag = context.Tags.FirstOrDefault(t => t.Name == pt);
         if (tag == null)
         {
             tag = new Tag()
             {
                 Name = pt.ToLower()
             };
             context.Tags.Add(tag);
             context.SaveChanges();
         }
         result.Add(tag);
     }
 }
Ejemplo n.º 3
0
        //POST api/posts/create
        public HttpResponseMessage PostCreate(PostModel post, string sessionKey)
        {
            var context = new BlogContext();
            var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () =>
            {
                HttpResponseMessage response;
                var newPost = new Post();
                using (var tran = new TransactionScope())
                {
                    newPost.Title = post.Title;
                    newPost.Text = post.Text;
                    newPost.PostDate = DateTime.Now;
                    newPost.User = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);

                    var tags = Helpers.GetTags(post, context);
                    tags.ForEach(t => newPost.Tags.Add(t));
                
                    context.Posts.Add(newPost);
                    context.SaveChanges();
                    tran.Complete();
                }

                if (newPost.Id > 0)
                {
                    response = this.Request.CreateResponse(HttpStatusCode.Created, new ResponsePostModel()
                    {
                        Id = newPost.Id,
                        Title = newPost.Title
                    });
                }
                else
                {
                    response = this.Request.CreateResponse(HttpStatusCode.InternalServerError);
                }

                return response;
            });

            return responseMsg;
        }
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();
                    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("Users doesn't exists");
                        }

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

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

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

                        return response;
                    }
                });
            return responseMsg;
        }
Ejemplo n.º 5
0
        //PUT api/posts/{postId}/comment
        public HttpResponseMessage PutComment(int postId, CommentModel comment, string sessionKey)
        {
            var context = new BlogContext();
            var responseMsg = this.PerformOperationAndHandleExceptionsWithSessionKey(sessionKey, context, () =>
            {
                if (comment != null && string.IsNullOrEmpty(comment.Text))
                {
                    throw new InvalidOperationException("Comment text cannot be empty");
                }

                var post = context.Posts.FirstOrDefault(p => p.Id == postId);
                if (post != null)
                {
                    post.Comments.Add(new Comment()
                    {
                        Text = comment.Text,
                        User = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey),
                        PostDate = DateTime.Now
                    });
                    context.SaveChanges();
                }
                else
                {
                    throw new InvalidOperationException("Post with id " + postId + " do not exist");
                }

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

            return responseMsg;
        }
        public HttpResponseMessage PutLogoutUser(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("Users doesn't exists");
                        }

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

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

                        return response;
                    }
                });
            return responseMsg;
        }
Ejemplo n.º 7
0
        public HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                ValidateSessionKey(sessionKey);

                var context = new BlogContext();
                using (context)
                {
                    var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (user != null)
                    {
                        user.SessionKey = null;
                        context.SaveChanges();
                    }
                }

                var response = this.Request.CreateResponse(HttpStatusCode.OK);
                return response;
            });
            return responseMsg;
        }
Ejemplo n.º 8
0
        public HttpResponseMessage PutLogoutUser()
        {
            var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey");

            var responseMessage = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();

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

                        if (user.SessionKey == null)
                        {
                            throw new ArgumentNullException("User is already logout!");
                        }
                        user.SessionKey = null;

                        context.SaveChanges();

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

        }
Ejemplo n.º 9
0
        public HttpResponseMessage PutCommentOnPost(int postId, CreateCommentModel model)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
            {
                var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey");
                var dbContext = new BlogContext();
                // dbContext.Configuration.ProxyCreationEnabled = false;

                using (dbContext)
                {
                    var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Users must be logged when create a new comment!");
                    }

                    var post = dbContext.Posts.FirstOrDefault(pst => pst.Id == postId);
                                     
                                        
                    var newComment = new Comment()
                    {
                        
                        Text = model.Text,
                        CommentDate = DateTime.Now,
                        User = user,
                        Post=post
                    };

                    dbContext.Comments.Add(newComment);
                    dbContext.SaveChanges();

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

        }
Ejemplo n.º 10
0
        public HttpResponseMessage PostPost(CreatePostModel model)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
            {
                var sessionKey = this.GetHeaderValue(Request.Headers, "sessionKey");
                var dbContext = new BlogContext();
               // dbContext.Configuration.ProxyCreationEnabled = false;

                using (dbContext)
                {
                    var user = dbContext.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                    if (user == null)
                    {
                        throw new ArgumentException("Users must be logged when create a new post!");
                    }

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

                    if (!(model.Tags==null))
                    {                        
                        foreach (var tagItem in model.Tags)
                        {
                            var tagEntity = new Tag()
                            {
                                Text = tagItem.ToLower()
                            };

                            newTags.Add(tagEntity);
                        }
                    }

                    //split title and add to the tags
                    string[] wordsIntitle = model.Title.Split(new char[] { ' ', ',','.','!','?' });

                    foreach (var word in wordsIntitle)
                    {
                        if (!(word == string.Empty))
                        {
                            var tagEntity = new Tag()
                                {
                                    Text = word.ToLower().Trim()
                                };

                            newTags.Add(tagEntity);
                        }
                    }
                    
                    var newPost = new Post()
                    {
                        Title = model.Title,
                        Text = model.Text,
                        PostDate = DateTime.Now,                        
                        User = user,
                        Tags=newTags
                    };
                    
                    dbContext.Posts.Add(newPost);
                    dbContext.SaveChanges();

                    var postCreatedModel = new CreatedPostModel()
                    {
                        Title = newPost.Title,
                        Id = newPost.Id
                    };
                    var ret = Request.CreateResponse(HttpStatusCode.Created, postCreatedModel);

                   // var ret = Request.CreateResponse(HttpStatusCode.Created);
                    return ret;
                }
            });
            return responseMessage;
        }
        public HttpResponseMessage PostNewPost(PostModel model, string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogContext();

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

                        if (user == null)
                        {
                            throw new InvalidOperationException("Users doesn't exists");
                        }

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

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

                        Post post = new Post();
                        post.Title = model.Title;
                        post.Text = model.Text;
                        post.PostDate = DateTime.Now;
                        post.PostedBy = user;

                        foreach (var tag in model.Tags)
                        {
                            Tag currentTag = CreateOrLoadTag(context, tag.ToLower());
                            post.Tags.Add(currentTag);
                        }

                        string[] titleTags = Regex.Split(model.Title, @"[,!\. ;?']+");

                        foreach (var titleTagName in titleTags)
                        {
                            Tag titleTag = CreateOrLoadTag(context, titleTagName.ToLower());
                            post.Tags.Add(titleTag);
                        }

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

                        var response = this.Request.CreateResponse(HttpStatusCode.Created, new
                        {
                            Title = post.Title,
                            Id = post.Id
                        });

                        return response;
                    }
                });

            return responseMsg;
        }
        private static Tag CreateOrLoadTag(BlogContext context, string tagName)
        {
            Tag existingTag =
                (from t in context.Tags
                 where t.Text == tagName
                 select t).FirstOrDefault();

            if (existingTag != null)
            {
                return existingTag;
            }

            Tag newTag = new Tag();
            newTag.Text = tagName;
            context.Tags.Add(newTag);

            context.SaveChanges();

            return newTag;
        }
        public HttpResponseMessage PutNewComment([FromUri] int postId, [FromUri] string sessionKey, [FromBody] CommentModel model)
        {
            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("Users doesn't exists");
                        }

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

                        if (post == null)
                        {
                            throw new InvalidOperationException("This post doesn't exists");
                        }

                        Comment newComment = new Comment();
                        newComment.Text = model.Text;
                        newComment.PostDate = DateTime.Now;
                        newComment.CommentedBy = user;
                        newComment.Post = post;

                        post.Comments.Add(newComment);
                        context.Comments.Add(newComment);

                        context.SaveChanges();

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

                        return response;
                    }
                });
            return responseMsg;
        }