예제 #1
0
        public ActionResult Create([Deserialize] postVars model, string PrC, string PrS, string post, string filesToUpload)
        {
            //var up = EncryptionHelper.Unprotect(UP);
            //var cp = EncryptionHelper.Unprotect(CP);
            //var sp = EncryptionHelper.Unprotect(SP);
            var prc = EncryptionHelper.Unprotect(PrC);
            var prs = EncryptionHelper.Unprotect(PrS);

            //int?[] receivers = { UP, CP, SP };
            //int?[] senders = { PrC, PrS };

            int?[] senders = { prc, prs };

            if (ModelState.IsValid && senders.Count(x => x != null) <= 1)
            {
                Post   postEntry  = new Post();
                int    senderUser = WebSecurity.CurrentUserId;
                int?   sndr       = null;
                int    recvr      = 0;
                string notifData  = null;

                postEntry.insertDate   = DateTime.UtcNow;
                postEntry.posterUserID = senderUser;
                postEntry.post         = post;

                var notiftype = NotificationType.Post;

                //send to userProfile
                if (model.UId != null)
                {
                    var receiverUser = unitOfWOrk.ActiveUserRepository.GetByID(model.UId);
                    if (receiverUser.Friends.Any(a => a.friendID == senderUser) ||
                        receiverUser.FriendsImIn.Any(a => a.userID == senderUser) ||
                        receiverUser.UserId == senderUser)
                    {
                        postEntry.postedUserID = model.UId;
                        recvr = (int)model.UId;
                    }
                    else
                    {
                        throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                    }
                }
                else if (model.CoId != null)
                {
                    if (unitOfWOrk.NotExpiredCompanyRepository.GetByID(model.CoId).Followers.Any(a => a.followerUserID == senderUser))
                    {
                        postEntry.postedCoID = model.CoId;
                        notiftype            = NotificationType.PostCo;
                        recvr = (int)model.CoId;
                    }
                    else
                    {
                        throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                    }
                }
                else if (model.StId != null)
                {
                    if (unitOfWOrk.StoreNotExpiredRepository.GetByID(model.StId).Followers.Any(a => a.followerUserID == senderUser))
                    {
                        postEntry.postedStoreID = model.StId;
                        notiftype = NotificationType.PostSt;
                        recvr     = (int)model.StId;
                    }
                    else
                    {
                        throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                    }
                }
                else
                {
                    throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                }

                //send as company
                if (prc != null)
                {
                    if (unitOfWOrk.NotExpiredCompanyRepository.GetByID(prc).Admins.Any(a => a.UserId == senderUser))
                    {
                        postEntry.posterCoID = prc;
                        sndr      = prc;
                        notifData = JsonConvert.SerializeObject(new PosterCoNotif {
                            coid = prc
                        });
                    }
                    else
                    {
                        throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                    }
                }
                //send as Store
                else if (prs != null)
                {
                    if (unitOfWOrk.StoreNotExpiredRepository.GetByID(prs).Admins.Any(a => a.UserId == senderUser))
                    {
                        postEntry.posterStoreID = prs;
                        sndr      = prs;
                        notifData = JsonConvert.SerializeObject(new PosterCoNotif {
                            stid = prs
                        });
                    }
                    else
                    {
                        throw new JsonCustomException(Resource.ControllerError.ajaxErrorPostInsert);
                    }
                }


                unitOfWOrk.PostRepository.Insert(postEntry);
                unitOfWOrk.Save();

                NotificationHelper.NotificationInsert(notiftype,
                                                      elemId: postEntry.postID,
                                                      recId: recvr,
                                                      senderCoStoreId: sndr,
                                                      data: notifData
                                                      );
                FeedHelper.FeedInsert(FeedType.Post,
                                      postEntry.postID,
                                      WebSecurity.CurrentUserId);

                if (!String.IsNullOrWhiteSpace(filesToUpload))
                {
                    var fileUploadResult = UploadHelper.UpdateUploadedFiles(filesToUpload, String.Empty, "Post");
                    postEntry.image    = fileUploadResult.ImagesToUpload;
                    postEntry.document = fileUploadResult.DocsToUpload;
                    unitOfWOrk.PostRepository.Update(postEntry);
                }
                unitOfWOrk.Save();
                UnitOfWork newpost = new UnitOfWork();
                var        npost   = newpost.PostRepository.GetByID(postEntry.postID);
                return(Json(new { Success = true, Result = RenderPartialViewHelper.RenderPartialView(this, "PostPartial", npost) }));
            }
            return(Json(new { Success = false, Message = ControllerError.ajaxErrorPostInsert }));
        }
예제 #2
0
 public ActionResult Create(postVars model)
 {
     return(PartialView(new PostViewModel {
         prams = model
     }));
 }