Exemplo n.º 1
0
        public async Task <IActionResult> Transpond([Required][FromBody] MessageForTransponder message)
        {
            //需要验证身份
            //返回是否转发成功
            //同样存在与Topic的联动
            int userId;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = int.Parse(HttpContext.User.Claims.First().Value);
            }
            else
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData();
                rr.Code    = 200;
                rr.Message = "Need Authentication";
                return(new JsonResult(rr));
            }

            return(await Wrapper.wrap(async (OracleConnection conn) =>
            {
                //FUNC_TRANSPOND_MESSAGE(userID in INTEGER, message_content in VARCHAR2, transpondID in INTEGER, messageID out INTEGER)
                //return INTEGER
                string procedureName = "FUNC_TRANSPOND_MESSAGE";
                OracleCommand cmd = new OracleCommand(procedureName, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                //Add return value
                OracleParameter p1 = new OracleParameter();
                p1 = cmd.Parameters.Add("state", OracleDbType.Int32);
                p1.Direction = ParameterDirection.ReturnValue;

                //Add first parameter userID
                OracleParameter p2 = new OracleParameter();
                p2 = cmd.Parameters.Add("userID", OracleDbType.Int32);
                p2.Direction = ParameterDirection.Input;
                p2.Value = userId;

                //Add second parameter message_content
                OracleParameter p3 = new OracleParameter();
                p3 = cmd.Parameters.Add("message_content", OracleDbType.Varchar2);
                p3.Direction = ParameterDirection.Input;
                p3.Value = message.message_content;

                //Add third parameter message_sender_user_id
                OracleParameter p4 = new OracleParameter();
                p4 = cmd.Parameters.Add("transpondID", OracleDbType.Int32);
                p4.Direction = ParameterDirection.Input;
                p4.Value = message.message_transpond_message_id;

                //Add fourth parameter message_id
                OracleParameter p5 = new OracleParameter();
                p5 = cmd.Parameters.Add("messageID", OracleDbType.Int32);
                p5.Direction = ParameterDirection.Output;

                await cmd.ExecuteReaderAsync();
                if (int.Parse(p1.Value.ToString()) == 0)
                {
                    throw new Exception("failed");
                }

                await TopicController.AddTopicsInTwitter(message.message_content, int.Parse(p5.Value.ToString()));
                await AtController.AddAtsInTwitter(message.message_content, int.Parse(p5.Value.ToString()), userId);

                RestfulResult.RestfulData rr = new RestfulResult.RestfulData(200, "success");
                return new JsonResult(rr);
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Send([Required][FromForm] MessageForSender message)
        {
            //TODO 需要验证身份
            //有很多参数都是有初始化的
            //!!!!!与Topic的联动
            //首先要检查message中是否有两个#号括起来的连续无空格字符串
            //若有,则去数据库中检索该Topic是否存在,若不存在则添加,若存在则将其热度提高

            int           userId;
            List <string> topics = new List <string>();
            List <string> ats    = new List <string>();

            System.Text.RegularExpressions.Regex topicRegex = new System.Text.RegularExpressions.Regex(@"#(\w+)#");
            System.Text.RegularExpressions.Regex atRegex    = new System.Text.RegularExpressions.Regex(@"@(\w+)");

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = int.Parse(HttpContext.User.Claims.First().Value);

                //检查message_content里含有的话题,用两个#包含的内容作为话题。若出现两个连续的#,则忽略之。
                //所有的话题内容会被保存到topics列表内,并在调用第二个函数FUNC_ADD_TOPIC时,逐一对topic的内容进行处理(不存在则创建,存在则热度+1)
                //对艾特内容同理
                System.Text.RegularExpressions.MatchCollection topicCollection = topicRegex.Matches(message.message_content);
                System.Text.RegularExpressions.MatchCollection atCollection    = atRegex.Matches(message.message_content);
                for (int i = 0; i < topicCollection.Count; i++)
                {
                    topics.Add(topicCollection[i].Groups[1].ToString());
                }
                for (int i = 0; i < atCollection.Count; i++)
                {
                    ats.Add(atCollection[i].Groups[1].ToString());
                }
            }
            else
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData();
                rr.Code    = 200;
                rr.Message = "Need Authentication";
                return(new JsonResult(rr));
            }

            using (OracleConnection conn = new OracleConnection(ConnStr.getConnStr()))
            {
                try
                {
                    conn.ConnectionString = ConnStr.getConnStr();
                    conn.Open();
                    //FUNC_SEND_MESSAGE(message_content in VARCHAR2, message_has_image in INTEGER, user_id in INTEGER, message_image_count in INTEGER, message_id out INTEGER)
                    //return INTEGER
                    string        procedureName = "FUNC_SEND_MESSAGE";
                    OracleCommand cmd           = new OracleCommand(procedureName, conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    //Add return value
                    OracleParameter p1 = new OracleParameter();
                    p1           = cmd.Parameters.Add("state", OracleDbType.Int32);
                    p1.Direction = ParameterDirection.ReturnValue;

                    //Add first parameter message_content
                    OracleParameter p2 = new OracleParameter();
                    p2           = cmd.Parameters.Add("message_content", OracleDbType.Varchar2);
                    p2.Direction = ParameterDirection.Input;
                    p2.Value     = message.message_content;

                    //Add second parameter message_has_image
                    OracleParameter p3 = new OracleParameter();
                    p3           = cmd.Parameters.Add("message_has_image", OracleDbType.Int32);
                    p3.Direction = ParameterDirection.Input;
                    p3.Value     = message.message_has_image;

                    //Add third parameter user_id
                    OracleParameter p4 = new OracleParameter();
                    p4           = cmd.Parameters.Add("user_id", OracleDbType.Int32);
                    p4.Direction = ParameterDirection.Input;
                    p4.Value     = userId;

                    //Add fourth parameter message_image_count
                    OracleParameter p5 = new OracleParameter();
                    p5           = cmd.Parameters.Add("message_image_count", OracleDbType.Int32);
                    p5.Direction = ParameterDirection.Input;
                    p5.Value     = message.message_image_count;

                    //Add fifth parameter message_id
                    OracleParameter p6 = new OracleParameter();
                    p6           = cmd.Parameters.Add("message_id", OracleDbType.Int32);
                    p6.Direction = ParameterDirection.Output;

                    await cmd.ExecuteReaderAsync();

                    if (int.Parse(p1.Value.ToString()) == 0)
                    {
                        throw new Exception("failed");
                    }

                    await TopicController.AddTopicsInTwitter(message.message_content, int.Parse(p6.Value.ToString()));

                    await AtController.AddAtsInTwitter(message.message_content, int.Parse(p6.Value.ToString()), userId);

                    //若推特含图,从POST体内获得图的内容并保存到服务器
                    if (message.message_has_image == 1)
                    {
                        var images  = Request.Form.Files;
                        int img_num = 0;
                        Directory.CreateDirectory(@"wwwroot\Messages\" + p6.Value.ToString());
                        foreach (var imgfile in images)
                        {
                            if (imgfile.Length > 0)
                            {
                                string img_path;
                                if (imgfile.ContentType.Substring(0, 5) == "image")
                                {
                                    img_path = @"wwwroot\Messages\" + p6.Value.ToString() + @"\" + img_num.ToString() + ".jpg";
                                }
                                else
                                {
                                    string videoFormat = imgfile.ContentType.Split("/")[1];
                                    img_path = @"wwwroot\Messages\" + p6.Value.ToString() + @"\" + img_num.ToString() + "." + videoFormat;
                                }
                                using (var stream = new FileStream(img_path, FileMode.Create))
                                {
                                    await imgfile.CopyToAsync(stream);
                                }
                                img_num++;
                            }
                        }
                    }

                    RestfulResult.RestfulData rr = new RestfulResult.RestfulData(200, "success");
                    conn.Close();
                    return(new JsonResult(rr));
                }
                catch (Exception e)
                {
                    RestfulResult.RestfulData rr = new RestfulResult.RestfulData(500, "fail");
                    Console.Write(e.Message);
                    Console.Write(e.StackTrace);
                    conn.Close();
                    return(new JsonResult(rr));
                }
            }
        }