Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> Delete()
        {
            var user = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault());

            List <NodeDto> successEntity = new List <NodeDto>(); //成功处理的Entity信息
            JDBCEntity     currentEntity = null;                 //当前处理的Entity信息

            try
            {
                if (Request.RequestUri.PathAndQuery.ToLower().Equals("/entity/all"))//清空数据库
                {
                    await MyCoreApi.clearDb();

                    return(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.OK, Content = new StringContent("Clear database successfully!")
                    });
                }
                Uri uri   = ParseToQueryUri(Request.RequestUri);
                var nodes = await MyCoreApi.FindNodeByUriAsync(uri);//获取被删除entity

                var queryDict = ParseToQueryDictionary(Request.RequestUri);
                if ((!queryDict.ContainsKey("recursive") || queryDict["recursive"] == "false") && nodes.Count() > 1)//递归删除查询到的所有entity
                {
                    throw new Exception("Please use [recursive=true] to delete queried entities!");
                }

                if (nodes.Count() == 0)//entity不存在
                {
                    throw new Exception("No entity is found!");
                }
                var deleteChildren = false;
                if (queryDict.ContainsKey("_recursive") && queryDict["_recursive"] == "true")
                {
                    deleteChildren = true;
                }

                foreach (var entity in nodes)
                {
                    currentEntity = entity;
                    if (!await MyCoreApi.Authorization(currentEntity.Id, user, "1"))
                    {
                        throw new Exception("Not authorization!");
                    }
                    await MyCoreApi.DeletAsync(entity.Id, deleteChildren);

                    successEntity.Add(Mapper.Map <NodeDto>(currentEntity));
                }
                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")
                });
            }
        }