protected async Task <IActionResult> DeleteImplAsync(string[] ids)
        {
            var items = await GetModelsAsync(ids, false);

            if (items.Count == 0)
            {
                return(NotFound());
            }

            var results = new ModelActionResults();

            results.AddNotFound(ids.Except(items.Select(i => i.Id)));

            var list = items.ToList();

            foreach (var model in items)
            {
                var permission = await CanDeleteAsync(model);

                if (permission.Allowed)
                {
                    continue;
                }

                list.Remove(model);
                results.Failure.Add(permission);
            }

            if (list.Count == 0)
            {
                return(results.Failure.Count == 1 ? Permission(results.Failure.First()) : BadRequest(results));
            }

            IEnumerable <string> workIds;

            try {
                workIds = await DeleteModelsAsync(list) ?? new List <string>();
            } catch (Exception ex) {
                using (_logger.BeginScope(new ExceptionlessState().Identity(CurrentUser.EmailAddress).Property("User", CurrentUser).SetHttpContext(HttpContext)))
                    _logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            if (results.Failure.Count == 0)
            {
                return(WorkInProgress(workIds));
            }

            results.Workers.AddRange(workIds);
            results.Success.AddRange(list.Select(i => i.Id));
            return(BadRequest(results));
        }
예제 #2
0
        public virtual async Task <IHttpActionResult> DeleteAsync(string[] ids)
        {
            var items = await GetModelsAsync(ids, false);

            if (items.Count == 0)
            {
                return(NotFound());
            }

            var results = new ModelActionResults();

            results.AddNotFound(ids.Except(items.Select(i => i.Id)));

            var list = items.ToList();

            foreach (var model in items)
            {
                var permission = await CanDeleteAsync(model);

                if (permission.Allowed)
                {
                    continue;
                }

                list.Remove(model);
                results.Failure.Add(permission);
            }

            if (list.Count == 0)
            {
                return(results.Failure.Count == 1 ? Permission(results.Failure.First()) : BadRequest(results));
            }

            IEnumerable <string> workIds;

            try {
                workIds = await DeleteModelsAsync(list) ?? new List <string>();
            } catch (Exception ex) {
                _logger.Error().Exception(ex).Identity(ExceptionlessUser.EmailAddress).Property("User", ExceptionlessUser).SetActionContext(ActionContext).Write();
                return(StatusCode(HttpStatusCode.InternalServerError));
            }

            if (results.Failure.Count == 0)
            {
                return(WorkInProgress(workIds));
            }

            results.Workers.AddRange(workIds);
            results.Success.AddRange(list.Select(i => i.Id));
            return(BadRequest(results));
        }
        public virtual async Task <IHttpActionResult> Delete(string[] ids)
        {
            var items = GetModels(ids, false);

            if (!items.Any())
            {
                return(NotFound());
            }

            var results = new ModelActionResults();

            results.AddNotFound(ids.Except(items.Select(i => i.Id)));

            foreach (var model in items.ToList())
            {
                var permission = CanDelete(model);
                if (permission.Allowed)
                {
                    continue;
                }

                items.Remove(model);
                results.Failure.Add(permission);
            }

            if (items.Count == 0)
            {
                return(results.Failure.Count == 1 ? Permission(results.Failure.First()) : BadRequest(results));
            }

            try {
                await DeleteModels(items);
            } catch (Exception ex) {
                Log.Error().Exception(ex).Write();
                return(StatusCode(HttpStatusCode.InternalServerError));
            }

            if (results.Failure.Count == 0)
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }

            results.Success.AddRange(items.Select(i => i.Id));
            return(BadRequest(results));
        }