Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions NoSQL/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ services:
- 5433:5432
environment:
- POSTGRES_PASSWORD=docker
#Administration Db mongo
promocode-factory-administration-db-mongo:
image: "mongo:latest"
container_name: 'promocode-factory-administration-db-mongo'
restart: always
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=docker

#Administration Api
promocode-factory-administration-api:
build: src/Pcf.Administration/
Expand All @@ -16,9 +27,11 @@ services:
ports:
- "8091:8080"
environment:
- "ConnectionStrings:PromocodeFactoryAdministrationDb=Host=promocode-factory-administration-db;Database=promocode_factory_administration_db;Username=postgres;Password=docker"
#- "ConnectionStrings:PromocodeFactoryAdministrationDb=Host=promocode-factory-administration-db;Database=promocode_factory_administration_db;Username=postgres;Password=docker"
- "ConnectionStrings:PromocodeFactoryAdministrationDbMongo=mongodb://root:docker@promocode-factory-administration-db-mongo:27018/promocode_factory_administration_db_mongo"
depends_on:
- promocode-factory-administration-db
#- promocode-factory-administration-db
- promocode-factory-administration-db-mongo

#ReceivingFromPartner Db
promocode-factory-receiving-from-partner-db:
Expand Down Expand Up @@ -52,6 +65,17 @@ services:
- 5435:5432
environment:
- POSTGRES_PASSWORD=docker
#Administration Db mongo
promocode-factory-giving-to-customer-db-mongo:
image: "mongo:latest"
container_name: 'promocode-factory-giving-to-customer-db-mongo'
restart: always
ports:
- 27019:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=docker

#GivingToCustomer Api
promocode-factory-giving-to-customer-api:
build: src/Pcf.GivingToCustomer/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
namespace Pcf.Administration.DataAccess.Data
using MongoDB.Driver;
using Pcf.Administration.Core.Domain.Administration;

namespace Pcf.Administration.DataAccess.Data
{
public class EfDbInitializer
: IDbInitializer
{
private readonly DataContext _dataContext;
private readonly IMongoDatabase _db;

public EfDbInitializer(DataContext dataContext)
public EfDbInitializer(DataContext dataContext, IMongoDatabase db)
{
_dataContext = dataContext;
_db = db;
}

public void InitializeDb()
{
_dataContext.Database.EnsureDeleted();
_dataContext.Database.EnsureCreated();

_dataContext.AddRange(FakeDataFactory.Employees);
_dataContext.SaveChanges();
/* _dataContext.Database.EnsureDeleted();
_dataContext.Database.EnsureCreated();

_dataContext.AddRange(FakeDataFactory.Employees);
_dataContext.SaveChanges();*/
string employeeCollectionName = typeof(Employee).Name;
_db.DropCollection(employeeCollectionName);
var collectionEmployee = _db.GetCollection<Employee>(employeeCollectionName);
collectionEmployee.InsertManyAsync(FakeDataFactory.Employees);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Pcf.Administration.Core.Abstractions.Repositories;
using Pcf.Administration.Core.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Pcf.Administration.DataAccess.Repositories
{
public class MongoRepository<T>
: IRepository<T>
where T : BaseEntity
{
private readonly IMongoDatabase _db;
private readonly IMongoCollection<T> _collection;

public MongoRepository(IMongoDatabase db)
{
_db = db;
_collection = db.GetCollection<T>(typeof(T).Name);
}

public async Task<IEnumerable<T>> GetAllAsync()
{
var entities = await _collection.Find("{}").ToListAsync();
return entities;
}

public async Task<T> GetByIdAsync(Guid id)
{
var entity = await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
return entity;
}
public async Task<IEnumerable<T>> GetRangeByIdsAsync(List<Guid> ids)
{
var entities = await _collection.FindAsync(x => ids.Contains(x.Id));
return await entities.ToListAsync();
}

public async Task<T> GetFirstWhere(Expression<Func<T, bool>> predicate)
{
return await _collection.Find(predicate).FirstOrDefaultAsync();
}

public async Task<IEnumerable<T>> GetWhere(Expression<Func<T, bool>> predicate)
{
return await (await _collection.FindAsync(predicate)).ToListAsync();
}

public async Task AddAsync(T entity)
{
await _collection.InsertOneAsync(entity);
}
public async Task UpdateAsync(T entity)
{
await _collection.FindOneAndReplaceAsync(x => x.Id == entity.Id, entity);
}

public async Task DeleteAsync(T entity)
{
await _collection.FindOneAndDeleteAsync(x => x.Equals(entity));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="MongoDB.Driver" Version="2.28.0" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.28.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="NSwag.AspNetCore" Version="14.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Pcf.Administration.DataAccess.Repositories;
using Pcf.Administration.Core.Domain.Administration;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
using MongoDB.Driver;

namespace Pcf.Administration.WebHost
{
Expand All @@ -34,7 +35,8 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddMvcOptions(x=>
x.SuppressAsyncSuffixInActionNames = false);
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
//services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IRepository<>), typeof(MongoRepository<>));
services.AddScoped<IDbInitializer, EfDbInitializer>();
services.AddDbContext<DataContext>(x =>
{
Expand All @@ -44,6 +46,10 @@ public void ConfigureServices(IServiceCollection services)
x.UseLazyLoadingProxies();
});

services.AddSingleton(new MongoClient(Configuration.GetConnectionString("PromocodeFactoryAdministrationDbMongo"))
.GetDatabase(Configuration.GetSection("mongoDbName").Value)
);

services.AddOpenApiDocument(options =>
{
options.Title = "PromoCode Factory Administration API Doc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings":{
"PromocodeFactoryAdministrationDb":"Host=localhost;Database=promocode_factory_administration_db;Username=postgres;Password=docker;Port=5433"
"mongoDbName": "promocode_factory_administration_db_mongo",
"ConnectionStrings": {
"PromocodeFactoryAdministrationDb": "Host=localhost;Database=promocode_factory_administration_db;Username=postgres;Password=docker;Port=5433",
"PromocodeFactoryAdministrationDbMongo": "mongodb://root:docker@localhost:27018"
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
using System.Threading.Tasks;
using MongoDB.Driver;
using Pcf.GivingToCustomer.Core.Domain;
using System.Threading.Tasks;

namespace Pcf.GivingToCustomer.DataAccess.Data
{
public class EfDbInitializer
: IDbInitializer
{
private readonly DataContext _dataContext;
private readonly IMongoDatabase _db;

public EfDbInitializer(DataContext dataContext)
public EfDbInitializer(DataContext dataContext, IMongoDatabase db)
{
_dataContext = dataContext;
_db = db;
}

public void InitializeDb()
{
_dataContext.Database.EnsureDeleted();
_dataContext.Database.EnsureCreated();
//_dataContext.Database.EnsureDeleted();
//_dataContext.Database.EnsureCreated();

_dataContext.AddRange(FakeDataFactory.Preferences);
_dataContext.SaveChanges();

_dataContext.AddRange(FakeDataFactory.Customers);
_dataContext.SaveChanges();
//_dataContext.AddRange(FakeDataFactory.Preferences);
//_dataContext.SaveChanges();

//_dataContext.AddRange(FakeDataFactory.Customers);
//_dataContext.SaveChanges();

_db.DropCollection(typeof(Preference).Name);
var collectionPreferences = _db.GetCollection<Preference>(typeof(Preference).Name);
collectionPreferences.InsertMany(FakeDataFactory.Preferences);

_db.DropCollection(typeof(Customer).Name);
var collectionCustomers = _db.GetCollection<Customer>(typeof(Customer).Name);
collectionCustomers.InsertMany(FakeDataFactory.Customers);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="MongoDB.Driver" Version="2.29.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Pcf.GivingToCustomer.Core.Abstractions.Repositories;
using Pcf.GivingToCustomer.Core.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Pcf.GivingToCustomer.DataAccess.Repositories
{
public class MongoRepository<T>
: IRepository<T>
where T : BaseEntity
{
private readonly IMongoDatabase _db;
private readonly IMongoCollection<T> _collection;

public MongoRepository(IMongoDatabase db)
{
_db = db;
_collection = db.GetCollection<T>(typeof(T).Name);
}

public async Task<IEnumerable<T>> GetAllAsync()
{
var entities = await _collection.Find("{}").ToListAsync();
return entities;
}

public async Task<T> GetByIdAsync(Guid id)
{
var entity = await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
return entity;
}
public async Task<IEnumerable<T>> GetRangeByIdsAsync(List<Guid> ids)
{
var entities = await _collection.FindAsync(x => ids.Contains(x.Id));
return await entities.ToListAsync();
}

public async Task<T> GetFirstWhere(Expression<Func<T, bool>> predicate)
{
return await _collection.Find(predicate).FirstOrDefaultAsync();
}

public async Task<IEnumerable<T>> GetWhere(Expression<Func<T, bool>> predicate)
{
return await (await _collection.FindAsync(predicate)).ToListAsync();
}

public async Task AddAsync(T entity)
{
await _collection.InsertOneAsync(entity);
}
public async Task UpdateAsync(T entity)
{
await _collection.FindOneAndReplaceAsync(x => x.Id == entity.Id, entity);
}

public async Task DeleteAsync(T entity)
{
await _collection.FindOneAndDeleteAsync(x => x.Equals(entity));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Pcf.GivingToCustomer.DataAccess.Repositories;
using Pcf.GivingToCustomer.Integration;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
using MongoDB.Driver;

namespace Pcf.GivingToCustomer.WebHost
{
Expand All @@ -35,7 +36,8 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddMvcOptions(x=>
x.SuppressAsyncSuffixInActionNames = false);
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
//services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IRepository<>), typeof(MongoRepository<>));
services.AddScoped<INotificationGateway, NotificationGateway>();
services.AddScoped<IDbInitializer, EfDbInitializer>();
services.AddDbContext<DataContext>(x =>
Expand All @@ -46,6 +48,10 @@ public void ConfigureServices(IServiceCollection services)
x.UseLazyLoadingProxies();
});

services.AddSingleton(new MongoClient(Configuration.GetConnectionString("PromocodeFactoryGivingTocustomerDbMongo"))
.GetDatabase(Configuration.GetSection("mongoDbName").Value)
);

services.AddOpenApiDocument(options =>
{
options.Title = "PromoCode Factory Giving To Customer API Doc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings":{
"PromocodeFactoryGivingToCustomerDb":"Host=localhost;Database=promocode_factory_givingToCustomer_db;Username=postgres;Password=docker;Port=5435"
"mongoDbName": "promocode_factory_giving_to_customer_db_mongo",
"ConnectionStrings": {
"PromocodeFactoryGivingToCustomerDb": "Host=localhost;Database=promocode_factory_givingToCustomer_db;Username=postgres;Password=docker;Port=5435",
"PromocodeFactoryGivingToCustomerDbMongo": "mongodb://root:docker@localhost:27019"
},
"AllowedHosts": "*"
}