Skip to content

Commit 9ba5a16

Browse files
authored
Notifications Service: .NET 8 (#627)
1 parent 6739ad2 commit 9ba5a16

File tree

51 files changed

+214
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+214
-83
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Threading.Tasks;
6+
using Microsoft.EntityFrameworkCore;
7+
using Z.EntityFramework.Plus;
8+
9+
namespace HwProj.Repositories.Net8;
10+
11+
public class CrudRepository<TEntity, TKey>(DbContext context) : ICrudRepository<TEntity, TKey>
12+
where TEntity : class, IEntity<TKey>
13+
where TKey : IEquatable<TKey>
14+
{
15+
protected DbContext Context => context;
16+
17+
public async Task<TKey> AddAsync(TEntity item)
18+
{
19+
await context.AddAsync(item);
20+
await context.SaveChangesAsync();
21+
return item.Id;
22+
}
23+
24+
public async Task<List<TKey>> AddRangeAsync(IEnumerable<TEntity> items)
25+
{
26+
items = items.ToList();
27+
await context.AddRangeAsync(items);
28+
await context.SaveChangesAsync();
29+
return items.Select(item => item.Id).ToList();
30+
}
31+
32+
public async Task DeleteAsync(TKey id)
33+
{
34+
await context.Set<TEntity>()
35+
.Where(entity => entity.Id.Equals(id))
36+
.DeleteAsync()
37+
;
38+
}
39+
40+
public async Task UpdateAsync(TKey id, Expression<Func<TEntity, TEntity>> updateFactory)
41+
{
42+
await context.Set<TEntity>()
43+
.Where(entity => entity.Id.Equals(id))
44+
.UpdateAsync(updateFactory)
45+
;
46+
}
47+
48+
public IQueryable<TEntity> GetAll()
49+
{
50+
return context.Set<TEntity>().AsNoTracking();
51+
}
52+
53+
public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> predicate)
54+
{
55+
return context.Set<TEntity>().AsNoTracking().Where(predicate);
56+
}
57+
58+
public async Task<TEntity> GetAsync(TKey id)
59+
{
60+
return await context.FindAsync<TEntity>(id);
61+
}
62+
63+
public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate)
64+
{
65+
return await context.Set<TEntity>().AsNoTracking().FirstOrDefaultAsync(predicate);
66+
}
67+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<LangVersion>12</LangVersion>
5+
<Nullable>disable</Nullable>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
9+
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="9.104.0" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Threading.Tasks;
6+
7+
namespace HwProj.Repositories.Net8;
8+
9+
public interface ICrudRepository<TEntity, TKey>
10+
where TEntity : IEntity<TKey>
11+
where TKey : IEquatable<TKey>
12+
{
13+
Task<TKey> AddAsync(TEntity item);
14+
Task<List<TKey>> AddRangeAsync(IEnumerable<TEntity> items);
15+
Task DeleteAsync(TKey id);
16+
Task UpdateAsync(TKey id, Expression<Func<TEntity, TEntity>> updateFactory);
17+
IQueryable<TEntity> GetAll();
18+
IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> predicate);
19+
Task<TEntity> GetAsync(TKey id);
20+
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace HwProj.Repositories.Net8;
4+
5+
public interface IEntity<TKey>
6+
where TKey : IEquatable<TKey>
7+
{
8+
TKey Id { get; set; }
9+
}

HwProj.CoursesService/HwProj.CoursesService.API/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ COPY ["HwProj.Common/HwProj.Repositories/", "HwProj.Common/HwProj.Repositories/"
1111
COPY ["HwProj.AuthService/HwProj.AuthService.Client/", "HwProj.AuthService/HwProj.AuthService.Client/"]
1212
COPY ["HwProj.ContentService/HwProj.ContentService.Client/", "HwProj.ContentService/HwProj.ContentService.Client/"]
1313
COPY ["HwProj.Common/HwProj.HttpUtils/", "HwProj.Common/HwProj.HttpUtils/"]
14+
COPY ["HwProj.NotificationsService/HwProj.NotificationService.Events/", "HwProj.NotificationsService/HwProj.NotificationService.Events/"]
1415

1516
WORKDIR "/src/HwProj.CoursesService/HwProj.CoursesService.API"
1617
RUN dotnet publish "HwProj.CoursesService.API.csproj" -c Release -o /app/publish

HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<ProjectReference Include="..\..\HwProj.AuthService\HwProj.AuthService.Client\HwProj.AuthService.Client.csproj" />
2727
<ProjectReference Include="..\..\HwProj.Common\HwProj.Repositories\HwProj.Repositories.csproj" />
2828
<ProjectReference Include="..\..\HwProj.Common\HwProj.Utils\HwProj.Utils.csproj" />
29+
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationService.Events\HwProj.NotificationService.Events.csproj" />
2930
</ItemGroup>
3031
<ProjectExtensions><VisualStudio><UserProperties properties_4launchsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
3132
</Project>

HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Transactions;
55
using System.Threading.Tasks;
6-
using HwProj.CoursesService.API.Events;
76
using HwProj.CoursesService.API.Models;
87
using HwProj.CoursesService.API.Repositories;
98
using HwProj.CoursesService.API.Repositories.Groups;
@@ -14,6 +13,7 @@
1413
using HwProj.CoursesService.API.Domains;
1514
using HwProj.Models.CoursesService.DTO;
1615
using HwProj.Models.Roles;
16+
using HwProj.NotificationService.Events.CoursesService;
1717
using Microsoft.EntityFrameworkCore;
1818
using Microsoft.EntityFrameworkCore.Internal;
1919

HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
using HwProj.CoursesService.API.Models;
55
using HwProj.CoursesService.API.Repositories;
66
using HwProj.EventBus.Client.Interfaces;
7-
using HwProj.CoursesService.API.Events;
87
using HwProj.CoursesService.API.Domains;
98
using HwProj.Models;
109
using HwProj.Models.CoursesService.ViewModels;
10+
using HwProj.NotificationService.Events.CoursesService;
1111

1212
namespace HwProj.CoursesService.API.Services
1313
{

HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Threading.Tasks;
3-
using HwProj.CoursesService.API.Events;
43
using HwProj.CoursesService.API.Models;
54
using HwProj.CoursesService.API.Repositories;
65
using HwProj.EventBus.Client.Interfaces;
76
using HwProj.CoursesService.API.Domains;
87
using System.Linq;
98
using HwProj.Models;
9+
using HwProj.NotificationService.Events.CoursesService;
1010

1111
namespace HwProj.CoursesService.API.Services
1212
{

HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerAcceptToCourseEvent.cs renamed to HwProj.NotificationsService/HwProj.NotificationService.Events/CoursesService/LecturerAcceptToCourseEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using HwProj.EventBus.Client;
22

3-
namespace HwProj.CoursesService.API.Events
3+
namespace HwProj.NotificationService.Events.CoursesService
44
{
55
public class LecturerAcceptToCourseEvent : Event
66
{

0 commit comments

Comments
 (0)