Esempio n. 1
0
        public async Task <HttpResponseMessage> Rename()
        {
            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 newname = GetValueFromForm(form, "name");
                currentEntity = await MyCoreApi.FindNodeByIdAsync(new Guid(GetValueFromForm(form, "node")));

                if (!await MyCoreApi.Authorization(currentEntity.Id, user, "1"))
                {
                    throw new Exception("Not authorization!");
                }
                var newNode = await MyCoreApi.ReNameNodeAsync(currentEntity.Id, newname);

                successEntity.Add(Mapper.Map <NodeDto>(newNode));//保存处理结果
                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")
                });
            }
        }