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;
|
sale.Total = sale.SubTotal + sale.Tax;
|
||||||
|
|
||||||
//save
|
//save
|
||||||
SqlDataAccess sql = new SqlDataAccess();
|
using (SqlDataAccess sql = new SqlDataAccess())
|
||||||
sql.SaveData("dbo.spSaleInsert", sale, "RMData");
|
{
|
||||||
|
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
|
// finish filling up sale details
|
||||||
foreach (var item in details)
|
foreach (var item in details)
|
||||||
{
|
{
|
||||||
item.SaleId = sale.Id;
|
item.SaleId = sale.Id;
|
||||||
|
|
||||||
// Save Details
|
sql.SaveDataInTransaction("dbo.spSaleDetailInsert", item);
|
||||||
|
}
|
||||||
sql.SaveData("dbo.spSaleDetailInsert", item, "RMData");
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
sql.RollBackTransaction();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace RMDataManagerLibrary.Internal.DataAccess
|
namespace RMDataManagerLibrary.Internal.DataAccess
|
||||||
{
|
{
|
||||||
public class SqlDataAccess
|
internal class SqlDataAccess : IDisposable
|
||||||
{
|
{
|
||||||
public string GetConnectionString(string name)
|
public string GetConnectionString(string name)
|
||||||
{
|
{
|
||||||
@ -40,5 +40,51 @@ namespace RMDataManagerLibrary.Internal.DataAccess
|
|||||||
commandType: CommandType.StoredProcedure);
|
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