Commit 59c91ca 1 parent cff5c46 commit 59c91ca Copy full SHA for 59c91ca
File tree 10 files changed +118
-33
lines changed
10 files changed +118
-33
lines changed Original file line number Diff line number Diff line change
1
+ global using BlazorEcommerce . Shared ;
2
+ global using System . Net . Http . Json ;
3
+ global using BlazorEcommerce . Client . Services . ProductService ;
1
4
using BlazorEcommerce . Client ;
2
5
using Microsoft . AspNetCore . Components . Web ;
3
6
using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
7
10
builder . RootComponents . Add < HeadOutlet > ( "head::after" ) ;
8
11
9
12
builder . Services . AddScoped ( sp => new HttpClient { BaseAddress = new Uri ( builder . HostEnvironment . BaseAddress ) } ) ;
13
+ builder . Services . AddScoped < IProductService , ProductService > ( ) ;
10
14
11
15
await builder . Build ( ) . RunAsync ( ) ;
Original file line number Diff line number Diff line change
1
+ namespace BlazorEcommerce . Client . Services . ProductService
2
+ {
3
+ public interface IProductService
4
+ {
5
+ List < Product > Products { get ; set ; }
6
+ Task GetProducts ( ) ;
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ namespace BlazorEcommerce . Client . Services . ProductService
2
+ {
3
+ public class ProductService : IProductService
4
+ {
5
+ private readonly HttpClient _http ;
6
+
7
+ public ProductService ( HttpClient http )
8
+ {
9
+ _http = http ;
10
+ }
11
+
12
+ public List < Product > Products { get ; set ; } = new List < Product > ( ) ;
13
+
14
+ public async Task GetProducts ( )
15
+ {
16
+ var result =
17
+ await _http . GetFromJsonAsync < ServiceResponse < List < Product > > > ( "api/product" ) ;
18
+ if ( result != null && result . Data != null )
19
+ Products = result . Data ;
20
+ }
21
+ }
22
+ }
Original file line number Diff line number Diff line change 1
- @inject HttpClient Http
1
+ @inject IProductService ProductService
2
2
3
- <ul class =" list-unstyled" >
4
- @foreach ( var product in Products )
5
- {
6
- <li class =" media my-3" >
7
- <div class =" media-img-wrapper mr-2" >
8
- <a href =" #" >
9
- <img class =" media-img" src =" @product.ImageUrl" alt =" @product.Title" />
10
- </a >
11
- </div >
12
- <div class =" media-body" >
13
- <a href =" #" >
14
- <h4 class =" mb-0" >@product.Title </h4 >
15
- </a >
16
- <p >@product.Description </p >
17
- <h5 class =" price" >
18
- $@product.Price
19
- </h5 >
20
- </div >
21
- </li >
22
- }
23
- </ul >
3
+ @if (ProductService .Products == null || ProductService .Products .Count == 0 )
4
+ {
5
+ <span >Loading Products .. .</span >
6
+ }
7
+ else
8
+ {
9
+ <ul class =" list-unstyled" >
10
+ @foreach ( var product in ProductService .Products )
11
+ {
12
+ <li class =" media my-3" >
13
+ <div class =" media-img-wrapper mr-2" >
14
+ <a href =" #" >
15
+ <img class =" media-img" src =" @product.ImageUrl" alt =" @product.Title" />
16
+ </a >
17
+ </div >
18
+ <div class =" media-body" >
19
+ <a href =" #" >
20
+ <h4 class =" mb-0" >@product.Title </h4 >
21
+ </a >
22
+ <p >@product.Description </p >
23
+ <h5 class =" price" >
24
+ $@product.Price
25
+ </h5 >
26
+ </div >
27
+ </li >
28
+ }
29
+ </ul >
30
+ }
24
31
25
32
@code {
26
- private static List <Product > Products = new List <Product >();
27
-
28
33
protected override async Task OnInitializedAsync ()
29
34
{
30
- var result = await Http .GetFromJsonAsync <List <Product >>(" api/Product" );
31
- if (result != null )
32
- Products = result ;
35
+ await ProductService .GetProducts ();
33
36
}
34
37
}
Original file line number Diff line number Diff line change 9
9
@using BlazorEcommerce .Client
10
10
@using BlazorEcommerce .Client .Shared
11
11
@using BlazorEcommerce .Shared
12
+ @using BlazorEcommerce .Client .Services .ProductService
Original file line number Diff line number Diff line change @@ -7,18 +7,18 @@ namespace BlazorEcommerce.Server.Controllers
7
7
[ ApiController ]
8
8
public class ProductController : ControllerBase
9
9
{
10
- private readonly DataContext _context ;
10
+ private readonly IProductService _productService ;
11
11
12
- public ProductController ( DataContext context )
12
+ public ProductController ( IProductService productService )
13
13
{
14
- _context = context ;
14
+ _productService = productService ;
15
15
}
16
16
17
17
[ HttpGet ]
18
- public async Task < ActionResult < List < Product > > > GetProduct ( )
18
+ public async Task < ActionResult < ServiceResponse < List < Product > > > > GetProducts ( )
19
19
{
20
- var products = await _context . Products . ToListAsync ( ) ;
21
- return Ok ( products ) ;
20
+ var result = await _productService . GetProductsAsync ( ) ;
21
+ return Ok ( result ) ;
22
22
}
23
23
}
24
24
}
Original file line number Diff line number Diff line change 1
1
global using BlazorEcommerce . Shared ;
2
2
global using Microsoft . EntityFrameworkCore ;
3
3
global using BlazorEcommerce . Server . Data ;
4
+ global using BlazorEcommerce . Server . Services . ProductService ;
4
5
using Microsoft . AspNetCore . ResponseCompression ;
5
6
6
7
var builder = WebApplication . CreateBuilder ( args ) ;
17
18
builder . Services . AddEndpointsApiExplorer ( ) ;
18
19
builder . Services . AddSwaggerGen ( ) ;
19
20
21
+ builder . Services . AddScoped < IProductService , ProductService > ( ) ;
22
+
20
23
var app = builder . Build ( ) ;
21
24
22
25
app . UseSwaggerUI ( ) ;
Original file line number Diff line number Diff line change
1
+ namespace BlazorEcommerce . Server . Services . ProductService
2
+ {
3
+ public interface IProductService
4
+ {
5
+ Task < ServiceResponse < List < Product > > > GetProductsAsync ( ) ;
6
+ }
7
+ }
Original file line number Diff line number Diff line change
1
+ namespace BlazorEcommerce . Server . Services . ProductService
2
+ {
3
+ public class ProductService : IProductService
4
+ {
5
+ private readonly DataContext _context ;
6
+
7
+ public ProductService ( DataContext context )
8
+ {
9
+ _context = context ;
10
+ }
11
+
12
+ public async Task < ServiceResponse < List < Product > > > GetProductsAsync ( )
13
+ {
14
+ var response = new ServiceResponse < List < Product > >
15
+ {
16
+ Data = await _context . Products . ToListAsync ( )
17
+ } ;
18
+
19
+ return response ;
20
+ }
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+ using System . Threading . Tasks ;
6
+
7
+ namespace BlazorEcommerce . Shared
8
+ {
9
+ public class ServiceResponse < T >
10
+ {
11
+ public T ? Data { get ; set ; }
12
+ public bool Success { get ; set ; } = true ;
13
+ public string Message { get ; set ; } = string . Empty ;
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments