Skip to content

Commit dfbb5a7

Browse files
committed
Fix bugs on post api and refactored
1 parent 14c3036 commit dfbb5a7

File tree

13 files changed

+48
-26
lines changed

13 files changed

+48
-26
lines changed

OnionArchitecture/OA.Data/CustomerContext.cs renamed to OnionArchitecture/OA.Data/ApplicationContext.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using Microsoft.EntityFrameworkCore;
22
using OA.Domain.Entities;
3+
using System.Threading.Tasks;
34

45
namespace OA.Data
56
{
6-
public class CustomerContext : DbContext
7+
public class ApplicationContext : DbContext, IApplicationContext
78
{
89
// This constructor is used of runit testing
9-
public CustomerContext()
10+
public ApplicationContext()
1011
{
1112

1213
}
13-
public CustomerContext(DbContextOptions options) : base(options)
14+
public ApplicationContext(DbContextOptions options) : base(options)
1415
{
1516
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
1617
}
@@ -35,5 +36,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3536
}
3637

3738
}
39+
40+
public async Task<int> SaveChangesAsync()
41+
{
42+
return await base.SaveChangesAsync();
43+
}
3844
}
3945
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using OA.Domain.Entities;
3+
using System.Threading.Tasks;
4+
5+
namespace OA.Data
6+
{
7+
public interface IApplicationContext
8+
{
9+
DbSet<Category> Categories { get; set; }
10+
DbSet<Customer> Customers { get; set; }
11+
DbSet<Order> Orders { get; set; }
12+
DbSet<Product> Products { get; set; }
13+
DbSet<Supplier> Suppliers { get; set; }
14+
15+
Task<int> SaveChangesAsync();
16+
}
17+
}

OnionArchitecture/OA.Data/Migrations/20200627044955_Initial-setup.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OnionArchitecture/OA.Data/Migrations/CustomerContextModelSnapshot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace OA.Data.Migrations
1010
{
11-
[DbContext(typeof(CustomerContext))]
11+
[DbContext(typeof(ApplicationContext))]
1212
partial class CustomerContextModelSnapshot : ModelSnapshot
1313
{
1414
protected override void BuildModel(ModelBuilder modelBuilder)

OnionArchitecture/OA.Infrastructure/Extension/ConfigureServiceContainer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using System.IO;
1414
using System.Reflection;
15+
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
1516

1617
namespace OA.Infrastructure.Extension
1718
{
@@ -20,7 +21,7 @@ public static class ConfigureServiceContainer
2021
public static void AddDbContext(this IServiceCollection serviceCollection,
2122
IConfiguration configuration, IConfigurationRoot configRoot)
2223
{
23-
serviceCollection.AddDbContext<CustomerContext>(options =>
24+
serviceCollection.AddDbContext<ApplicationContext>(options =>
2425
options.UseSqlServer(configuration.GetConnectionString("OnionArchConn") ?? configRoot["ConnectionStrings:OnionArchConn"])
2526
);
2627
}
@@ -84,6 +85,10 @@ public static void AddMailSetting(this IServiceCollection serviceCollection,
8485
serviceCollection.Configure<MailSettings>(configuration.GetSection("MailSettings"));
8586
}
8687

88+
public static void AddNewtonJson(this IServiceCollection serviceCollection)
89+
{
90+
serviceCollection.AddControllers().AddNewtonsoftJson();
91+
}
8792

8893
}
8994
}

OnionArchitecture/OA.Infrastructure/OA.Infrastructure.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
1415
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5">
1516
<PrivateAssets>all</PrivateAssets>
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

OnionArchitecture/OA.Persistence/Repository/CustomerRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace OA.Persistence.Repository
1010
{
1111
public class CustomerRepository : ICustomerRepository
1212
{
13-
private readonly CustomerContext _context;
14-
public CustomerRepository(CustomerContext context)
13+
private readonly ApplicationContext _context;
14+
public CustomerRepository(ApplicationContext context)
1515
{
1616
_context = context;
1717
}

OnionArchitecture/OA.Persistence/Repository/GenericRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace OA.Persistence.Repository
99
public class GenericRepository<T> : IGenericRepository<T> where T : class
1010
{
1111
private readonly DbSet<T> entities;
12-
private readonly CustomerContext _context;
12+
private readonly ApplicationContext _context;
1313

14-
public GenericRepository(CustomerContext context)
14+
public GenericRepository(ApplicationContext context)
1515
{
1616
_context = context;
1717
entities = _context.Set<T>();

OnionArchitecture/OA.Test.Unit/Data/CustomerContextTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CustomerContextTest
1212
public void CanInsertCustomerIntoDatabasee()
1313
{
1414

15-
using var context = new CustomerContext();
15+
using var context = new ApplicationContext();
1616
var customer = new Customer();
1717
context.Customers.Add(customer);
1818
Assert.AreEqual(EntityState.Added, context.Entry(customer).State);

OnionArchitecture/OA.Test.Unit/Persistence/CustomerRepositoryTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CustomerRepositoryTest
1212
{
1313
private DbContextOptionsBuilder builder;
1414

15-
private CustomerContext context;
15+
private ApplicationContext context;
1616

1717
[SetUp]
1818
public void Setup()
@@ -44,7 +44,7 @@ public async Task CheckCustomerRepositoryGetCustomerAsync()
4444
void SettingUp()
4545
{
4646
// Inserting to inmemory database
47-
context = new CustomerContext(builder.Options);
47+
context = new ApplicationContext(builder.Options);
4848
var customerRepository = new GenericRepository<Customer>(context);
4949
customerRepository.Add(new Customer { CustomerName = "Shweta Naik", Address = "Bangalore" });
5050
customerRepository.Add(new Customer { CustomerName = "Amit Naik", Address = "Bangalore" });

0 commit comments

Comments
 (0)