Skip to content

Commit 9a58836

Browse files
committed
improvements
1 parent 4d9a23d commit 9a58836

File tree

13 files changed

+27
-70
lines changed

13 files changed

+27
-70
lines changed

src/Extensions/WebAppBuilderExtension.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Globalization;
3-
using System.Reflection;
42
using System.Text.Json;
53
using System.Text.Json.Serialization;
6-
using FluentValidation;
7-
using Microsoft.AspNetCore.Builder;
84
using Microsoft.AspNetCore.Http.Json;
9-
using Microsoft.Extensions.DependencyInjection;
105
using Microsoft.OpenApi.Models;
116
using Serilog;
12-
using Microsoft.AspNetCore.Builder;
13-
using Microsoft.AspNetCore.Hosting;
14-
using Microsoft.Extensions.DependencyInjection;
15-
using Microsoft.Extensions.Hosting;
16-
using System;
17-
using Microsoft.EntityFrameworkCore;
187
using System.Threading.RateLimiting;
19-
using Microsoft.AspNetCore.RateLimiting;
20-
using Microsoft.AspNetCore.Http;
21-
using Microsoft.Extensions.Configuration;
228
using Microsoft.AspNetCore.Authentication.JwtBearer;
239
using Microsoft.IdentityModel.Tokens;
2410
using NetAPI.Infrastructure;

src/Extensions/WebAppExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Globalization;
3-
using Microsoft.AspNetCore.Builder;
42
using Serilog;
5-
using NetAPI.Features.Posts;
6-
using Microsoft.OpenApi.Models;
7-
using NetAPI.Common.Api;
83
using NetAPI.Features;
94
using NetAPI.Features.GraphQL.Configuration;
105

src/Features/GraphQL/Configuration/GraphQLConfiguration.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
using HotChocolate.Diagnostics;
2-
using HotChocolate.Execution.Configuration;
3-
using HotChocolate.Execution;
4-
using HotChocolate.Language;
5-
using HotChocolate.AspNetCore;
6-
using HotChocolate.AspNetCore.Extensions;
71
using Microsoft.EntityFrameworkCore;
82
using NetAPI.Features.GraphQL.Data;
93
using NetAPI.Features.GraphQL.DataLoaders;

src/Features/GraphQL/Configuration/GraphQLVoyagerExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using Microsoft.AspNetCore.Http;
2-
using System.Text;
3-
41
namespace NetAPI.Features.GraphQL.Configuration;
52

63
/// <summary>

src/Features/GraphQL/DataLoaders/BlogDataLoaders.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public BlogByIdDataLoader(
1515
IDbContextFactory<BlogDbContext> dbContextFactory,
1616
IBatchScheduler batchScheduler,
1717
DataLoaderOptions? options = null)
18-
: base(batchScheduler, options)
18+
: base(batchScheduler, options ?? new DataLoaderOptions())
1919
{
2020
_dbContextFactory = dbContextFactory;
2121
}
@@ -76,7 +76,7 @@ public PostByIdDataLoader(
7676
IDbContextFactory<BlogDbContext> dbContextFactory,
7777
IBatchScheduler batchScheduler,
7878
DataLoaderOptions? options = null)
79-
: base(batchScheduler, options)
79+
: base(batchScheduler, options ?? new DataLoaderOptions())
8080
{
8181
_dbContextFactory = dbContextFactory;
8282
}

src/Features/GraphQL/Resolvers/FieldResolvers.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using HotChocolate;
21
using NetAPI.Features.GraphQL.DataLoaders;
32
using NetAPI.Features.GraphQL.Models;
43
using NetAPI.Features.GraphQL.Services;
@@ -44,7 +43,7 @@ public async Task<IEnumerable<CommentType>> GetRecentCommentsAsync(
4443
CancellationToken cancellationToken)
4544
{
4645
var comments = await commentLoader.LoadAsync(blog.Id, cancellationToken);
47-
return comments.Take(10).Select(MapCommentToType); // Return only recent 10 comments
46+
return comments?.Take(10).Select(MapCommentToType) ?? Enumerable.Empty<CommentType>(); // Return only recent 10 comments
4847
}
4948

5049
/// <summary>
@@ -56,7 +55,7 @@ public async Task<int> GetPublishedPostCountAsync(
5655
CancellationToken cancellationToken)
5756
{
5857
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
59-
return posts.Count(p => p.IsPublished);
58+
return posts?.Count(p => p.IsPublished) ?? 0;
6059
}
6160

6261
/// <summary>
@@ -68,7 +67,7 @@ public async Task<int> GetDraftPostCountAsync(
6867
CancellationToken cancellationToken)
6968
{
7069
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
71-
return posts.Count(p => !p.IsPublished);
70+
return posts?.Count(p => !p.IsPublished) ?? 0;
7271
}
7372

7473
/// <summary>
@@ -80,7 +79,7 @@ public async Task<int> GetTotalViewsAsync(
8079
CancellationToken cancellationToken)
8180
{
8281
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
83-
return posts.Sum(p => p.ViewCount);
82+
return posts?.Sum(p => p.ViewCount) ?? 0;
8483
}
8584

8685
/// <summary>
@@ -92,7 +91,7 @@ public async Task<int> GetTotalLikesAsync(
9291
CancellationToken cancellationToken)
9392
{
9493
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
95-
return posts.Sum(p => p.LikeCount);
94+
return posts?.Sum(p => p.LikeCount) ?? 0;
9695
}
9796

9897
/// <summary>
@@ -105,7 +104,7 @@ public async Task<int> GetTotalLikesAsync(
105104
CancellationToken cancellationToken)
106105
{
107106
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
108-
var mostPopular = posts.OrderByDescending(p => p.ViewCount).FirstOrDefault();
107+
var mostPopular = posts?.OrderByDescending(p => p.ViewCount).FirstOrDefault();
109108

110109
if (mostPopular == null) return null;
111110

@@ -122,7 +121,7 @@ public async Task<int> GetTotalLikesAsync(
122121
CancellationToken cancellationToken)
123122
{
124123
var posts = await postLoader.LoadAsync(blog.Id, cancellationToken);
125-
var latest = posts.OrderByDescending(p => p.CreatedAt).FirstOrDefault();
124+
var latest = posts?.OrderByDescending(p => p.CreatedAt).FirstOrDefault();
126125

127126
if (latest == null) return null;
128127

@@ -226,7 +225,7 @@ public async Task<IEnumerable<CommentType>> GetCommentsAsync(
226225
CancellationToken cancellationToken)
227226
{
228227
var comments = await commentLoader.LoadAsync(post.Id, cancellationToken);
229-
return comments.Select(MapCommentToType);
228+
return comments?.Select(MapCommentToType) ?? Enumerable.Empty<CommentType>();
230229
}
231230

232231
/// <summary>
@@ -238,7 +237,7 @@ public async Task<IEnumerable<CommentType>> GetApprovedCommentsAsync(
238237
CancellationToken cancellationToken)
239238
{
240239
var comments = await commentLoader.LoadAsync(post.Id, cancellationToken);
241-
return comments.Where(c => c.IsApproved).Select(MapCommentToType);
240+
return comments?.Where(c => c.IsApproved).Select(MapCommentToType) ?? Enumerable.Empty<CommentType>();
242241
}
243242

244243
/// <summary>
@@ -250,10 +249,10 @@ public async Task<IEnumerable<CommentType>> GetRecentCommentsAsync(
250249
CancellationToken cancellationToken)
251250
{
252251
var comments = await commentLoader.LoadAsync(post.Id, cancellationToken);
253-
return comments.Where(c => c.IsApproved)
252+
return comments?.Where(c => c.IsApproved)
254253
.OrderByDescending(c => c.CreatedAt)
255254
.Take(5)
256-
.Select(MapCommentToType);
255+
.Select(MapCommentToType) ?? Enumerable.Empty<CommentType>();
257256
}
258257

259258
/// <summary>
@@ -394,9 +393,9 @@ public TimeSpan GetTimeAgo([Parent] CommentType comment)
394393
}
395394

396395
// Helper methods
397-
private static async Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService blogService, CancellationToken cancellationToken)
396+
private static Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService blogService, CancellationToken cancellationToken)
398397
{
399-
return new BlogType
398+
return Task.FromResult(new BlogType
400399
{
401400
Id = blog.Id,
402401
Title = blog.Title,
@@ -410,12 +409,12 @@ private static async Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService b
410409
PostCount = blog.Posts?.Count ?? 0,
411410
CommentCount = blog.Comments?.Count ?? 0,
412411
LastPostDate = blog.Posts?.OrderByDescending(p => p.CreatedAt).FirstOrDefault()?.CreatedAt
413-
};
412+
});
414413
}
415414

416-
private static async Task<PostType> MapPostToTypeAsync(Post post, IPostService postService, CancellationToken cancellationToken)
415+
private static Task<PostType> MapPostToTypeAsync(Post post, IPostService postService, CancellationToken cancellationToken)
417416
{
418-
return new PostType
417+
return Task.FromResult(new PostType
419418
{
420419
Id = post.Id,
421420
Title = post.Title,
@@ -434,7 +433,7 @@ private static async Task<PostType> MapPostToTypeAsync(Post post, IPostService p
434433
CommentCount = post.Comments?.Count ?? 0,
435434
ReadingTime = CalculateReadingTime(post.Content),
436435
Slug = GenerateSlug(post.Title)
437-
};
436+
});
438437
}
439438

440439
private static CommentType MapCommentToType(Comment comment)

src/Features/GraphQL/Resolvers/Mutation.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using GqlAuthorize = HotChocolate.Authorization.AuthorizeAttribute;
2-
using Microsoft.AspNetCore.Authorization;
32
using NetAPI.Features.GraphQL.Services;
43
using NetAPI.Features.GraphQL.Types;
5-
using System.Security.Claims;
64

75
namespace NetAPI.Features.GraphQL.Resolvers;
86

src/Features/GraphQL/Resolvers/Query.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NetAPI.Features.GraphQL.Models;
33
using NetAPI.Features.GraphQL.Services;
44
using NetAPI.Features.GraphQL.Types;
5-
using System.Security.Claims;
65

76
namespace NetAPI.Features.GraphQL.Resolvers;
87

@@ -181,9 +180,9 @@ public async Task<SearchResultType> SearchAsync(
181180
}
182181

183182
// Helper methods for mapping entities to GraphQL types
184-
private static async Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService blogService, CancellationToken cancellationToken)
183+
private static Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService blogService, CancellationToken cancellationToken)
185184
{
186-
return new BlogType
185+
return Task.FromResult(new BlogType
187186
{
188187
Id = blog.Id,
189188
Title = blog.Title,
@@ -197,12 +196,12 @@ private static async Task<BlogType> MapBlogToTypeAsync(Blog blog, IBlogService b
197196
PostCount = blog.Posts?.Count ?? 0,
198197
CommentCount = blog.Comments?.Count ?? 0,
199198
LastPostDate = blog.Posts?.OrderByDescending(p => p.CreatedAt).FirstOrDefault()?.CreatedAt
200-
};
199+
});
201200
}
202201

203-
private static async Task<PostType> MapPostToTypeAsync(Post post, IPostService postService, CancellationToken cancellationToken)
202+
private static Task<PostType> MapPostToTypeAsync(Post post, IPostService postService, CancellationToken cancellationToken)
204203
{
205-
return new PostType
204+
return Task.FromResult(new PostType
206205
{
207206
Id = post.Id,
208207
Title = post.Title,
@@ -221,7 +220,7 @@ private static async Task<PostType> MapPostToTypeAsync(Post post, IPostService p
221220
CommentCount = post.Comments?.Count ?? 0,
222221
ReadingTime = CalculateReadingTime(post.Content),
223222
Slug = GenerateSlug(post.Title)
224-
};
223+
});
225224
}
226225

227226
private static CommentType MapCommentToType(Comment comment)

src/Features/GraphQL/Resolvers/Subscription.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using NetAPI.Features.GraphQL.DataLoaders;
21
using NetAPI.Features.GraphQL.Models;
32
using NetAPI.Features.GraphQL.Types;
43

src/Features/GraphQL/Services/BlogService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using NetAPI.Features.GraphQL.Data;
33
using NetAPI.Features.GraphQL.Models;
44
using NetAPI.Features.GraphQL.Types;
5-
using System.Text.RegularExpressions;
65

76
namespace NetAPI.Features.GraphQL.Services;
87

0 commit comments

Comments
 (0)