Esempio n. 1
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogDb();
                    using (context)
                    {
                        UserPersister.ValidateUsername(model.Username);
                        UserPersister.ValidateNickname(model.DisplayName);
                        UserPersister.ValidateAuthCode(model.AuthCode);
                        
                        var usernameToLower = model.Username.ToLower();
                        var nicknameToLower = model.DisplayName.ToLower();
                        
                        var user = context.Users.FirstOrDefault(
                            usr => usr.Username == usernameToLower ||
                                   usr.DisplayName.ToLower() == nicknameToLower);
                        
                        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 = UserPersister.GenerateSessionKey(user.UserId);
                        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 HttpResponseMessage CreateComment([ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey,
            int postId, [FromBody]CreateCommentModel comment)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogDb();
                    using (context)
                    {

                        UserPersister.ValidateSessionKey(sessionKey);

                        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.PostId == postId);
                        if (post == null)
                        {
                             throw new ArgumentNullException("Invalid post id");
                        }

                        Comment commToAdd = new Comment()  
                        { 
                             Content= comment.Text,
                             UserOfComment = user,
                             DateCreated = DateTime.Now
                        };

                        post.Comments.Add(commToAdd);
                        context.SaveChanges();

                        var response =
                           this.Request.CreateResponse(HttpStatusCode.OK);
                        return response;
                    }
                });
            return responseMsg;
        }
Esempio n. 3
0
        public HttpResponseMessage PutLogoutUser([ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogDb();
                    using (context)
                    {
                        UserPersister.ValidateSessionKey(sessionKey);

                        var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                        if (user == null)
                        {
                            throw new InvalidOperationException("Invalid session key.");
                        }
                        else
                        {
                            user.SessionKey = null;
                            context.SaveChanges();
                        }
                    }
                    
                    var response =
                        this.Request.CreateResponse(HttpStatusCode.OK);
                    
                    return response;
                });
            
            return responseMsg;
        }
Esempio n. 4
0
        public HttpResponseMessage PostCreatePost(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey ,
            [FromBody]CreatePostModel postToAdd)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    var context = new BlogDb();
                    using (context)
                    {
                        UserPersister.ValidateSessionKey(sessionKey);

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

                        if (user == null)
                        {
                            throw new InvalidOperationException("Invalid username or password");
                        }
                        List<Tag> tags = new List<Tag>();
                       
                        var wordsFromTitle = postToAdd.Title.Split(new char[] { ' ', ',' },
                            StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < wordsFromTitle.Length; i++)
                        {
                            tags.Add(new Tag() { TagName = wordsFromTitle[i].ToLower() });
                        }
                      
                        for (int i = 0; i < postToAdd.Tags.Length; i++)
                        {
                            tags.Add(new Tag() { TagName = postToAdd.Tags[i].ToLower() });
                        }

                        var post = new Post()
                        {
                            Title = postToAdd.Title,
                            DateCreated = DateTime.Now,
                            UserOfPost = user,
                            Text = postToAdd.Text,
                            Tags = tags
                        };
                       
                        context.Posts.Add(post);
                        context.SaveChanges();

                        CreatedPostModel postModel = new CreatedPostModel() { Id = post.PostId, Title = post.Title };
                      

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

            return responseMsg;
        }