-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProductInfoService.cs
73 lines (63 loc) · 2.56 KB
/
ProductInfoService.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Microsoft.Extensions.Options;
using ProductScraper.Common.Naming;
using ProductScraper.Models.EntityModels;
using ProductScraper.Models.ViewModels;
using ProductScraper.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace ProductScraper.Services.Implementations
{
public class ProductInfoService : IProductInfoService
{
private readonly IAzureTableStorage<ProductInfo> _repository;
private readonly IOptions<AppSettings> _settings;
public ProductInfoService(IAzureTableStorage<ProductInfo> repository,
IOptions<AppSettings> settings)
{
_repository = repository;
_settings = settings;
}
public async Task AddAsync(string userId, ProductInfo productInfo)
{
productInfo.Id = DateTime.Now.Ticks;
productInfo.RowKey = productInfo.Id.ToString();
productInfo.PartitionKey = userId;
await _repository.Insert(productInfo);
}
public async Task CheckAsync(string userId, long id)
{
CancellationToken cancellationToken;
string url = _settings.Value.AzureFunctionURL + FunctionName.ScrapeProduct + $"/{userId}/{id}/?code=" + _settings.Value.AzureFunctionCode;
using HttpClient client = new HttpClient();
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
using HttpResponseMessage response = await client
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception(await response.Content.ReadAsStringAsync());
}
}
public async Task DeleteAsync(string userId, long id)
{
await _repository.Delete(userId, id.ToString());
}
public async Task<IList<ProductInfo>> GetAllAsync(string userId)
{
return await _repository.GetList(userId);
}
public async Task<ProductInfo> GetDetailsAsync(string userId, long id)
{
return await _repository.GetItem(userId, id.ToString());
}
public async Task UpdateAsync(string userId, ProductInfo productInfo)
{
productInfo.RowKey = productInfo.Id.ToString();
productInfo.PartitionKey = userId;
await _repository.Update(productInfo);
}
}
}