Example #1
0
        // 實現onCreateNewPost
        public override Task <CreateNewPostResult> CreateNewPostFun(NewPostInfo request, ServerCallContext context)
        {
            CreateNewPostResult ret = new CreateNewPostResult();

            ret.Result = 0;

            try
            {
                CreateNewPost rData = new CreateNewPost
                {
                    MemberID = request.MemberID,

                    Photo = request.Photo,

                    Content = request.Content
                };

                string sData = JsonConvert.SerializeObject(rData);

                JObject jsMain = new JObject();

                jsMain.Add("CmdID", (int)C2S_CmdID.emCreateNewPost);
                jsMain.Add("Data", JsonConvert.DeserializeObject <JObject>(sData));

                MsgInfo info = new MsgInfo(jsMain.ToString(), this.SendToClient);

                this.addQueue?.Invoke(info);

                ret.Result = 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message}");

                SaveLog($"[Error] gRpcImpl::CreateNewPostFun, Catch Error, Msg:{ex.Message}");
            }

            return(Task.FromResult(ret));
        }
Example #2
0
        /// <summary>
        /// 建立新貼文
        /// </summary>
        /// <param name="data"> 封包資料 </param>
        /// <returns> 結果 </returns>
        public string OnCreateNewPost(string data)
        {
            string ret = "";

            CreateNewPost packet = JsonConvert.DeserializeObject <CreateNewPost>(data);

            CreateNewPostResult rData = new CreateNewPostResult();

            PostInfo newPost = new PostInfo();

            try
            {
                UserAccount account = GetSql().Queryable <UserAccount>().With(SqlSugar.SqlWith.RowLock).Where(it => it.MemberID == packet.MemberID).Single();

                // 會員存在
                if (account != null)
                {
                    string dateTime = DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss");

                    string guidAll = Guid.NewGuid().ToString();

                    string[] guidList = guidAll.Split('-');

                    // 建立貼文
                    newPost.PostID     = "DbPst-" + guidList[0];        // 取GUID前8碼
                    newPost.MemberID   = packet.MemberID;
                    newPost.Photo      = packet.Photo != null ? packet.Photo : "[]";
                    newPost.Content    = packet.Content;
                    newPost.LikeList   = "[]";
                    newPost.CreateDate = dateTime;

                    // 設定DB 交易的起始點
                    GetSql().BeginTran();

                    // 寫入資料庫
                    if (GetSql().Insertable(newPost).With(SqlSugar.SqlWith.TabLockX).ExecuteCommand() > 0)
                    {
                        rData.Result = (int)CreateNewPostResult.ResultDefine.emResult_Success;

                        SaveLog($"[Info] MessageFcunction::OnCreateNewPost Create New Post Success");
                    }
                    else
                    {
                        rData.Result = (int)CreateNewPostResult.ResultDefine.emResult_Fail;

                        SaveLog($"[Warning] MessageFcunction::OnCreateNewPost Member:{packet.MemberID}'s Post Fail");
                    }
                }
                else
                {
                    rData.Result = (int)CreateNewPostResult.ResultDefine.emResult_Fail;

                    SaveLog($"[Info] MessageFcunction::OnCreateNewPost Can Not Find Member:{packet.MemberID}");
                }
            }
            catch (Exception ex)
            {
                rData.Result = (int)CreateNewPostResult.ResultDefine.emResult_Fail;

                SaveLog($"[Error] MessageFcunction::OnCreateNewPost Catch Error Msg:{ex.Message}");
            }

            if (rData.Result == (int)CreateNewPostResult.ResultDefine.emResult_Success)
            {
                // DB 交易提交
                GetSql().CommitTran();

                // 建立Redis 會員貼文
                {
                    JObject jsNewPost = new JObject();
                    jsNewPost.Add("MemberID", newPost.MemberID);
                    jsNewPost.Add("CreateDate", newPost.CreateDate);
                    jsNewPost.Add("Place", newPost.Place);
                    jsNewPost.Add("Photo", newPost.Photo);
                    jsNewPost.Add("Content", newPost.Content);

                    JArray jsLikeList = new JArray();
                    jsNewPost.Add("LikeList", jsLikeList.ToString());

                    GetRedis((int)Connect.RedisDB.emRedisDB_Post).StringSet($"PostInfo_" + newPost.PostID, jsNewPost.ToString());
                }

                // 建立Redis 會員的貼文ID列表
                {
                    string sPostList = "[]";

                    if (GetRedis((int)Connect.RedisDB.emRedisDB_Post).KeyExists($"PostList_" + newPost.MemberID))
                    {
                        sPostList = GetRedis((int)Connect.RedisDB.emRedisDB_Post).StringGet($"PostList_" + newPost.MemberID);
                    }

                    JArray        jsMemberPostList = JArray.Parse(sPostList);
                    List <string> memberPostList   = jsMemberPostList.ToObject <List <string> >();

                    memberPostList.Add(newPost.PostID);

                    JArray jsNewList = JArray.FromObject(memberPostList);

                    GetRedis((int)Connect.RedisDB.emRedisDB_Post).StringSet($"PostList_" + newPost.MemberID, jsNewList.ToString());
                }

                AddFriendPost(newPost.MemberID, newPost.PostID);
            }
            else
            {
                // DB 交易失敗, 啟動Rollback
                GetSql().RollbackTran();
            }

            JObject jsMain = new JObject();

            jsMain.Add("CmdID", (int)S2C_CmdID.emCreateNewPostResult);
            jsMain.Add("Data", JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(rData)));

            ret = jsMain.ToString();

            return(ret);
        }