Skip to content

Commit

Permalink
refactor (Query): Change intent of query to SingleOrDefault as querie…
Browse files Browse the repository at this point in the history
…s are on Id.
  • Loading branch information
jafin authored and rnwood committed May 11, 2021
1 parent a6d78bd commit 51b21ea
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Rnwood.Smtp4dev/ApiModel/Message.cs
Expand Up @@ -183,7 +183,7 @@ internal static string GetPartSource(Message message, string id)

private static MimeEntity GetPart(Message message, string id)
{
MessageEntitySummary part = message.Parts.Flatten(p => p.ChildParts).FirstOrDefault(p => p.Id == id);
MessageEntitySummary part = message.Parts.Flatten(p => p.ChildParts).SingleOrDefault(p => p.Id == id);

if (part == null)
{
Expand Down
4 changes: 2 additions & 2 deletions Rnwood.Smtp4dev/Controllers/MessagesController.cs
Expand Up @@ -45,7 +45,7 @@ public IEnumerable<ApiModel.MessageSummary> GetSummaries(string sortColumn = "re

private DbModel.Message GetDbMessage(Guid id)
{
return messagesRepository.GetMessages().FirstOrDefault(m => m.Id == id) ??
return messagesRepository.GetMessages().SingleOrDefault(m => m.Id == id) ??
throw new FileNotFoundException($"Message with id {id} was not found.");
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public string GetMessageHtml(Guid id)
{
string cid = imageElement.Attributes["src"].Value.Replace("cid:", "", StringComparison.OrdinalIgnoreCase);

var part = message.Parts.Flatten(p => p.ChildParts).FirstOrDefault(p => p.ContentId == cid);
var part = message.Parts.Flatten(p => p.ChildParts).SingleOrDefault(p => p.ContentId == cid);

imageElement.Attributes["src"].Value = $"api/Messages/{id.ToString()}/part/{part?.Id ?? "notfound"}/content";
}
Expand Down
4 changes: 2 additions & 2 deletions Rnwood.Smtp4dev/Controllers/SessionsController.cs
Expand Up @@ -39,14 +39,14 @@ public IEnumerable<ApiModel.SessionSummary> GetSummaries()
[HttpGet("{id}")]
public ApiModel.Session GetSession(Guid id)
{
Session result = dbContext.Sessions.FirstOrDefault(m => m.Id == id);
Session result = dbContext.Sessions.SingleOrDefault(m => m.Id == id);
return new ApiModel.Session(result);
}

[HttpGet("{id}/log")]
public string GetSessionLog(Guid id)
{
Session result = dbContext.Sessions.FirstOrDefault(m => m.Id == id);
Session result = dbContext.Sessions.SingleOrDefault(m => m.Id == id);
return result.Log;
}

Expand Down
1 change: 1 addition & 0 deletions Rnwood.Smtp4dev/Rnwood.Smtp4dev.csproj
Expand Up @@ -27,6 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="5.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions Rnwood.Smtp4dev/Server/ImapServer.cs
Expand Up @@ -30,7 +30,7 @@ public ImapServer(IMessagesRepository messagesRepository, IOptionsMonitor<Server

using var scope = serviceScopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetService<Smtp4devDbContext>();
if (dbContext.ImapState.FirstOrDefault() == null)
if (!dbContext.ImapState.Any())
{
dbContext.Add(new ImapState
{
Expand Down Expand Up @@ -162,7 +162,7 @@ private void Session_Fetch(object sender, IMAP_e_Fetch e)
{
foreach (var msgInfo in e.MessagesInfo)
{
var dbMessage = this.messagesRepository.GetMessages().FirstOrDefault(m => m.Id == new Guid(msgInfo.ID));
var dbMessage = this.messagesRepository.GetMessages().SingleOrDefault(m => m.Id == new Guid(msgInfo.ID));

if (dbMessage != null)
{
Expand Down
3 changes: 1 addition & 2 deletions Rnwood.Smtp4dev/Server/Smtp4devServer.cs
Expand Up @@ -260,8 +260,7 @@ internal Task DeleteSession(Guid id)
{
using var scope = serviceScopeFactory.CreateScope();
Smtp4devDbContext dbContext = scope.ServiceProvider.GetService<Smtp4devDbContext>();
Session session = dbContext.Sessions.FirstOrDefault(s => s.Id == id);
Session session = dbContext.Sessions.SingleOrDefault(s => s.Id == id);
if (session != null)
{
dbContext.Sessions.Remove(session);
Expand Down

0 comments on commit 51b21ea

Please sign in to comment.