using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using iiie.Logs.DataAccess;
using iiie.Logs.DBO;
namespace iiie.WebApiUtils.BusinessManager
{
///
/// Helper for SQL Server data access
///
public abstract class SqlServerManager
: SqlServerBasicManager
where TDbObject : class
where TEntities : DbContext, new()
where TThis : SqlServerManager, new()
{
///
/// Convert a DB object to DBO
///
/// The object to be converted
/// The DBO
public abstract TDboGet DbToDboGet(TDbObject obj);
///
/// Convert a DBO to DB object
///
/// The object to be converted
/// The DB object
public abstract TDbObject DboAddToDb(TDboAdd obj);
///
/// Convert a DBO to DB object
///
/// The object to be converted
/// The object beeing edited
/// The DB object
public abstract TDbObject DboEditToDb(TDboEdit obj, TDbObject edit);
///
/// Helper to convert DB object to DBO
///
/// The result to convert
/// The DBO object
public static TDboGet DbToDboGetStatic(TDbObject res)
{
var obj = new TThis();
return obj.DbToDboGet(res);
}
///
/// Helper to convert DBO object to DB
///
/// The result to convert
/// The DB object
public static TDbObject DboAddToDbStatic(TDboAdd res)
{
var obj = new TThis();
return obj.DboAddToDb(res);
}
///
/// Helper to convert DBO object to DB
///
/// The result to convert
/// The object beeing edited
/// The DB object
public static TDbObject DboEditToDbStatic(TDboEdit res, TDbObject edit)
{
var obj = new TThis();
return obj.DboEditToDb(res, edit);
}
///
/// Get a single DB object matching the predicate
///
/// The table to search in
/// The predicate
/// The object that match
public static OpResult GetSingleDbObject(DbSet table, Expression> predicate)
{
var o = table.FirstOrDefault(predicate);
if (o == null)
{
return OpResult.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
}
return OpResult.Ok(o);
}
///
/// Get a single DBO matching the predicate
///
/// The predicate
/// The object that match, or null
public static OpResult GetSingle(Expression> predicate)
{
return Execute((db, table) =>
{
var obj = GetSingleDbObject(table, predicate);
if (!obj)
return obj.To();
return OpResult.Ok(DbToDboGetStatic(obj.Data));
});
}
///
/// Get an object by its primary key(s)
///
/// The key names and values
/// The object
public static OpResult GetSingleByKeys(params KeyValuePair[] keys)
{
return GetSingle(GetExpression(keys));
}
///
/// Get an object by its id
///
/// The id of the object
/// The object
public static OpResult GetSingleById(long id)
{
return GetSingleByKeys(new KeyValuePair("id", id));
}
///
/// Get all DB object matching the predicate
///
/// The predicate
/// The order by function
/// The page numeber (0 based)
/// The maximum number of items par page
/// All matching objects
public static OpResult> GetMultiple(Expression> predicate,
Expression> orderBy, int page = 0,int perPage = Int32.MaxValue)
{
return Execute((db, table) =>
{
var results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).ToList();
return OpResult>.Ok(results.Select(DbToDboGetStatic));
});
}
///
/// Get all DB object matching the predicate built from keys
///
/// The page numeber (0 based)
/// The maximum number of items par page
/// The order by function
/// The key names and values
/// All matching objects
public static OpResult> GetMultipleByKeys(Expression> orderBy,
int page = 0, int perPage = Int32.MaxValue, params KeyValuePair[] keys)
{
return GetMultiple(GetExpression(keys), orderBy, page, perPage);
}
///
/// Add an entry in the database
///
/// The object to be added
/// Always true or an OpResult error
public static OpResult Add(TDboAdd obj)
{
return Execute((db, table) =>
{
table.Add(DboAddToDbStatic(obj));
db.SaveChanges();
return OpResult.Ok(true);
});
}
///
/// Edit an entry in the database
///
/// The object to be added
/// The predicate to be used to select data
/// Always true or an OpResult error
public static OpResult Edit(TDboEdit obj, Expression> predicate)
{
return Execute((db, table) =>
{
var edit = GetSingleDbObject(table, predicate);
if (!edit)
return edit.To();
var res = DboEditToDbStatic(obj, edit.Data);
db.Entry(edit.Data).CurrentValues.SetValues(res);
db.SaveChanges();
return OpResult.Ok(true);
});
}
///
/// Edit an entry in the database
///
/// The object to be added
/// The keys to be used to select data
/// Always true or an OpResult error
public static OpResult Edit(TDboEdit obj, params KeyValuePair[] keys)
{
return Edit(obj, GetExpression(keys));
}
///
/// Edit an entry in the database
///
/// The object to be added
/// The id to be edited
/// Always true or an OpResult error
public static OpResult EditById(TDboEdit obj, long id)
{
return Edit(obj, new KeyValuePair("id", id));
}
///
/// Delete an entry in the database
///
/// The predicate to be used to delete data
/// Always true or an OpResult error
public static OpResult Delete(Expression> predicate)
{
return Execute((db, table) =>
{
var del = GetSingleDbObject(table, predicate);
if (!del)
return del.To();
table.Remove(del.Data);
db.SaveChanges();
return OpResult.Ok(true);
});
}
///
/// Delete an entry in the database
///
/// The keys to be used to delete data
/// Always true or an OpResult error
public static OpResult Delete(params KeyValuePair[] keys)
{
return Delete(GetExpression(keys));
}
///
/// Delete an entry in the database
///
/// The id to be deleted
/// Always true or an OpResult error
public static OpResult DeleteById(long id)
{
return Delete(new KeyValuePair("id", id));
}
}
}