예제 #1
0
        public async Task <HttpResponseMessage> Move()
        {
            var            user          = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault());
            List <NodeDto> successEntity = new List <NodeDto>(); //成功处理的Entity信息
            JDBCEntity     currentEntity = null;                 //当前处理的Entity信息

            try
            {
                var dict = ParseToQueryDictionary(Request.RequestUri);
                NameValueCollection form = Request.Content.ReadAsFormDataAsync().Result;
                var fromNodes            = await MyCoreApi.FindNodeByUriAsync(new Uri("jdbc://" + GetValueFromForm(form, "from").Replace(";", "&").Replace(",", "&").Trim("'\"()[]{}".ToCharArray()))); //获取源entity

                var toNodes = await MyCoreApi.FindNodeByUriAsync(new Uri("jdbc://" + GetValueFromForm(form, "to").Replace(";", "&").Replace(",", "&").Trim("'\"()[]{}".ToCharArray())));                //获取目的entity

                foreach (var fromNode in fromNodes)
                {
                    foreach (var toNode in toNodes)
                    {
                        currentEntity = toNode;
                        if (!await MyCoreApi.Authorization(currentEntity.Id, user, "1"))
                        {
                            throw new Exception("Not authorization!");
                        }
                        var newNode = await MyCoreApi.MoveJdbcEntityAsync(toNode.Id, fromNode.Id); //迁移entity到新的父节点下

                        successEntity.Add(Mapper.Map <NodeDto>(toNode));                           //保存处理结果
                    }
                }
                return(new HttpResponseMessage {
                    Content = new StringContent(SerializeObjectToString(successEntity), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
                });
            }
            catch (Exception e)
            {
                if (currentEntity == null)
                {
                    return(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(e.InnerException != null ? e.InnerException.Message : e.Message)
                    });
                }
                var response = new ResponseEntityMessage
                {
                    Fail = new
                    {
                        Description = e.InnerException != null ? e.InnerException.Message : e.Message,
                        Id          = currentEntity.Id,
                        Path        = currentEntity.Path
                    },
                    Success = successEntity
                };
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(SerializeObjectToString(response), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
                });
            }
        }