Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using System.Net;

namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore;

Expand Down Expand Up @@ -109,7 +109,7 @@ internal async Task WrapExceptionAsync(string id, Func<Task> action, Cancellatio
}
catch (DbUpdateConcurrencyException ex)
{
throw new HttpException(StatusCodes.Status409Conflict, ex.Message, ex) { Payload = await GetEntityAsync(id, cancellationToken).ConfigureAwait(false) };
throw new HttpException((int)HttpStatusCode.Conflict, ex.Message, ex) { Payload = await GetEntityAsync(id, cancellationToken).ConfigureAwait(false) };
}
catch (DbUpdateException ex)
{
Expand All @@ -136,7 +136,7 @@ await WrapExceptionAsync(entity.Id, async () =>
// We do not use Any() here because it is not supported by all providers (e.g. Cosmos)
if (DataSet.Count(x => x.Id == entity.Id) > 0)
{
throw new HttpException(StatusCodes.Status409Conflict) { Payload = await GetEntityAsync(entity.Id, cancellationToken).ConfigureAwait(false) };
throw new HttpException((int)HttpStatusCode.Conflict) { Payload = await GetEntityAsync(entity.Id, cancellationToken).ConfigureAwait(false) };
}

UpdateManagedProperties(entity);
Expand All @@ -150,17 +150,17 @@ public virtual async ValueTask DeleteAsync(string id, byte[]? version = null, Ca
{
if (string.IsNullOrEmpty(id))
{
throw new HttpException(StatusCodes.Status400BadRequest, "ID is required");
throw new HttpException((int)HttpStatusCode.BadRequest, "ID is required");
}

await WrapExceptionAsync(id, async () =>
{
TEntity storedEntity = await DataSet.FindAsync(new object[] { id }, cancellationToken).ConfigureAwait(false)
?? throw new HttpException(StatusCodes.Status404NotFound);
TEntity storedEntity = await DataSet.FindAsync([id], cancellationToken).ConfigureAwait(false)
?? throw new HttpException((int)HttpStatusCode.NotFound);

if (version?.Length > 0 && !storedEntity.Version.SequenceEqual(version))
{
throw new HttpException(StatusCodes.Status412PreconditionFailed) { Payload = await GetEntityAsync(id, cancellationToken).ConfigureAwait(false) };
throw new HttpException((int)HttpStatusCode.PreconditionFailed) { Payload = await GetEntityAsync(id, cancellationToken).ConfigureAwait(false) };
}

_ = DataSet.Remove(storedEntity);
Expand All @@ -173,11 +173,11 @@ public virtual async ValueTask<TEntity> ReadAsync(string id, CancellationToken c
{
if (string.IsNullOrEmpty(id))
{
throw new HttpException(StatusCodes.Status400BadRequest, "ID is required");
throw new HttpException((int)HttpStatusCode.BadRequest, "ID is required");
}

TEntity entity = await DataSet.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id, cancellationToken).ConfigureAwait(false)
?? throw new HttpException(StatusCodes.Status404NotFound);
?? throw new HttpException((int)HttpStatusCode.NotFound);

return entity;
}
Expand All @@ -187,17 +187,17 @@ public virtual async ValueTask ReplaceAsync(TEntity entity, byte[]? version = nu
{
if (string.IsNullOrEmpty(entity.Id))
{
throw new HttpException(StatusCodes.Status400BadRequest, "ID is required");
throw new HttpException((int)HttpStatusCode.BadRequest, "ID is required");
}

await WrapExceptionAsync(entity.Id, async () =>
{
TEntity storedEntity = await DataSet.FindAsync(new object[] { entity.Id }, cancellationToken).ConfigureAwait(false)
?? throw new HttpException(StatusCodes.Status404NotFound);
?? throw new HttpException((int)HttpStatusCode.NotFound);

if (version?.Length > 0 && !storedEntity.Version.SequenceEqual(version))
{
throw new HttpException(StatusCodes.Status412PreconditionFailed) { Payload = await GetEntityAsync(entity.Id, cancellationToken).ConfigureAwait(false) };
throw new HttpException((int)HttpStatusCode.PreconditionFailed) { Payload = await GetEntityAsync(entity.Id, cancellationToken).ConfigureAwait(false) };
}

UpdateManagedProperties(entity);
Expand Down
Loading