Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Grinstead committed Sep 28, 2023
1 parent f718a0c commit 03fd007
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 128 deletions.
7 changes: 2 additions & 5 deletions src/Aquifer.API/Modules/Bibles/BibleResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Aquifer.API.Utilities;

namespace Aquifer.API.Modules.Bibles;
namespace Aquifer.API.Modules.Bibles;

public class BibleResponse
{
Expand All @@ -14,8 +12,7 @@ public class BibleResponse

public class BibleResponseBook
{
public int BookId { get; set; }
public BibleUtilities.BookCode BookCode { get; set; }
public string BookCode { get; set; }
public string DisplayName { get; set; } = null!;
public int TextSize { get; set; }
public int AudioSize { get; set; }
Expand Down
20 changes: 12 additions & 8 deletions src/Aquifer.API/Modules/Bibles/BiblesModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Aquifer.API.Utilities;
using Aquifer.Data;
using Aquifer.Data;
using Aquifer.Data.Enums;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;

Expand All @@ -11,7 +11,7 @@ public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
{
var group = endpoints.MapGroup("bibles");
group.MapGet("language/{languageId:int}", GetBibleByLanguage);
group.MapGet("{bibleId:int}/book/{bookId:int}", GetBibleBookDetails);
group.MapGet("{bibleId:int}/book/{bookCode}", GetBibleBookDetails);

return endpoints;
}
Expand All @@ -29,8 +29,7 @@ private async Task<Ok<List<BibleResponse>>> GetBibleByLanguage(int languageId,
Books = bible.BibleBookContents.OrderBy(book => book.BookId).Select(book =>
new BibleResponseBook
{
BookId = book.BookId,
BookCode = BibleUtilities.BookIdToCode(book.BookId),
BookCode = book.BookId.ToCode(),
DisplayName = book.DisplayName,
TextSize = book.TextSize,
AudioSize = book.AudioSize,
Expand All @@ -43,10 +42,16 @@ private async Task<Ok<List<BibleResponse>>> GetBibleByLanguage(int languageId,

private async Task<Results<Ok<BibleBookDetailsResponse>, NotFound>> GetBibleBookDetails(
int bibleId,
int bookId,
string bookCode,
AquiferDbContext dbContext,
CancellationToken cancellationToken)
{
var bookId = BookIdSerializer.FromCode(bookCode);
if (bookId == null)
{
return TypedResults.NotFound();
}

var book = await dbContext.BibleBookContents
.SingleOrDefaultAsync(b => b.BibleId == bibleId && b.BookId == bookId, cancellationToken);

Expand All @@ -59,8 +64,7 @@ private async Task<Results<Ok<BibleBookDetailsResponse>, NotFound>> GetBibleBook
{
AudioSize = book.AudioSize,
AudioUrls = book.AudioUrls,
BookId = book.BookId,
BookCode = BibleUtilities.BookIdToCode(book.BookId),
BookCode = book.BookId.ToCode(),
ChapterCount = book.ChapterCount,
DisplayName = book.DisplayName,
TextSize = book.TextSize,
Expand Down
13 changes: 11 additions & 2 deletions src/Aquifer.API/Modules/Passages/PassagesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Aquifer.API.Modules.Passages;

public class PassagesModule : IModule
{
private static readonly ResourceEntityType[] AllowedResourceTypes = { ResourceEntityType.CBBTER };

public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
{
var group = endpoints.MapGroup("passages");
Expand All @@ -16,11 +18,17 @@ public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
return endpoints;
}

private async Task<Ok<List<PassagesBookResponse>>> GetPassagesByLanguageAndResource(int languageId,
private async Task<Results<Ok<List<PassagesBookResponse>>, NotFound>> GetPassagesByLanguageAndResource(
int languageId,
ResourceEntityType resourceType,
AquiferDbContext dbContext,
CancellationToken cancellationToken)
{
if (!AllowedResourceTypes.Contains(resourceType))
{
return TypedResults.NotFound();
}

var passagesByBook = (await dbContext.Passages
.Where(p => p.PassageResources.Any(pr =>
pr.Resource.Type == resourceType &&
Expand Down Expand Up @@ -49,7 +57,7 @@ private async Task<Results<Ok<PassageDetailsResponse>, NotFound>> GetPassageDeta
AquiferDbContext dbContext,
CancellationToken cancellationToken)
{
var passage = await dbContext.Passages.FindAsync(new object?[] { passageId }, cancellationToken);
var passage = await dbContext.Passages.FindAsync(passageId, cancellationToken);
if (passage == null)
{
return TypedResults.NotFound();
Expand Down Expand Up @@ -102,6 +110,7 @@ private async Task<Results<Ok<PassageDetailsResponse>, NotFound>> GetPassageDeta
PassageStartDetails = BibleUtilities.TranslateVerseId(passage.StartVerseId),
PassageEndDetails = BibleUtilities.TranslateVerseId(passage.EndVerseId),
Contents = passageResourceContent.Concat(verseResourceContent).Concat(supportingResourceContent)
.DistinctBy(rc => rc.ContentId)
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ public class ResourceContentUrlJsonSchema

public class ResourceContentAudioJsonSchema
{
public ResourceContentAudioTypeJsonSchema Webm { get; set; } = null!;
public ResourceContentAudioTypeJsonSchema Mp3 { get; set; } = null!;
}

public class ResourceContentAudioTypeJsonSchema
{
public string Url { get; set; } = null!;
public ResourceContentUrlJsonSchema Webm { get; set; } = null!;
public ResourceContentUrlJsonSchema Mp3 { get; set; } = null!;
}
8 changes: 7 additions & 1 deletion src/Aquifer.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using Aquifer.API.Modules;
using Aquifer.API.Services;
using Aquifer.Data;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration.Get<ConfigurationOptions>();
Expand All @@ -13,7 +15,11 @@
.AddApplicationInsightsTelemetry()
.AddDbContext<AquiferDbContext>(options =>
options.UseSqlServer(configuration?.ConnectionStrings?.BiblioNexusDb))
.RegisterModules();
.RegisterModules()
.Configure<JsonOptions>(options =>
{
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

var app = builder.Build();

Expand Down
101 changes: 1 addition & 100 deletions src/Aquifer.API/Utilities/BibleUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Aquifer.API.Utilities;
namespace Aquifer.API.Utilities;

public static class BibleUtilities
{
[JsonConverter(typeof(BookCodeConverter))]
public enum BookCode
{
None = 0,
GEN = 1,
EXO = 2,
LEV = 3,
NUM = 4,
DEU = 5,
JOS = 6,
JDG = 7,
RUT = 8,
_1SA = 9,
_2SA = 10,
_1KI = 11,
_2KI = 12,
_1CH = 13,
_2CH = 14,
EZR = 15,
NEH = 16,
EST = 17,
JOB = 18,
PSA = 19,
PRO = 20,
ECC = 21,
SNG = 22,
ISA = 23,
JER = 24,
LAM = 25,
EZK = 26,
DAN = 27,
HOS = 28,
JOL = 29,
AMO = 30,
OBA = 31,
JON = 32,
MIC = 33,
NAM = 34,
HAB = 35,
ZEP = 36,
HAG = 37,
ZEC = 38,
MAL = 39,
MAT = 41,
MRK = 42,
LUK = 43,
JHN = 44,
ACT = 45,
ROM = 46,
_1CO = 47,
_2CO = 48,
GAL = 49,
EPH = 50,
PHP = 51,
COL = 52,
_1TH = 53,
_2TH = 54,
_1TI = 55,
_2TI = 56,
TIT = 57,
PHM = 58,
HEB = 59,
JAS = 60,
_1PE = 61,
_2PE = 62,
_1JN = 63,
_2JN = 64,
_3JN = 65,
JUD = 66,
REV = 67
}

public static (int bookId, int chapter, int verse) TranslateVerseId(int verseId)
{
int verse = verseId % 1000;
Expand All @@ -95,28 +20,4 @@ public static int UpperBoundOfBook(int bookId)
{
return (bookId * 1000000) + 1000999999;
}

public static BookCode BookIdToCode(int bookId)
{
return (BookCode)bookId;
}
}

public class BookCodeConverter : JsonConverter<BibleUtilities.BookCode>
{
public override BibleUtilities.BookCode Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
var value = reader.GetString();
if (value == null)
{
return BibleUtilities.BookCode.None;
}
return (BibleUtilities.BookCode)Enum.Parse(typeof(BibleUtilities.BookCode), value);
}

public override void Write(Utf8JsonWriter writer, BibleUtilities.BookCode value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString().Replace("_", ""));
}
}
3 changes: 3 additions & 0 deletions src/Aquifer.Data/Aquifer.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11"/>
</ItemGroup>




</Project>
7 changes: 4 additions & 3 deletions src/Aquifer.Data/Entities/BibleBookContentEntity.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Aquifer.Data.Enums;
using Microsoft.EntityFrameworkCore;

namespace Aquifer.Data.Entities;

[PrimaryKey(nameof(BibleId), nameof(BookId))]
public class BibleBookContentEntity
{
public int BibleId { get; set; }
public int BookId { get; set; }
public BookId BookId { get; set; }
public string DisplayName { get; set; } = null!;
public string TextUrl { get; set; } = null!;
public string AudioUrls { get; set; } = null!; // JSON
Expand All @@ -21,4 +22,4 @@ public class BibleBookContentEntity
public DateTime Updated { get; set; } = DateTime.UtcNow;

public BibleEntity Bible { get; set; } = null!;
}
}
1 change: 0 additions & 1 deletion src/Aquifer.Data/Entities/ResourceContentEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class ResourceContentEntity
public ResourceEntity Resource { get; set; } = null!;
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ResourceContentMediaType
{
None = 0,
Expand Down
1 change: 0 additions & 1 deletion src/Aquifer.Data/Entities/ResourceEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void Configure(EntityTypeBuilder<ResourceEntity> builder)
}
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ResourceEntityType
{
None = 0,
Expand Down
Loading

0 comments on commit 03fd007

Please sign in to comment.