| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data |
| | | 6 | | { |
| | | 7 | | public class ProductRepository : MainRepository, IRepository<Product> |
| | | 8 | | { |
| | 0 | 9 | | public ProductRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 10 | | { |
| | 0 | 11 | | } |
| | | 12 | | |
| | | 13 | | public async Task<int> Add(Product value) |
| | | 14 | | { |
| | 0 | 15 | | await _context.Products.AddAsync(value); |
| | 0 | 16 | | return await _context.SaveChangesAsync(); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public async Task<int> Delete(Product value) |
| | | 20 | | { |
| | 0 | 21 | | _context.Products.Remove(value); |
| | 0 | 22 | | return await _context.SaveChangesAsync(); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<List<Product>> Get() |
| | | 26 | | { |
| | 0 | 27 | | await Task.CompletedTask; |
| | 0 | 28 | | return _context.Products.ToList(); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<Product?> GetAsync(int id) |
| | | 32 | | { |
| | 0 | 33 | | return await _context.Products.FirstOrDefaultAsync(p => p.Id == id); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<Product?> GetAsync(Product value) |
| | | 37 | | { |
| | 0 | 38 | | return await _context.Products.FirstOrDefaultAsync(p => p.Article == value.Article); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public async Task<int> Update(Product value) |
| | | 42 | | { |
| | 0 | 43 | | _context.Products.Update(value); |
| | 0 | 44 | | return await _context.SaveChangesAsync(); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary>Словарь article → ImageUrl для списка артикулов. Только записи с непустым ImageUrl.</summary> |
| | | 48 | | public async Task<Dictionary<string, string>> GetImageUrlsByArticlesAsync(IEnumerable<string> articles) |
| | | 49 | | { |
| | 0 | 50 | | var articleList = articles.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList(); |
| | 0 | 51 | | if (articleList.Count == 0) |
| | 0 | 52 | | return new Dictionary<string, string>(); |
| | | 53 | | |
| | 0 | 54 | | return await _context.Products |
| | 0 | 55 | | .Where(p => p.Article != null && articleList.Contains(p.Article) && p.ImageUrl != null) |
| | 0 | 56 | | .Select(p => new { p.Article, p.ImageUrl }) |
| | 0 | 57 | | .ToDictionaryAsync( |
| | 0 | 58 | | p => p.Article!, |
| | 0 | 59 | | p => p.ImageUrl!); |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | } |