Beispiel #1
0
        public int UpdatePost(Post post)
        {
            using (var conn = DbUtilities.Connection)
            {
                conn.Open();
                using (var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    int? res;
                    try
                    {
                        res = conn.Query<int>(QueryPostStore.UpdatePost,
                        new
                        {
                            idCategory = post.Category.IdCategory,
                            title = post.Title,
                            body = post.Body,
                            idStatus = post.Status,
                            timeStamp = post.TimeStamp,
                            timestampApprovation = post.TimeStampApprovation,
                            userId = post.UserId,
                            slugUrl = post.SlugUrl,
                            editCode = post.EditCode,
                            idPost = post.IdPost
                        }, transaction: transaction).SingleOrDefault();

                        res = res +
                              conn.Query<int>(QueryPostStore.UpdateLastUserEdit,
                                  new { lastedit = DateTime.Now, idPost = post.IdPost }, transaction: transaction).SingleOrDefault();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }

                    transaction.Commit();
                    return res.GetValueOrDefault();

                }
            }
        }
Beispiel #2
0
        public void UpdatePostWithAttachment(Post post)
        {
            using (var conn = DbUtilities.Connection)
            {
                conn.Open();
                using (var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    try
                    {

                       conn.Execute(QueryPostStore.UpdatePost,
                        new
                        {
                            idCategory = post.Category.IdCategory,
                            title = post.Title,
                            body = post.Body,
                            idStatus = post.Status,
                            timeStamp = post.TimeStamp,
                            timestampApprovation = post.TimeStampApprovation,
                            userId = post.UserId,
                            slugUrl = post.SlugUrl,
                            editCode = post.EditCode,
                            idPost = post.IdPost
                        },transaction: transaction);

                        conn.Execute(QueryPostStore.UpdateLastUserEdit,
                            new { lastedit = DateTime.Now, idPost = post.IdPost }, transaction: transaction);

                        foreach (var attach in post.Attachments)
                        {
                            conn.Execute(QueryPostStore.InsertPostAttachment,
                              new
                              {
                                  idPost = post.IdPost,
                                  fileName = attach.FileName,
                                  contentType = attach.ContentType,
                                  size = attach.Size,
                                  timestamp = DateTime.Now

                              }, transaction: transaction);
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }

                }
            }
        }
Beispiel #3
0
 public int InserPost(Post post)
 {
     using (var conn = DbUtilities.Connection)
     {
         return conn.Query<int>(QueryPostStore.InserPost + " " + QueryStore.LastInsertedId,
             new
             {
                 idCategory = post.Category.IdCategory,
                 title = post.Title,
                 body = post.Body,
                 slugUrl = post.SlugUrl,
                 timestamp = DateTime.Now,
                 userid = post.UserId,
                 editCode = post.EditCode
             }).SingleOrDefault();
     }
 }
Beispiel #4
0
        public int InserPostWithAttachment(Post post)
        {
            using (var conn = DbUtilities.Connection)
            {
                conn.Open();
                using (var transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    try
                    {
                        var identity = conn.Query<int>(QueryPostStore.InserPost + " " + QueryStore.LastInsertedId,
                            new
                            {
                                idCategory = post.Category.IdCategory,
                                title = post.Title,
                                body = post.Body,
                                slugUrl = post.SlugUrl,
                                timestamp = DateTime.Now,
                                userid = post.UserId,
                                editCode = post.EditCode
                            }, transaction: transaction).SingleOrDefault();

                        foreach (var attach in post.Attachments)
                        {
                            conn.Execute(QueryPostStore.InsertPostAttachment,
                              new
                              {
                                  idPost = identity,
                                  fileName = attach.FileName,
                                  contentType = attach.ContentType,
                                  size=attach.Size,
                                  timestamp=DateTime.Now

                              },transaction:transaction);
                        }

                        transaction.Commit();

                        return identity;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }

                }
            }
        }
Beispiel #5
0
        public BaseResponse AddPost(Post post)
        {
            post.CannotBeNull("post");

            BuildAttachAttachments(post);
            int res;

            var editCode = GetRandomString();
            var founded = false;

            if (_postRepository.GetPostByEditCode(editCode) != null)
            {
                while (!founded)
                {
                    editCode = GetRandomString();
                    if (_postRepository.GetPostByEditCode(editCode) == null)
                        founded = true;
                }
            }

            post.EditCode = editCode;
            post.SlugUrl = Slug.CreateSlug(true, post.Title);

            if (!post.HasAttachments)
                res = _postRepository.InserPost(post);
            else
            {
                res = _postRepository.InserPostWithAttachment(post);
                if (res > 0) _fileManager.MoveTempInFinalFolder(post.UserId,res.ToString());
            }

            bool success = res > 0;
            return new BaseResponse { Success = success, Message = res.ToString() };
        }
Beispiel #6
0
 private void BuildAttachAttachments(Post post)
 {
     post.CannotBeNull("post");
     var res =_fileManager.GetTempAttachMentsByUserId(post.UserId);
     post.Attachments =
         res.Select(x => new PostAttachments()
         {
             FileName = x.FileName,
             ContentType = x.ContentType,
             Size = x.Size,
         }).ToList();
 }
Beispiel #7
0
        public void UpdatePost(Post post)
        {
            post.CannotBeNull("post");
            BuildAttachAttachments(post);
            post.SlugUrl = Slug.CreateSlug(true, post.Title);

            if (!post.HasAttachments)
                _postRepository.UpdatePost(post);
            else
            {
                _postRepository.UpdatePostWithAttachment(post);
                _fileManager.MoveTempInFinalFolder(post.UserId, post.IdPost.ToString());
            }
        }
Beispiel #8
0
        private PostViewModelSingleContent FillinglePostViewModel(Post post)
        {
            int uservote = 0;
            var lastVote = GetLastVoteInfo();
            if (lastVote != null)
            {
                var found = lastVote.Find(x => x.Item1 == post.IdPost);
                uservote = found == null ? 0 : found.Item3;
            }

            return new PostViewModelSingleContent()
            {
                Body = post.Body,
                Title = post.Title,
                CategoryPost = post.Category.Description,
                IdPost = post.IdPost,
                AttachMenents = post.Attachments.Select(x=> new PostAttachMentViewModel(post.IdPost.ToString(CultureInfo.InvariantCulture)){
                    Id = x.Id,
                    Name = x.FileName,
                    Size = x.Size

                }).ToList(),
                CreationDate = post.TimeStamp,
                Votes = post.Votes,
                LastVote = uservote
            };
        }
Beispiel #9
0
        public ActionResult Insert(InsertPostViewModel postVm)
        {
            if (!ModelState.IsValid)
                return View(ViewsStore.Insert, FillInsertModel());

            var post = new Post
            {
                Body = postVm.Body,
                Category = new Category { IdCategory = postVm.IdCategory },
                Title = postVm.Title,
                UserId = CurrentUserId
            };

            var res = PostManager.AddPost(post);

            //return RedirectToAction(ActionsStore.EditCode, );
            return RedirectToRoute(RouteStore.EditPostView, new { idPost = res.Message });
        }