-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix the livedocs models to support serialization: you cannot ha…
…ve nullable, required properties
- Loading branch information
1 parent
48bd6aa
commit 17cf17a
Showing
8 changed files
with
549 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using HotChocolate; | ||
using LiveDocs.GraphQLApi.Validations; | ||
using RxDBDotNet.Documents; | ||
|
||
namespace LiveDocs.GraphQLApi.Models.Shared; | ||
|
||
[GraphQLName("Hero")] | ||
public class Hero : IReplicatedDocument | ||
{ | ||
/// <inheritdoc /> | ||
[Required] | ||
[NotDefault] | ||
public required Guid Id { get; init; } | ||
|
||
/// <inheritdoc /> | ||
[Required] | ||
public required bool IsDeleted { get; set; } | ||
|
||
/// <inheritdoc /> | ||
[NotMapped] | ||
public List<string>? Topics { get; init; } | ||
|
||
/// <inheritdoc /> | ||
[Required] | ||
[NotDefault] | ||
public required DateTimeOffset UpdatedAt { get; set; } | ||
|
||
[Required] | ||
[MaxLength(100)] | ||
public required string Name { get; set; } | ||
|
||
[Required] | ||
[MaxLength(30)] | ||
public required string Color { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using LiveDocs.GraphQLApi.Data; | ||
using LiveDocs.GraphQLApi.Models.Shared; | ||
using Microsoft.EntityFrameworkCore; | ||
using RxDBDotNet.Repositories; | ||
using RxDBDotNet.Services; | ||
|
||
namespace LiveDocs.GraphQLApi.Services; | ||
|
||
public class HeroService : IDocumentService<Hero> | ||
{ | ||
private readonly LiveDocsDbContext _dbContext; | ||
private readonly IEventPublisher _eventPublisher; | ||
private readonly List<Hero> _pendingEvents = []; | ||
|
||
public HeroService(LiveDocsDbContext dbContext, IEventPublisher eventPublisher) | ||
{ | ||
_dbContext = dbContext; | ||
_eventPublisher = eventPublisher; | ||
} | ||
|
||
public IQueryable<Hero> GetQueryableDocuments() | ||
{ | ||
return _dbContext.Heroes.AsNoTracking(); | ||
} | ||
|
||
public Task<List<Hero>> ExecuteQueryAsync(IQueryable<Hero> query, CancellationToken cancellationToken) | ||
{ | ||
return query.ToListAsync(cancellationToken); | ||
} | ||
|
||
public Task<Hero?> GetDocumentByIdAsync(Guid id, CancellationToken cancellationToken) | ||
{ | ||
return _dbContext.Heroes.Where(d => d.Id == id).SingleOrDefaultAsync(cancellationToken); | ||
} | ||
|
||
public async Task<Hero> CreateDocumentAsync(Hero document, CancellationToken cancellationToken) | ||
{ | ||
await _dbContext.Set<Hero>() | ||
.AddAsync(document, cancellationToken); | ||
|
||
_pendingEvents.Add(document); | ||
|
||
return document; | ||
} | ||
|
||
public async Task<Hero> UpdateDocumentAsync(Hero document, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(document); | ||
|
||
var heroToUpdate = await GetDocumentByIdAsync(document.Id, cancellationToken) | ||
?? throw new InvalidOperationException($"Entity with a ReplicateDocumentId of {document.Id} not found for update."); | ||
|
||
heroToUpdate.Name = document.Name; | ||
heroToUpdate.Color = document.Color; | ||
heroToUpdate.UpdatedAt = DateTimeOffset.Now; | ||
|
||
_pendingEvents.Add(heroToUpdate); | ||
|
||
return heroToUpdate; | ||
} | ||
|
||
public async Task<Hero> MarkAsDeletedAsync(Hero document, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(document); | ||
|
||
var heroToDelete = await GetDocumentByIdAsync(document.Id, cancellationToken) | ||
?? throw new InvalidOperationException($"Entity with a ReplicateDocumentId of {document.Id} not found for update."); | ||
|
||
heroToDelete.IsDeleted = true; | ||
heroToDelete.UpdatedAt = DateTimeOffset.Now; | ||
|
||
_pendingEvents.Add(heroToDelete); | ||
|
||
return heroToDelete; | ||
} | ||
|
||
public bool AreDocumentsEqual(Hero document1, Hero document2) | ||
{ | ||
return true; | ||
} | ||
|
||
public async Task SaveChangesAsync(CancellationToken cancellationToken) | ||
{ | ||
await _dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
foreach (var doc in _pendingEvents) | ||
{ | ||
await _eventPublisher.PublishDocumentChangedEventAsync(doc, cancellationToken); | ||
} | ||
|
||
_pendingEvents.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.