| | 1 | | using System.Reflection; |
| | 2 | | using Infrastructure.Entity; |
| | 3 | | using Infrastructure.Repository; |
| | 4 | | using Microsoft.EntityFrameworkCore; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace Application.Service; |
| | 8 | | public class ProductService { |
| | 9 | |
|
| | 10 | | private readonly ILogger<ProductService> _logger; |
| | 11 | | private readonly NorthwindRepository _repository; |
| | 12 | |
|
| 0 | 13 | | public ProductService(ILogger<ProductService> logger, NorthwindRepository repository) { |
| 0 | 14 | | _logger = logger; |
| 0 | 15 | | _repository = repository; |
| 0 | 16 | | } |
| | 17 | |
|
| 0 | 18 | | public async Task<product> Create(product product, CancellationToken ct) { |
| 0 | 19 | | await _repository.Add(product, ct); |
| 0 | 20 | | await _repository.SaveChangesAsync(ct); |
| 0 | 21 | | _logger.LogInformation("Product created: {ProductId}", product.product_id); |
| 0 | 22 | | return product; |
| 0 | 23 | | } |
| | 24 | |
|
| 0 | 25 | | public async Task<product?> Update(int id, product product, CancellationToken ct) { |
| 0 | 26 | | var existing = await _repository.GetById<product>(id, ct); |
| 0 | 27 | | if (existing == null) { |
| 0 | 28 | | _logger.LogWarning("Product not found for update: {ProductId}", id); |
| 0 | 29 | | return null; |
| | 30 | | } |
| | 31 | |
|
| 0 | 32 | | existing.product_name = product.product_name; |
| 0 | 33 | | existing.unit_price = product.unit_price; |
| 0 | 34 | | existing.units_in_stock = product.units_in_stock; |
| 0 | 35 | | existing.category_id = product.category_id; |
| | 36 | |
|
| | 37 | | //TODO other properties and mapping |
| | 38 | |
|
| 0 | 39 | | await _repository.Update(existing, ct); |
| 0 | 40 | | await _repository.SaveChangesAsync(ct); |
| 0 | 41 | | return existing; |
| 0 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | public async Task<bool> DeleteAsync(int id, CancellationToken ct) { |
| 0 | 45 | | var product = await _repository.GetById<product>(id, ct); |
| 0 | 46 | | if (product == null) { |
| 0 | 47 | | _logger.LogWarning("Product not found for delete: {ProductId}", id); |
| 0 | 48 | | return false; |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | await _repository.Delete(product, ct); |
| 0 | 52 | | await _repository.SaveChangesAsync(ct); |
| 0 | 53 | | _logger.LogInformation("Product deleted: {ProductId}", id); |
| 0 | 54 | | return true; |
| 0 | 55 | | } |
| | 56 | | } |