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
2 changes: 1 addition & 1 deletion src/app/Repositories/IOrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace app.Repositories;
public interface IOrderRepository
{
event Action<Order> OrderProcessed;
void Add(Order order);
Order? Add(Order order);
Order? GetById(int id);
IReadOnlyCollection<Order> GetAll();
}
4 changes: 4 additions & 0 deletions src/app/Repositories/IProductsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public interface IProductsRepository
{
IReadOnlyCollection<Product> GetAll();
Product? GetById(int Id);
IList<Product> GetProductByOrderId(int Id);
Product? Add(Product product);
bool Update(Product product);

}
32 changes: 19 additions & 13 deletions src/app/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,37 @@ public OrderService(IOrderRepository orderRepository)

public Order RegisterOrder(Order order)
{
if(_orderRepository.GetById(order.Id) is null)
_orderRepository.Add(order);
var orderAdd = _orderRepository.Add(order);

return order;
return orderAdd;
}

public IReadOnlyCollection<Order> GetAll()
{
return _orderRepository.GetAll();
}

public Task ProcessOrderAsync(int orderId)
public async Task ProcessOrderAsync(int orderId)
{
// Asynchronously process the order
// Simulates a long CPU bound calculation
// TODO: Retrieve the order with the corresponding Id from the repository
Thread.Sleep(1400);
// TODO: Change the status of this order
throw new NotImplementedException();
var order = _orderRepository.GetById(orderId);

if (order is not null)
{
order.ChangeOrderStatus(true);
_orderRepository.OrderProcessed += HandleOrderProcessed;
}

}

private void HandleOrderProcessed(Order order)
{
// Notify the user that an order has been processed
// TODO: Write the order informations to the console
throw new NotImplementedException();
Console.WriteLine($" {order.Processed} Total Price: {order.TotalPrice}");
foreach (var product in order.Products)
{
Console.WriteLine($"Id:\t{product.Id}");
Console.WriteLine($"Name:\t{product.Name}");
Console.WriteLine($"Price:\t{product.Price}");
Console.WriteLine("___________________________________________");
}
}
}
9 changes: 9 additions & 0 deletions src/app/Services/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ public IReadOnlyCollection<Product> GetAll()
{
return _productsRepository.GetById(Id);
}

public Product? Add(Product product)
{
return _productsRepository.Add(product);
}
public bool Update(Product product)
{
return _productsRepository.Update(product);
}
}
110 changes: 110 additions & 0 deletions src/lib/Data/DatabaseAccessMethodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using models;
using System;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;

namespace lib.Data
{
public class DatabaseAccessMethodes(IDatabaseInitMethodes _databaseInitMethodes) : IDatabaseAccessMethodes
{
public async Task<Response<TEntity>> CallDatabaseResponseAsync<TEntity>(DatabaseAccessModel accessModel)
{
Response<TEntity> result = new();

try
{
var sqliteConnectionBuilder = new SqliteConnectionStringBuilder()
{
DataSource = _databaseInitMethodes.DatabasePath
};
using (IDbConnection connection = new SqliteConnection(sqliteConnectionBuilder.ToString()))
{
//connection.Open();

if (accessModel.ResultType == ResultType.Single)
{
result.Result = (await connection.QueryFirstOrDefaultAsync<TEntity>(accessModel.CommandText, accessModel.Parameters));
result.IsSuccess = true;
}
else if (accessModel.ResultType == ResultType.Multiple)
{
result.Results = (await connection.QueryAsync<TEntity>(accessModel.CommandText, accessModel.Parameters));
result.IsSuccess = true;
}
else if (accessModel.ResultType == ResultType.NoResult)
{
await connection.ExecuteAsync(accessModel.CommandText, accessModel.Parameters);
result.IsSuccess = true;
}
}
return result;
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
return result;

}
}

public async Task<Response<TEntity>> CallDatabasetransactionAsync<TEntity>(DatabaseAccessModel accessModel, DatabaseAccessModel accessModel1)
{
Response<TEntity> result = new();

try
{
var sqliteConnectionBuilder = new SqliteConnectionStringBuilder()
{
DataSource = _databaseInitMethodes.DatabasePath
};
using (IDbConnection connection = new SqliteConnection(sqliteConnectionBuilder.ToString()))
{
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
SQLitePCL.Batteries.Init();
connection.Open();

using (IDbTransaction transaction = connection.BeginTransaction())
{
try
{
var orderId = await connection.QueryFirstOrDefaultAsync<TEntity>(accessModel.CommandText, accessModel.Parameters, transaction);

List<ProductsOrder> productsOrders = new List<ProductsOrder>();
var param = (Product[])accessModel1.Parameters;
foreach (var item in param)
{
connection.Execute(accessModel1.CommandText, new { OrderId = orderId, ProductId = item.Id }, transaction);
}

transaction.Commit();
result.IsSuccess = true;
result.Result = orderId;
}
catch (Exception ex)
{
transaction.Rollback();
Console.WriteLine(ex.Message);

return result;
}
}

}
return result;
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
return result;

}
}
}
}
86 changes: 86 additions & 0 deletions src/lib/Data/DatabaseInitMethodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.IO;
using SQLitePCL;


namespace lib.Data;

public class DatabaseInitMethodes : IDatabaseInitMethodes
{

public string DatabasePath { get => $"{Environment.CurrentDirectory}\\Infinite-database.db"; }

public async void CreateDatabase()
{

SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
SQLitePCL.Batteries.Init();

using (IDbConnection connection = new SqliteConnection($"Data Source={DatabasePath}"))
{
connection.Open();
try
{
var CommandText = @"CREATE TABLE IF NOT EXISTS Products (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Price DECIMAL NOT NULL
)";
await connection.ExecuteAsync(CommandText, CommandType.Text);

var CommandText1 = @"CREATE TABLE IF NOT EXISTS Orders (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
TotalPrice DECIMAL NOT NULL
)";

await connection.ExecuteAsync(CommandText1, CommandType.Text);

var CommandText2 = @"CREATE TABLE IF NOT EXISTS ProductsOrder (
OrderId INTEGER,
ProductId INTEGER,
FOREIGN KEY (OrderId) REFERENCES Orders(Id),
FOREIGN KEY (ProductId) REFERENCES Products(Id)
)";
await connection.ExecuteAsync(CommandText2, CommandType.Text);

var CommandText3 = @"INSERT INTO Products (Name, Price) VALUES
('Product1', 1999),
('Product2', 2999),
('Product3', 1499);

INSERT INTO Orders (TotalPrice) VALUES
(5597),
(4298),
(7150);

INSERT INTO ProductsOrder (OrderId, ProductId) VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 3); ";
await connection.ExecuteAsync(CommandText3, CommandType.Text);
}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}

}
}

public void InitializeDatabase()
{
if (!File.Exists(DatabasePath))
{
CreateDatabase();
}
}
}
16 changes: 16 additions & 0 deletions src/lib/Data/IDatabaseAccessMethodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lib.Data
{
public interface IDatabaseAccessMethodes
{
Task<Response<TEntity>> CallDatabaseResponseAsync<TEntity>(DatabaseAccessModel accessModel);
Task<Response<TEntity>> CallDatabasetransactionAsync<TEntity>(DatabaseAccessModel accessModel, DatabaseAccessModel accessModel1);

}
}
15 changes: 15 additions & 0 deletions src/lib/Data/IDatabaseInitMethodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lib.Data
{
public interface IDatabaseInitMethodes
{
void InitializeDatabase();
string DatabasePath { get; }
void CreateDatabase();
}
}
4 changes: 4 additions & 0 deletions src/lib/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using app.Repositories;
using lib.Data;
using lib.Repositories;
using lib.Repositories.Concrete;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -11,6 +12,9 @@ public static IServiceCollection RegisterLib(this IServiceCollection services)
{
services.AddScoped<IOrderRepository, OrderRepository>();
services.AddScoped<IProductsRepository, ProductsRepository>();
services.AddScoped<IDatabaseInitMethodes, DatabaseInitMethodes>();
services.AddScoped<IDatabaseAccessMethodes, DatabaseAccessMethodes>();

return services;
}
}
Loading