1- using Application . Domain ;
2- using Application . Ports ;
31using Infrastructure . Entities ;
42using Microsoft . EntityFrameworkCore ;
5- using Vocabulary ;
63
74namespace Infrastructure . Db ;
85
96public class ProductDbContext ( DbContextOptions < ProductDbContext > options ) : DbContext ( options )
107{
118 public DbSet < ProductEntity > Products => Set < ProductEntity > ( ) ;
12- }
13-
14- public class ProductRepository ( ProductDbContext db ) : IProductPort
15- {
16- public async Task Save ( ProductAggregate product )
17- {
18- var entity = ToEntity ( product ) ;
19- await db . Products . AddAsync ( entity ) ;
20- await db . SaveChangesAsync ( ) ;
21- }
22-
23- public async Task Update ( ProductAggregate product )
24- {
25- var entity = ToEntity ( product ) ;
26- db . Products . Update ( entity ) ;
27- await db . SaveChangesAsync ( ) ;
28- }
29-
30- public async Task < ProductAggregate ? > FindById ( ProductId id , CancellationToken cancellationToken )
31- {
32- var entity = await db . Products . FindAsync ( [ id . Value ] , cancellationToken ) ;
33- return entity is null ? null : ToAggregate ( entity ) ;
34- }
35-
36- public async Task < IEnumerable < ProductAggregate > > FindAll ( CancellationToken cancellationToken )
37- {
38- var products = await db . Products . ToListAsync ( cancellationToken : cancellationToken ) ;
39- return products . Select ( ToAggregate ) ;
40- }
41-
42- private static ProductAggregate ToAggregate ( ProductEntity e )
43- {
44- return new ( new ProductId ( e . Id ) , new Name ( e . Name ) , new Category ( e . Category ) , new Price ( e . Price ) , new Stock ( e . Stock ) ) ;
45- }
46-
47- private static ProductEntity ToEntity ( ProductAggregate a ) => new ( )
48- {
49- Id = a . Id . Value ,
50- Name = a . Name . Value ,
51- Category = a . Category . Value ,
52- Price = a . Price . Value ,
53- Stock = a . Stock . Value
54- } ;
559}
0 commit comments