2
2
using Entities . Models ;
3
3
using Entities . RequestFeatures ;
4
4
using Entities . RequestParameters ;
5
+ using Microsoft . AspNetCore . Components ;
5
6
using Microsoft . AspNetCore . WebUtilities ;
6
7
using System ;
7
8
using System . Collections . Generic ;
@@ -17,10 +18,12 @@ namespace BlazorProducts.Client.HttpRepository
17
18
public class ProductHttpRepository : IProductHttpRepository
18
19
{
19
20
private readonly HttpClient _client ;
21
+ private readonly JsonSerializerOptions _options ;
20
22
21
23
public ProductHttpRepository ( HttpClient client )
22
24
{
23
25
_client = client ;
26
+ _options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ;
24
27
}
25
28
26
29
public async Task < PagingResponse < Product > > GetProducts ( ProductParameters productParameters )
@@ -31,42 +34,39 @@ public async Task<PagingResponse<Product>> GetProducts(ProductParameters product
31
34
[ "searchTerm" ] = productParameters . SearchTerm == null ? "" : productParameters . SearchTerm ,
32
35
[ "orderBy" ] = productParameters . OrderBy
33
36
} ;
34
-
35
- var response = await _client . GetAsync ( QueryHelpers . AddQueryString ( "https://localhost:5011/api/products" , queryStringParam ) ) ;
37
+ var response = await _client . GetAsync ( QueryHelpers . AddQueryString ( "products" , queryStringParam ) ) ;
36
38
var content = await response . Content . ReadAsStringAsync ( ) ;
37
39
if ( ! response . IsSuccessStatusCode )
38
40
{
39
41
throw new ApplicationException ( content ) ;
40
42
}
41
-
42
43
var pagingResponse = new PagingResponse < Product >
43
44
{
44
- Items = JsonSerializer . Deserialize < List < Product > > ( content , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ,
45
- MetaData = JsonSerializer . Deserialize < MetaData > ( response . Headers . GetValues ( "X-Pagination" ) . First ( ) , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } )
45
+ Items = JsonSerializer . Deserialize < List < Product > > ( content , _options ) ,
46
+ MetaData = JsonSerializer . Deserialize < MetaData > ( response . Headers . GetValues ( "X-Pagination" ) . First ( ) , _options )
46
47
} ;
47
-
48
+
48
49
return pagingResponse ;
49
50
}
50
51
51
- public async Task CreateProduct ( Product product )
52
- {
53
- var content = JsonSerializer . Serialize ( product ) ;
54
- var bodyContent = new StringContent ( content , Encoding . UTF8 , "application/json" ) ;
52
+ public async Task CreateProduct ( Product product )
53
+ {
54
+ var content = JsonSerializer . Serialize ( product ) ;
55
+ var bodyContent = new StringContent ( content , Encoding . UTF8 , "application/json" ) ;
55
56
56
- var postResult = await _client . PostAsync ( "https://localhost:5011/api/ products" , bodyContent ) ;
57
- var postContent = await postResult . Content . ReadAsStringAsync ( ) ;
57
+ var postResult = await _client . PostAsync ( "products" , bodyContent ) ;
58
+ var postContent = await postResult . Content . ReadAsStringAsync ( ) ;
58
59
59
- if ( ! postResult . IsSuccessStatusCode )
60
- {
61
- throw new ApplicationException ( postContent ) ;
62
- }
60
+ if ( ! postResult . IsSuccessStatusCode )
61
+ {
62
+ throw new ApplicationException ( postContent ) ;
63
+ }
63
64
}
64
65
65
66
public async Task < string > UploadProductImage ( MultipartFormDataContent content )
66
67
{
67
- var postResult = await _client . PostAsync ( "https://localhost:5011/api/ upload" , content ) ;
68
+ var postResult = await _client . PostAsync ( "upload" , content ) ;
68
69
var postContent = await postResult . Content . ReadAsStringAsync ( ) ;
69
-
70
70
if ( ! postResult . IsSuccessStatusCode )
71
71
{
72
72
throw new ApplicationException ( postContent ) ;
@@ -80,7 +80,7 @@ public async Task<string> UploadProductImage(MultipartFormDataContent content)
80
80
81
81
public async Task < Product > GetProduct ( string id )
82
82
{
83
- var url = Path . Combine ( "https://localhost:5011/api/ products/ " , id ) ;
83
+ var url = Path . Combine ( "products" , id ) ;
84
84
85
85
var response = await _client . GetAsync ( url ) ;
86
86
var content = await response . Content . ReadAsStringAsync ( ) ;
@@ -89,29 +89,29 @@ public async Task<Product> GetProduct(string id)
89
89
throw new ApplicationException ( content ) ;
90
90
}
91
91
92
- var product = JsonSerializer . Deserialize < Product > ( content , new JsonSerializerOptions { PropertyNameCaseInsensitive = true } ) ;
92
+ var product = JsonSerializer . Deserialize < Product > ( content , _options ) ;
93
93
return product ;
94
94
}
95
95
96
96
public async Task UpdateProduct ( Product product )
97
97
{
98
98
var content = JsonSerializer . Serialize ( product ) ;
99
99
var bodyContent = new StringContent ( content , Encoding . UTF8 , "application/json" ) ;
100
- var url = Path . Combine ( "https://localhost:5011/api/ products/ " , product . Id . ToString ( ) ) ;
100
+ var url = Path . Combine ( "products" , product . Id . ToString ( ) ) ;
101
101
102
- var putResult = await _client . PutAsync ( url , bodyContent ) ;
103
- var putContent = await putResult . Content . ReadAsStringAsync ( ) ;
102
+ var postResult = await _client . PutAsync ( url , bodyContent ) ;
103
+ var postContent = await postResult . Content . ReadAsStringAsync ( ) ;
104
104
105
- if ( ! putResult . IsSuccessStatusCode )
105
+ if ( ! postResult . IsSuccessStatusCode )
106
106
{
107
- throw new ApplicationException ( putContent ) ;
107
+ throw new ApplicationException ( postContent ) ;
108
108
}
109
109
}
110
110
111
111
public async Task DeleteProduct ( Guid id )
112
112
{
113
- var url = Path . Combine ( "https://localhost:5011/api/ products" , id . ToString ( ) ) ;
114
-
113
+ var url = Path . Combine ( "products" , id . ToString ( ) ) ;
114
+
115
115
var deleteResult = await _client . DeleteAsync ( url ) ;
116
116
var deleteContent = await deleteResult . Content . ReadAsStringAsync ( ) ;
117
117
0 commit comments