Ejemplo n.º 1
0
        public async Task <FilePathResult> SubmitPostAndAttachment(Models.PostAndPostAttachment post)
        {
            var file = post.AttachmentFile;
            var item = post.PostAttachment;

            if (post.Post == null)
            {
                return(new FilePathResult()
                {
                    IsOk = false,
                    ErrorMessage = "No post info"
                });
            }
            if (item == null)
            {
                return(new FilePathResult()
                {
                    IsOk = false,
                    ErrorMessage = "No post attachment info"
                });
            }

            if (post.IsFile && (post.AttachmentFile == null || post.AttachmentFile.FileData == null))
            {
                return(new FilePathResult()
                {
                    IsOk = false,
                    ErrorMessage = "No file info"
                });
            }
            Common.Model.Post itemDb = null;
            var dbUser = DbUser;

            using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                using (PostRepository _postRepository = new PostRepository(Context, dbUser, null))
                    using (UserContactRepository _userContactRepository = new UserContactRepository(Context, dbUser, null))
                        using (PostAttachmentRepostitory _postAttachmentRepostitory = new PostAttachmentRepostitory(Context, dbUser, null))
                        {
                            {
                                try
                                {
                                    if (post.Post.Id == Guid.Empty)
                                    {
                                        //if (item.SelectedContact != null)
                                        //{
                                        //    var contactCircle = _userContactRepository.GetCircleByUsersPair(dbUser,
                                        //        item.SelectedContact.Id);
                                        //    if (contactCircle != null)
                                        //    {
                                        //        item.SelectedCircle = new Circle() { Id = contactCircle.Id };
                                        //    }
                                        //}

                                        itemDb = new Common.Model.Post()
                                        {
                                            Title    = post.Post.Title,
                                            Type     = post.Post.Type,
                                            CircleId = post.Post.CircleId ?? post.Post.SelectedCircle?.Id,
                                            UserId   = post.Post.UserId ?? post.Post.SelectedContact?.Id,
                                            ParentId = post.Post.ParentId
                                                       //LastUpdatedDate = DateTime.Now,
                                                       //LastUpdatedUserId = dbUser,
                                        };
                                        _postRepository.Create(itemDb);
                                    }
                                    else
                                    {
                                        itemDb       = _postRepository.GetById(post.Post.Id);
                                        itemDb.Title = post.Post.Title;
                                        itemDb.Type  = post.Post.Type;
                                        _postRepository.Update(itemDb);
                                    }

                                    Context.SaveChanges();

                                    /*Add Attachment*/

                                    Common.Model.PostAttachment itemAttachmentDb = null;
                                    try
                                    {
                                        itemAttachmentDb = new Common.Model.PostAttachment()
                                        {
                                            PostId           = itemDb.Id,
                                            Text             = item.Text,
                                            Type             = item.Type.HasValue ? item.Type.Value : (int)ChatType.Text,
                                            FilePath         = item.FilePath,
                                            FileDuration     = item.FileDuration,
                                            IsMainAttachment = item.IsMainAttachment
                                                               //LastUpdatedDate = DateTime.Now,
                                                               //LastUpdatedUserId = dbUser,
                                        };
                                        _postAttachmentRepostitory.Create(itemAttachmentDb);
                                        Context.SaveChanges();
                                    }
                                    catch (Exception e)
                                    {
                                        LogHelper.WriteError(e);
                                        return(new FilePathResult()
                                        {
                                            IsOk = false,
                                            ErrorMessage = e.ToString()
                                        });
                                    }

                                    if (itemDb != null)
                                    {
                                        if (itemDb.Type == (int)PostTypes.Prompt)
                                        {
                                            if (itemDb.UserId.HasValue)
                                            {
                                                Utilities.AddNotification(dbUser, itemDb.UserId.Value, itemDb.Id,
                                                                          (int)NotificationTypes.NewPromptInCircle,
                                                                          string.Format("You've got new Prompt"));
                                            }
                                            else if (itemDb.CircleId.HasValue)
                                            {
                                                var usersOfCircle = _userContactRepository.GetAllCircleUsers(itemDb.CircleId.Value);
                                                if (usersOfCircle != null)
                                                {
                                                    foreach (var userProfile in usersOfCircle)
                                                    {
                                                        Utilities.AddNotification(dbUser, userProfile.Id, itemDb.Id,
                                                                                  (int)NotificationTypes.NewPromptInCircle,
                                                                                  string.Format("You've got new Prompt"));
                                                    }
                                                }
                                            }
                                        }
                                        else if (itemDb.Type == (int)PostTypes.Reply && itemDb.ParentId.HasValue)
                                        {
                                            var parentPost = _postRepository.GetById(itemDb.ParentId.Value);
                                            if (parentPost != null && parentPost.CreatorId != dbUser)
                                            {
                                                Utilities.AddNotification(dbUser, parentPost.CreatorId, parentPost.Id,
                                                                          (int)NotificationTypes.Reply,
                                                                          string.Format("You've got new Reply on you post:{0}", parentPost.Title));
                                            }
                                        }
                                        else if (itemDb.Type == (int)PostTypes.TellMeMore && itemDb.ParentId.HasValue)
                                        {
                                            var parentReplyPost = _postRepository.GetById(itemDb.ParentId.Value);
                                            if (parentReplyPost != null && parentReplyPost.ParentId.HasValue)
                                            {
                                                var parentPost = _postRepository.GetById(parentReplyPost.ParentId.Value);
                                                if (parentPost != null)
                                                {
                                                    Utilities.AddNotification(dbUser, parentReplyPost.CreatorId, parentPost.Id,
                                                                              (int)NotificationTypes.TellMeMore,
                                                                              string.Format("You've got TellMeMore on you reply in post:{0}",
                                                                                            parentPost.Title));
                                                }
                                            }
                                        }
                                    }

                                    if (!post.IsFile)
                                    {
                                        transactionScope.Complete();
                                        return(new FilePathResult()
                                        {
                                            PostId = itemDb.Id,
                                            Id = itemAttachmentDb.Id,
                                            IsOk = true
                                        });
                                    }

                                    /*Add file here*/
                                    var fileUnicName  = Guid.NewGuid();
                                    var fileExtention = Path.GetExtension(file.FileName);
                                    var fileNewName   = string.Format("{0}{1}", fileUnicName, fileExtention);
                                    Common.Code.Utilities.UploadBlob(Common.Code.Utilities.GetAzureFolderByFileType(file.AttachmentType), fileNewName, file.FileData);
                                    //ConfigurationManager.AppSettings["RootPathToFiles"];
                                    //var request = ;

                                    try
                                    {
                                        itemAttachmentDb.FilePath = fileNewName;
                                        Context.SaveChanges();

                                        string thumbnailUrl = null;

                                        if (file.AttachmentType == (int)ChatType.Video)
                                        {
                                            var thumbNailFile = string.Format("Thumb_{0}.png", fileUnicName);
                                            Common.Code.Utilities.UploadBlob(Common.Code.Utilities.GetAzureFolderByFileType(file.AttachmentType), thumbNailFile, file.ThumbnailFileData);
                                            thumbnailUrl = Request.GetFileUrl((int)FileType.Video, thumbNailFile);
                                        }



                                        transactionScope.Complete();
                                        return(new FilePathResult()
                                        {
                                            PostId = itemDb.Id,
                                            Id = itemAttachmentDb.Id,
                                            IsOk = true,
                                            FileName = fileNewName,
                                            FileUrl = Request.GetFileUrl(file.AttachmentType, fileNewName),
                                            ThumbnailUrl = thumbnailUrl
                                        });

                                        //return new FilePathResult()
                                        //{
                                        //    IsOk = false,
                                        //    ErrorMessage = "Error On Save"
                                        //};
                                    }
                                    catch (Exception e)
                                    {
                                        LogHelper.WriteError(e);
                                        //Console.WriteLine(e.Message);
                                        return(new FilePathResult()
                                        {
                                            IsOk = false,
                                            ErrorMessage = "Errow while uploading file"
                                        });
                                    }
                                }
                                catch (Exception e)
                                {
                                    LogHelper.WriteError(e);
                                    return(new FilePathResult()
                                    {
                                        IsOk = false,
                                        ErrorMessage = e.ToString()
                                    });
                                }
                            }
                        }
        }
Ejemplo n.º 2
0
        public async Task <IdResult> Submit(Models.Post item)
        {
            if (item == null)
            {
                return(new IdResult()
                {
                    IsOk = false,
                    ErrorMessage = "No post info"
                });
            }

            Common.Model.Post itemDb = null;
            var dbUser = DbUser;

            using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                using (PostRepository _postRepository = new PostRepository(Context, dbUser, null))
                    using (UserContactRepository _userContactRepository = new UserContactRepository(Context, dbUser, null))
                    {
                        {
                            try
                            {
                                if (item.Id == Guid.Empty)
                                {
                                    //if (item.SelectedContact != null)
                                    //{
                                    //    var contactCircle = _userContactRepository.GetCircleByUsersPair(dbUser,
                                    //        item.SelectedContact.Id);
                                    //    if (contactCircle != null)
                                    //    {
                                    //        item.SelectedCircle = new Circle() { Id = contactCircle.Id };
                                    //    }
                                    //}

                                    itemDb = new Common.Model.Post()
                                    {
                                        Title    = item.Title,
                                        Type     = item.Type,
                                        CircleId = item.CircleId ?? item.SelectedCircle?.Id,
                                        UserId   = item.UserId ?? item.SelectedContact?.Id,
                                        ParentId = item.ParentId
                                                   //LastUpdatedDate = DateTime.Now,
                                                   //LastUpdatedUserId = dbUser,
                                    };
                                    _postRepository.Create(itemDb);
                                }
                                else
                                {
                                    itemDb       = _postRepository.GetById(item.Id);
                                    itemDb.Title = item.Title;
                                    itemDb.Type  = item.Type;
                                    _postRepository.Update(itemDb);
                                }

                                Context.SaveChanges();
                                if (itemDb != null)
                                {
                                    if (itemDb.Type == (int)PostTypes.Prompt)
                                    {
                                        if (itemDb.UserId.HasValue)
                                        {
                                            Utilities.AddNotification(dbUser, itemDb.UserId.Value, itemDb.Id, (int)NotificationTypes.NewPromptInCircle,
                                                                      string.Format("You've got new Prompt"));
                                        }
                                        else if (itemDb.CircleId.HasValue)
                                        {
                                            var usersOfCircle = _userContactRepository.GetAllCircleUsers(itemDb.CircleId.Value);
                                            if (usersOfCircle != null)
                                            {
                                                foreach (var userProfile in usersOfCircle)
                                                {
                                                    Utilities.AddNotification(dbUser, userProfile.Id, itemDb.Id, (int)NotificationTypes.NewPromptInCircle, string.Format("You've got new Prompt"));
                                                }
                                            }
                                        }
                                    }
                                    else if (itemDb.Type == (int)PostTypes.Reply && itemDb.ParentId.HasValue)
                                    {
                                        var parentPost = _postRepository.GetById(itemDb.ParentId.Value);
                                        if (parentPost != null)
                                        {
                                            Utilities.AddNotification(dbUser, parentPost.CreatorId, parentPost.Id, (int)NotificationTypes.Reply,
                                                                      string.Format("You've got new Reply on you post:{0}", parentPost.Title));
                                        }
                                    }
                                    else if (itemDb.Type == (int)PostTypes.TellMeMore && itemDb.ParentId.HasValue)
                                    {
                                        var parentReplyPost = _postRepository.GetById(itemDb.ParentId.Value);
                                        if (parentReplyPost != null && parentReplyPost.ParentId.HasValue)
                                        {
                                            var parentPost = _postRepository.GetById(parentReplyPost.ParentId.Value);
                                            if (parentPost != null)
                                            {
                                                Utilities.AddNotification(dbUser, parentReplyPost.CreatorId, parentPost.Id, (int)NotificationTypes.TellMeMore,
                                                                          string.Format("You've got TellMeMore on you reply in post:{0}", parentPost.Title));
                                            }
                                        }
                                    }

                                    transactionScope.Complete();
                                    return(new IdResult()
                                    {
                                        IsOk = true,
                                        Id = itemDb.Id
                                    });
                                }
                                return(new IdResult()
                                {
                                    IsOk = false,
                                    ErrorMessage = "Error On Save"
                                });
                            }
                            catch (Exception e)
                            {
                                LogHelper.WriteError(e);
                                return(new IdResult()
                                {
                                    IsOk = false,
                                    ErrorMessage = e.ToString()
                                });
                            }
                        }
                    }
        }