-
-
Notifications
You must be signed in to change notification settings - Fork 158
Question: which layer to implement audit fields #637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @fdlane, this indeed is a bit less easy than one would expect. Internally the repository accesses a list of all targeted attributes to know which ones to update:
That means AttrAttribute instances associated to your EditedBy and EditedDate field must be in that list. Indeed, this is taken care of under the hood when an attribute is targeted through a PATCH request. In your case you would have to add this attribute to the list manually.
In public BaseService(
... ,
ITargetedFields targetedFields,
IResourceGraphExplorer graph,
...) : base( ... ) { ... }
public override async Task<T> UpdateAsync(Guid id, T entity)
{
entity.EditedBy = _currentUser.Username;
entity.EditedDate = DateTime.Now;
var attributes = _graph.GetAttributes<YourResource>( r => new { r.EditedBy, r.EditedDate });
_targetedFields.AddRange(attributes);
var updatedEntity = await base.UpdateAsync(id, entity);
return updatedEntity;
} I see you're on
To get a hold of the attributes see my post here #536 (comment). That is still a bit complicated in alpha3. I would recommend you to switch to alpha4 ASAP anyway. Let me know if that helps! |
This was a great help and we will be moving to alpha4 soon. Thank you! My base class based on #536
|
Thanks for the help. |
Description
Currently I am implementing audit fields in the service layer. However, the values are only persisted to the db if the api user includes the properties in the request.
entity.EditedBy = _currentUser.Username;
is not applied if the user does not include the property in thePatch
request.How do I get the audit fields to always be persisted?
Environment
The text was updated successfully, but these errors were encountered: