Skip to content

Commit

Permalink
Re-organize how database Ids are _id
Browse files Browse the repository at this point in the history
so that _id is used and we can update data by _id
  • Loading branch information
bramvandenbussche committed Jun 16, 2024
1 parent 9f23e5e commit c1f1070
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static Highlight ToDomain(this CreateHighlightRequest.HighlightDto dto)
{
return new Highlight()
{
NoteId = Guid.NewGuid(),
RaisedTime = DateTimeOffset.UtcNow,
Author = dto.Author,
Title = dto.Title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BookDto

public class HighlightDto
{
public Guid Id { get; set; }
public string Id { get; set; }

public string Timestamp { get; set; } = string.Empty;
public string HighlightText { get; set; } = string.Empty;
Expand Down
8 changes: 2 additions & 6 deletions bramvandenbussche.readwiser.data.mongodb/Model/StoredNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ namespace bramvandenbussche.readwiser.data.mongodb.Model;

public class StoredNote
{
[BsonId]
[DataMember]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId _id { get; set; }

[DataMember]
public Guid Id { get; set; }
public ObjectId Id { get; set; }

[DataMember]
public DateTime Timestamp { get; set; }
Expand Down Expand Up @@ -51,12 +47,12 @@ public class StoredNote
public Highlight AsDomain() =>
new()
{
NoteId = Id.ToString(),
Author = Author,
Title = Title,
Chapter = Chapter,
Note = Note,
Text = Text,
NoteId = Id,
RaisedTime = Timestamp
};
}
59 changes: 30 additions & 29 deletions bramvandenbussche.readwiser.data.mongodb/MongoDbNoteRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using bramvandenbussche.readwiser.data.mongodb.Model;
using bramvandenbussche.readwiser.domain.Interface.DataAccess;
using bramvandenbussche.readwiser.domain.Model;
using MongoDB.Bson;
using MongoDB.Driver;

namespace bramvandenbussche.readwiser.data.mongodb
Expand All @@ -17,25 +18,41 @@ public MongoDbNoteRepository(IMongoDatabase database)
}

public async Task<IEnumerable<Highlight>> GetAll(int timestamp)
{

var data = await _noteCollection.Find(_ => true).ToListAsync();
=> (await _noteCollection.Find(_ => true)
.ToListAsync())
.Select(note => note.AsDomain());

return data.Select(note => note.AsDomain());
}

public async Task<IEnumerable<Highlight>> GetForBook(string title, string author)
{
var data = await _noteCollection.Find(x => x.Title == title && x.Author == author).ToListAsync();
=> (await _noteCollection.Find(x => x.Title == title && x.Author == author)
.ToListAsync())
.Select(note => note.AsDomain());


public async Task<IEnumerable<Highlight>> GetNotesForAuthor(string author)
=> (await _noteCollection.Find(x => x.Author == author)
.ToListAsync())
.Select(note => note.AsDomain());


public async Task<IEnumerable<string>> GetAllAuthors()
=> await _noteCollection.Distinct(x => x.Author, _ => true).ToListAsync();


public async Task<IEnumerable<Highlight>> GetRecent(int amount)
=> (await _noteCollection
.Find(_ => true)
.SortByDescending(n => n.Timestamp)
.Limit(amount)
.ToListAsync())
.Select(note => note.AsDomain());

return data.Select(note => note.AsDomain());
}

public async Task Save(Highlight note)
{
var newNote = new StoredNote()
{
Id = Guid.NewGuid(),
//Id = Guid.NewGuid(),
Timestamp = DateTime.UtcNow,
Title = note.Title,
Author = note.Author,
Expand All @@ -47,28 +64,12 @@ public async Task Save(Highlight note)
await _noteCollection.InsertOneAsync(newNote);
}

public async Task<IEnumerable<string>> GetAllAuthors()
=> await _noteCollection.Distinct(x => x.Author, _ => true).ToListAsync();

public async Task<IEnumerable<Highlight>> GetNotesForAuthor(string author)
{
var data = await _noteCollection.Find(x => x.Author == author).ToListAsync();

return data.Select(note => note.AsDomain());
}

public async Task<IEnumerable<Highlight>> GetRecentHighlights(int amount)
=> (await _noteCollection
.Find(_ => true)
.SortByDescending(n => n.Timestamp)
.Limit(amount)
.ToListAsync())
.Select(note => note.AsDomain());

public async Task UpdateHighlight(Highlight highlight)
{
var noteId = new BsonObjectId(new ObjectId(highlight.NoteId));
var existing = _noteCollection.Find(note => note.Id == noteId).FirstOrDefault();
var update = Builders<StoredNote>.Update.Set(note => note.Note, highlight.Note);
await _noteCollection.UpdateOneAsync(note => note.Id == highlight.NoteId, update);
await _noteCollection.UpdateOneAsync(note => note.Id == noteId, update);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task<IEnumerable<Book>> GetAllBooks()

public Task<IEnumerable<Highlight>> GetRecentHighlights(int amount)
{
return _repository.GetRecentHighlights(amount);
return _repository.GetRecent(amount);
}

public async Task UpdateHighlight(Highlight highlight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface INoteRepository
/// Retrieve a list of the most recent highlights
/// </summary>
/// <param name="amount">The amount of items to retrieve</param>
public Task<IEnumerable<Highlight>> GetRecentHighlights(int amount);
public Task<IEnumerable<Highlight>> GetRecent(int amount);



Expand Down
2 changes: 1 addition & 1 deletion bramvandenbussche.readwiser.domain/Model/Highlight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Highlight

public DateTimeOffset RaisedTime { get; set; } = DateTimeOffset.UtcNow;

public Guid NoteId { get; set; }
public string NoteId { get; set; }

public string SortOrder { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

<h3>Latest Notes</h3>

<p>Recent annotations</p>
<input type="number" @bind="@_maxNotes" />
<button class="btn btn-primary" @onclick="() => OnInitializedAsync()">Refresh</button>

@if (_recentNotes == null)
{
<p><em>Loading...</em></p>
Expand All @@ -20,8 +24,12 @@ else
@code {
private Highlight[]? _recentNotes;

private int _maxNotes = 5;

protected override async Task OnInitializedAsync()
{
_recentNotes = (await Service.GetRecentHighlights(5)).ToArray();
Console.WriteLine("Loading data");
_recentNotes = null;
_recentNotes = (await Service.GetRecentHighlights(_maxNotes)).ToArray();
}
}
9 changes: 4 additions & 5 deletions bramvandenbussche.readwiser.web/Components/Pages/Author.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

<PageTitle>Author: @Name</PageTitle>

<h1>Author: @Name</h1>
<h1>Books by @Name</h1>

@if (_books == null)
{
<p><em>Loading data...</em></p>
}
else
{
<table class="table">
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
Expand All @@ -29,8 +29,8 @@ else
@foreach (var book in _books)
{
var currentBook = book;
<tr>
<td @onclick="@(() => SelectBook(currentBook))" style="cursor: pointer;">
<tr @onclick="@(() => SelectBook(currentBook))" style="cursor: pointer;">
<td>
@book.Title
</td>
<td>@book.Highlights.Count()</td>
Expand Down Expand Up @@ -65,7 +65,6 @@ else
if (Name == null) return;

_books = (await Service.GetBooksForAuthor(Name)).ToArray();
_selectedBook = _books.Last();
}

void SelectBook(Book book)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/"
@attribute [StreamRendering]
@rendermode InteractiveServer

<PageTitle>Readwiser - Home</PageTitle>

Expand Down

0 comments on commit c1f1070

Please sign in to comment.