public virtual async Task <IHttpActionResult> UndeleteAsync(string[] ids)
        {
            if (!_supportsSoftDeletes)
            {
                return(StatusCode(HttpStatusCode.BadRequest));
            }

            var items = (await GetModels(ids, false)).ToList();

            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 = await CanDeleteAsync(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 UndeleteModelsAsync(items);

                foreach (var item in items)
                {
                    await AfterUndeleteAsync(item);
                }
            } catch (Exception ex) {
                _logger.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));
        }
Example #2
0
 protected IHttpActionResult BadRequest(ModelActionResults results)
 {
     return(new NegotiatedContentResult <ModelActionResults>(HttpStatusCode.BadRequest, results, this));
 }