public virtual async Task <IActionResult> Post([FromBody] TEntity entity, CancellationToken cancellationToken)
        {
            this.OnCreating(entity);

            if (!this.ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try {
                var dbSet = this.Context.Set <TEntity>();
                await dbSet.AddAsync(entity, cancellationToken);

                await this.Context.SaveChangesAsync(cancellationToken);

                return(Created(entity));
            } catch (DbUpdateException ex) {
                if (ArchiveDbContext.IsUserError(ex, out var message))
                {
                    return(BadRequest(new ProblemDetails {
                        Type = "archive-site:database-error/invalid-create",
                        Title = "The requested update was not valid.",
                        Detail = message
                    }));
                }
                else
                {
                    throw;
                }
            }
        }
        public virtual async Task <IActionResult> Put([FromODataUri] Int64 key, [FromBody] TEntity entity, CancellationToken cancellationToken)
        {
            var dbSet    = this.Context.Set <TEntity>();
            var existing = await dbSet.SingleOrDefaultAsync(e => e.Id == key, cancellationToken);

            if (existing == null)
            {
                return(NotFound());
            }

            this.OnUpdating(key, entity);

            entity.CopyTo(existing);
            try {
                await this.Context.SaveChangesAsync(cancellationToken);

                return(Ok(existing));
            } catch (DbUpdateException ex) {
                if (ArchiveDbContext.IsUserError(ex, out var message))
                {
                    return(BadRequest(new ProblemDetails {
                        Type = "archive-site:database-error/invalid-update",
                        Title = "The requested update was not valid.",
                        Detail = message
                    }));
                }
                else
                {
                    throw;
                }
            }
        }
示例#3
0
        protected async Task <IActionResult> TrySaveChanges(
            TEntity entity,
            Func <TEntity, Task <IActionResult> > onSuccess,
            String operation,
            CancellationToken cancellationToken)
        {
            try {
                await this.DbContext.SaveChangesAsync(cancellationToken);

                var result = await onSuccess(entity);

                if (this.transaction != null)
                {
                    await this.transaction.CommitAsync(cancellationToken);
                }

                return(result);
            } catch (DbUpdateException ex) {
                if (ArchiveDbContext.IsUserError(ex, out var message))
                {
                    return(BadRequest(new ProblemDetails {
                        Type = $"archive-site:database-error/invalid-{operation}",
                        Title = $"The requested {operation} was not valid.",
                        Detail = message
                    }));
                }
                else
                {
                    throw;
                }
            }
        }