Created a transaction
compressed sale into one transaction, if something fails all changes in sale and saledetail are rollbacked.
This commit is contained in:
parent
7f6c33e87c
commit
57b456b1e1
@ -53,24 +53,31 @@ namespace RMDataManagerLibrary.DataAcccess
|
||||
sale.Total = sale.SubTotal + sale.Tax;
|
||||
|
||||
//save
|
||||
SqlDataAccess sql = new SqlDataAccess();
|
||||
sql.SaveData("dbo.spSaleInsert", sale, "RMData");
|
||||
using (SqlDataAccess sql = new SqlDataAccess())
|
||||
{
|
||||
try
|
||||
{
|
||||
sql.StartTransaction("RMData");
|
||||
|
||||
// get id from saleModel
|
||||
sql.SaveDataInTransaction("spSaleInsert", sale);
|
||||
|
||||
sale.Id = sql.LoadData<int, dynamic>("spSaleLookUp",new { sale.CashierId, sale.SaleDate }, "RMData").FirstOrDefault();
|
||||
sale.Id = sql.LoadDataInTransaction<int, dynamic>("spSaleLookUp", new { sale.CashierId, sale.SaleDate }).FirstOrDefault();
|
||||
|
||||
// finish filling up sale details
|
||||
foreach (var item in details)
|
||||
{
|
||||
item.SaleId = sale.Id;
|
||||
|
||||
// Save Details
|
||||
|
||||
sql.SaveData("dbo.spSaleDetailInsert", item, "RMData");
|
||||
sql.SaveDataInTransaction("dbo.spSaleDetailInsert", item);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
sql.RollBackTransaction();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace RMDataManagerLibrary.Internal.DataAccess
|
||||
{
|
||||
public class SqlDataAccess
|
||||
internal class SqlDataAccess : IDisposable
|
||||
{
|
||||
public string GetConnectionString(string name)
|
||||
{
|
||||
@ -40,5 +40,51 @@ namespace RMDataManagerLibrary.Internal.DataAccess
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
private IDbConnection _connection;
|
||||
private IDbTransaction _transaction;
|
||||
public void StartTransaction(string connectionStringName)
|
||||
{
|
||||
string connectionString = GetConnectionString(connectionStringName);
|
||||
|
||||
_connection = new SqlConnection(connectionString);
|
||||
_connection.Open();
|
||||
|
||||
_transaction = _connection.BeginTransaction();
|
||||
}
|
||||
|
||||
public void SaveDataInTransaction<T>(string storedProcedure, T parameters)
|
||||
{
|
||||
_connection.Execute(storedProcedure, parameters,
|
||||
commandType: CommandType.StoredProcedure, transaction: _transaction);
|
||||
|
||||
}
|
||||
public List<T> LoadDataInTransaction<T, U>(string storedProcedure, U parameters)
|
||||
{
|
||||
|
||||
|
||||
List<T> rows = _connection.Query<T>(storedProcedure, parameters,
|
||||
commandType: CommandType.StoredProcedure, transaction: _transaction).ToList();
|
||||
|
||||
return rows;
|
||||
|
||||
}
|
||||
|
||||
public void CommitTransaction()
|
||||
{
|
||||
_transaction?.Commit();
|
||||
_connection?.Close();
|
||||
}
|
||||
|
||||
public void RollBackTransaction()
|
||||
{
|
||||
_transaction?.Rollback();
|
||||
_connection?.Close();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
CommitTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user