Skip to content

Commit f666054

Browse files
committed
Ordering.Infrastructure done
1 parent d9d9d43 commit f666054

File tree

7 files changed

+195
-10
lines changed

7 files changed

+195
-10
lines changed

src/Ordering/Ordering.Core/Repositories/IOrderRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace Ordering.Core.Repositories
77
{
88
public interface IOrderRepository : IRepository<Order>
99
{
10-
Task<IEnumerable<Order>> GetOrderListAsync();
10+
Task<IEnumerable<Order>> GetOrdersByUserName(string userName);
1111
}
1212
}

src/Ordering/Ordering.Infrastructure/Class1.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Ordering.Core.Entities;
3+
4+
namespace Ordering.Infrastructure.Data
5+
{
6+
public class OrderContext : DbContext
7+
{
8+
public OrderContext(DbContextOptions options) : base(options)
9+
{
10+
}
11+
12+
public DbSet<Order> Orders { get; set; }
13+
}
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.Extensions.Logging;
2+
using Ordering.Core.Entities;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Ordering.Infrastructure.Data
9+
{
10+
public class OrderContextSeed
11+
{
12+
public static async Task SeedAsync(OrderContext aspnetrunContext, ILoggerFactory loggerFactory, int? retry = 0)
13+
{
14+
int retryForAvailability = retry.Value;
15+
16+
try
17+
{
18+
// TODO: Only run this if using a real database
19+
// aspnetrunContext.Database.Migrate();
20+
// aspnetrunContext.Database.EnsureCreated();
21+
22+
if (!aspnetrunContext.Orders.Any())
23+
{
24+
aspnetrunContext.Orders.AddRange(GetPreconfiguredOrders());
25+
await aspnetrunContext.SaveChangesAsync();
26+
}
27+
}
28+
catch (Exception exception)
29+
{
30+
if (retryForAvailability < 5)
31+
{
32+
retryForAvailability++;
33+
var log = loggerFactory.CreateLogger<OrderContextSeed>();
34+
log.LogError(exception.Message);
35+
await SeedAsync(aspnetrunContext, loggerFactory, retryForAvailability);
36+
}
37+
throw;
38+
}
39+
}
40+
41+
private static IEnumerable<Order> GetPreconfiguredOrders()
42+
{
43+
return new List<Order>()
44+
{
45+
new Order() { FirstName = "swn", LastName = "swn" },
46+
new Order() { FirstName = "swn2", LastName = "swn2" }
47+
};
48+
}
49+
}
50+
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
</PackageReference>
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\Ordering.Core\Ordering.Core.csproj" />
23+
</ItemGroup>
24+
725
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Ordering.Core.Entities.Base;
3+
using Ordering.Core.Repositories.Base;
4+
using Ordering.Infrastructure.Data;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Linq.Expressions;
9+
using System.Threading.Tasks;
10+
11+
namespace Ordering.Infrastructure.Repository.Base
12+
{
13+
public class Repository<T> : IRepository<T> where T : Entity
14+
{
15+
protected readonly OrderContext _dbContext;
16+
17+
public Repository(OrderContext dbContext)
18+
{
19+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
20+
}
21+
22+
public async Task<IReadOnlyList<T>> GetAllAsync()
23+
{
24+
return await _dbContext.Set<T>().ToListAsync();
25+
}
26+
27+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
28+
{
29+
return await _dbContext.Set<T>().Where(predicate).ToListAsync();
30+
}
31+
32+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeString = null, bool disableTracking = true)
33+
{
34+
IQueryable<T> query = _dbContext.Set<T>();
35+
if (disableTracking) query = query.AsNoTracking();
36+
37+
if (!string.IsNullOrWhiteSpace(includeString)) query = query.Include(includeString);
38+
39+
if (predicate != null) query = query.Where(predicate);
40+
41+
if (orderBy != null)
42+
return await orderBy(query).ToListAsync();
43+
return await query.ToListAsync();
44+
}
45+
46+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, List<Expression<Func<T, object>>> includes = null, bool disableTracking = true)
47+
{
48+
IQueryable<T> query = _dbContext.Set<T>();
49+
if (disableTracking) query = query.AsNoTracking();
50+
51+
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include));
52+
53+
if (predicate != null) query = query.Where(predicate);
54+
55+
if (orderBy != null)
56+
return await orderBy(query).ToListAsync();
57+
return await query.ToListAsync();
58+
}
59+
60+
public virtual async Task<T> GetByIdAsync(int id)
61+
{
62+
return await _dbContext.Set<T>().FindAsync(id);
63+
}
64+
65+
public async Task<T> AddAsync(T entity)
66+
{
67+
_dbContext.Set<T>().Add(entity);
68+
await _dbContext.SaveChangesAsync();
69+
return entity;
70+
}
71+
72+
public async Task UpdateAsync(T entity)
73+
{
74+
_dbContext.Entry(entity).State = EntityState.Modified;
75+
await _dbContext.SaveChangesAsync();
76+
}
77+
78+
public async Task DeleteAsync(T entity)
79+
{
80+
_dbContext.Set<T>().Remove(entity);
81+
await _dbContext.SaveChangesAsync();
82+
}
83+
}
84+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Ordering.Core.Entities;
3+
using Ordering.Core.Repositories;
4+
using Ordering.Infrastructure.Data;
5+
using Ordering.Infrastructure.Repository.Base;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace Ordering.Infrastructure.Repository
11+
{
12+
public class OrderRepository : Repository<Order>, IOrderRepository
13+
{
14+
public OrderRepository(OrderContext dbContext) : base(dbContext)
15+
{
16+
}
17+
18+
public async Task<IEnumerable<Order>> GetOrdersByUserName(string userName)
19+
{
20+
var orderList = await _dbContext.Orders
21+
.Where(o => o.UserName == userName)
22+
.ToListAsync();
23+
24+
return orderList;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)