|
1 | 1 | using MakeSimple.SharedKernel.Contract;
|
2 | 2 | using Microsoft.EntityFrameworkCore;
|
| 3 | +using Microsoft.EntityFrameworkCore.ChangeTracking; |
3 | 4 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
4 | 5 | using Sieve.Attributes;
|
5 | 6 | using System;
|
|
10 | 11 |
|
11 | 12 | namespace MakeSimple.SharedKernel.Infrastructure.Test.Mocks
|
12 | 13 | {
|
13 |
| - public class Address : AuditEntity |
| 14 | + public class Address : AuditableEntity |
14 | 15 | {
|
15 | 16 | [Sieve(CanFilter = true, CanSort = true)]
|
16 | 17 | [Key]
|
@@ -49,6 +50,28 @@ public class MyDBContext : DbContext, IUnitOfWork
|
49 | 50 |
|
50 | 51 | public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default)
|
51 | 52 | {
|
| 53 | + foreach (EntityEntry<Entity> entry in ChangeTracker.Entries<Entity>()) |
| 54 | + { |
| 55 | + switch (entry.State) |
| 56 | + { |
| 57 | + case EntityState.Added: |
| 58 | + if (entry.Entity is AuditableEntity addAuditable) |
| 59 | + { |
| 60 | + addAuditable.CreatedBy = "test"; |
| 61 | + } |
| 62 | + entry.Entity.CreatedAt = DateTime.Now; |
| 63 | + break; |
| 64 | + |
| 65 | + case EntityState.Modified: |
| 66 | + if (entry.Entity is AuditableEntity modifiAuditable) |
| 67 | + { |
| 68 | + modifiAuditable.LastModifiedBy = "test"; |
| 69 | + } |
| 70 | + entry.Entity.LastModifiedAt = DateTime.Now; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
52 | 75 | return (await SaveChangesAsync(cancellationToken)) > 0;
|
53 | 76 | }
|
54 | 77 |
|
@@ -79,7 +102,7 @@ public class Student : Entity
|
79 | 102 | public string Name { get; set; }
|
80 | 103 | }
|
81 | 104 |
|
82 |
| - public class User : AuditEntity |
| 105 | + public class User : AuditableEntity |
83 | 106 | {
|
84 | 107 | [Key]
|
85 | 108 | public Guid Id { get; set; }
|
@@ -118,7 +141,7 @@ public class StudentDto
|
118 | 141 | public string Name { get; set; }
|
119 | 142 | }
|
120 | 143 |
|
121 |
| - public class UserDto : AuditEntity |
| 144 | + public class UserDto : AuditableEntity |
122 | 145 | {
|
123 | 146 | public Guid Id { get; set; }
|
124 | 147 | public ICollection<Address> Address { get; set; }
|
|
0 commit comments