I am using UnitOfWork pattern in my application. I implemented Remove method in Page like bellow:
public IActionResult OnPostRemove(int id)
{
var report = db.tblDailyReportPiping.Find(id);
if (report == null)
return new JsonResult(new { status = false, message = "Invalid Data" });
//Only user whci created data can remove data
if (report.fldUserId != HttpContext.Session.GetActiveUser_UserId())
return new JsonResult(new { status = false, message = "Incorrect user" });
//current row shal no be approved or finalized
if (report.fldApproved || report.fldFinalized)
return new JsonResult(new { status = false, message = "Approved or finalized before" });
try
{
//remove
if (!db.tblDailyReportPiping.Remove(report))
return new JsonResult(new { status = false, message = "unable to remove" });
//save
if (db.SaveChangesByUserId(HttpContext.Session.GetActiveUser_UserId()) == 0)
return new JsonResult(new { status = false, message = "unable to save" });
}
catch (Exception ex)
{
return new JsonResult(new { status = false, message = ex.Message });
}
return new JsonResult(true);
}
Before using Remove method from UOW, I inspected situation like userId and other situation. My question is: In which level I must inspect situation before CRUD operation? Both is possible, less code in action control or in repository but, which one is best practice?
I may use Remove from repository from different part of my application and by moving all code to repository I can lessen code line throughout in my application and just use Remove method from repository but, I must pass some extra parameter to method like userId, is it correct approach?
Thank you all