-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductService.cs
More file actions
46 lines (40 loc) · 1.65 KB
/
ProductService.cs
File metadata and controls
46 lines (40 loc) · 1.65 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
46
using ECommerceAPI.Application.Abstractions.Services;
using ECommerceAPI.Application.Repositories;
using System.Text.Json;
namespace ECommerceAPI.Persistence.Services
{
public class ProductService : IProductService
{
readonly IProductReadRepository _productReadRepository;
readonly IProductWriteRepository _productWriteRepository;
readonly IQRCodeService _qrCodeService;
public ProductService(IProductReadRepository productReadRepository, IQRCodeService qrCodeService, IProductWriteRepository productWriteRepository)
{
_productReadRepository = productReadRepository;
_qrCodeService = qrCodeService;
_productWriteRepository = productWriteRepository;
}
public async Task<byte[]> ProductQrCodeAsync(string productId)
{
var product = await _productReadRepository.GetByIdAsync(productId);
if (product == null)
throw new Exception("Product not found");
var plainObject = new
{
product.Id,
product.Name,
product.Price,
product.Stock,
product.CreatedDate
};
string plainText = JsonSerializer.Serialize(plainObject);
return _qrCodeService.GenerateQRCode(plainText);
}
public async Task UpdateQrCodeProductStock(string productId, int stock)
{
var product = await _productReadRepository.GetByIdAsync(productId) ?? throw new Exception("Product not found");
product.Stock = stock;
await _productWriteRepository.SaveAsync();
}
}
}