Skip to content

Commit

Permalink
fix: error message format
Browse files Browse the repository at this point in the history
  • Loading branch information
codingawayy committed Jul 11, 2018
1 parent e8cbcf0 commit da374cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
28 changes: 28 additions & 0 deletions Filer.EntityFrameworkCore/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace Filer.EntityFrameworkCore
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Filer.Core;
using Microsoft.EntityFrameworkCore;

public static class Extensions
{
Expand All @@ -18,5 +22,29 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
}
}
}

internal static File SingleOrException(this IQueryable<File> source, int fileId)
{
var file = source.SingleOrDefault(t => t.Id == fileId);

if (file == null)
{
throw new NullReferenceException($"File #{fileId} does not exist.");
}

return file;
}

internal static async Task<File> SingleOrExceptionAsync(this IQueryable<File> source, int fileId)
{
var file = await source.SingleOrDefaultAsync(t => t.Id == fileId);

if (file == null)
{
throw new NullReferenceException($"File #{fileId} does not exist.");
}

return file;
}
}
}
21 changes: 3 additions & 18 deletions Filer.EntityFrameworkCore/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ public async Task AttachFileToContextsAsync(int fileId, params string[] contexts
{
var file = await this.dbContext.Files
.Include(t => t.Contexts)
.SingleOrDefaultAsync(t => t.Id == fileId);

if (file == null)
{
throw new NullReferenceException($"File [id:{fileId}] does not exists");
}
.SingleOrExceptionAsync(fileId);

foreach (var context in contexts)
{
Expand Down Expand Up @@ -173,12 +168,7 @@ public void DetachFileFromContexts(int fileId, params string[] contexts)
{
var file = this.dbContext.Files
.Include(t => t.Contexts)
.SingleOrDefault(t => t.Id == fileId);

if (file == null)
{
throw new NullReferenceException($"File [id:{fileId}] does not exists");
}
.SingleOrException(fileId);

foreach (var context in contexts)
{
Expand Down Expand Up @@ -223,12 +213,7 @@ public void AttachFileToContexts(int fileId, params string[] contexts)
{
var file = this.dbContext.Files
.Include(t => t.Contexts)
.SingleOrDefault(t => t.Id == fileId);

if (file == null)
{
throw new NullReferenceException($"File [id:{fileId}] does not exists");
}
.SingleOrException(fileId);

foreach (var context in contexts)
{
Expand Down

0 comments on commit da374cd

Please sign in to comment.