I am creating a project with react and .Net Core and I want to add feature that tracking user behavior.
I just want to know who added the product? Who updated it? Who deleted it?
There is two option in my mind which one is better, I don't know
1- Create tracking mechanism in. Net like a trigger
public class ActivityType
{
public int ActivityTypeId { get; set; }
public string ActivityTypeName { get; set; }//Added,Updated
}
public class Activity
{
public int ActivityId { get; set; }
public DateTime ActivityTime { get; set; }
public int ActivityTypeId { get; set; }
public ActivityType ActivityType { get; set; }
}
public class ProductActivity
{
public int ProductActivityId { get; set; }
public virtual Product Product{ get; set; }
public int ProductId { get; set; }
public int ActivityId { get; set; }
public virtual Activity Activity { get; set; }
public virtual User User { get; set; }
public int UserId { get; set; }
}
Inside ProductController > addProduct
[HttpPost("add")]
public async Task<ActionResult<Product>> Add(Product product)
{
var result = _productService.Add(product);
if (result.Success)
{
//example code
//var newActivity=_context.ProductActivity
//{
//productId:result.Data.Id
//ActivityId:1 //added
//User:1
//})
//_context.ProductActivity.Add(newActivity);
//_context.SaveChanges();
return Ok(result);
}
}
2-Create tracking mechanism in React with action functions(redux)
If I choose that way. I have to create ProductActivity Controller and Endpoint after then I will hit this endpoint in react.
const createProduct = async () => {
const tokenResponce = await productService.createProduct({
productName:"x" ,
});
if(tokenResponce)
{
dispatch(addedProduct());
}
else
{
console.log("error");
}
};
CodePudding user response:
I suspect you would like to persist the tracking somewhere, and in that case it is better to be done on the server (option 1). By my understanding, you wouldn't need to do anything in the frontend since the backend knows which user calls each method (I assume the user is logged in). So you would take the identity of the currently logged in user in each method for create (add),read (get), update, delete (CRUD) of Product