-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProductService.txt
More file actions
45 lines (38 loc) · 1.44 KB
/
Copy pathProductService.txt
File metadata and controls
45 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
private readonly MarketContext _context;
private readonly IMapper _mapper;
public ProductService(MarketContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<List<ResultProductDto>> GetAllProductAsync()
{
var values = await _context.Products.ToListAsync();
return _mapper.Map<List<ResultProductDto>>(values);
}
public async Task<GetByIdProductDto> GetByIdProductAsync(int id)
{
var value = await _context.Products.FindAsync(id);
return _mapper.Map<GetByIdProductDto>(value);
}
public async Task CreateProductAsync(CreateProductDto createProductDto)
{
var value = _mapper.Map<Product>(createProductDto);
_context.Products.Add(value);
await _context.SaveChangesAsync();
}
public async Task UpdateProductAsync(UpdateProductDto updateProductDto)
{
var value = _mapper.Map<Product>(updateProductDto);
_context.Products.Update(value);
await _context.SaveChangesAsync();
}
public async Task DeleteProductAsync(int id)
{
var value = await _context.Products.FindAsync(id);
if (value != null)
{
_context.Products.Remove(value);
await _context.SaveChangesAsync();
}
}