Skip to content

Commit 8d76efd

Browse files
committed
Product Details, Categories & Seeding More Products.
1 parent 59c91ca commit 8d76efd

19 files changed

+901
-13
lines changed

BlazorEcommerce.sln

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorEcommerce.Server", "BlazorEcommerce\Server\BlazorEcommerce.Server.csproj", "{0F0FC238-1551-45A7-BB93-2848E45D1765}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorEcommerce.Server", "BlazorEcommerce\Server\BlazorEcommerce.Server.csproj", "{0F0FC238-1551-45A7-BB93-2848E45D1765}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorEcommerce.Client", "BlazorEcommerce\Client\BlazorEcommerce.Client.csproj", "{4822AE95-A67F-4E5D-9829-207876DD1F0E}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorEcommerce.Client", "BlazorEcommerce\Client\BlazorEcommerce.Client.csproj", "{4822AE95-A67F-4E5D-9829-207876DD1F0E}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorEcommerce.Shared", "BlazorEcommerce\Shared\BlazorEcommerce.Shared.csproj", "{A0446615-92FA-4B59-A78B-FF568B990EF0}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorEcommerce.Shared", "BlazorEcommerce\Shared\BlazorEcommerce.Shared.csproj", "{A0446615-92FA-4B59-A78B-FF568B990EF0}"
1111
EndProject
1212
Global
1313
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@page "/product/{id:int}"
2+
@inject IProductService ProductService
3+
4+
@if (product == null)
5+
{
6+
<span>@message</span>
7+
}
8+
else
9+
{
10+
<div class="media">
11+
<div class="media-img-wrapper mr-2">
12+
<img class="media-img" src="@product.ImageUrl" alt="@product.Title" />
13+
</div>
14+
<div class="media-body">
15+
<h2 class="mb-0">@product.Title</h2>
16+
<p>@product.Description</p>
17+
<h4 class="price">
18+
$@product.Price
19+
</h4>
20+
</div>
21+
</div>
22+
23+
}
24+
25+
@code {
26+
private Product? product = null;
27+
private string message = string.Empty;
28+
29+
[Parameter]
30+
public int Id { get; set; }
31+
32+
protected override async Task OnParametersSetAsync()
33+
{
34+
message = "Loading product...";
35+
36+
var result = await ProductService.GetProduct(Id);
37+
if (!result.Success)
38+
{
39+
message = result.Message;
40+
}
41+
else
42+
{
43+
product = result.Data;
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.media-img {
2+
max-height: 500px;
3+
max-width: 500px;
4+
border-radius: 6px;
5+
margin: 0 10px 10px 10px;
6+
}
7+
8+
.media-img-wrapper {
9+
max-width: 500px;
10+
max-height: 500px;
11+
text-align: center;
12+
}
13+
14+
@media (max-width: 1023.98px) {
15+
.media {
16+
flex-direction: column;
17+
}
18+
19+
.media-img {
20+
max-width: 300px;
21+
}
22+
23+
.media-img-wrapper {
24+
width: 100%;
25+
height: auto;
26+
}
27+
}

BlazorEcommerce/Client/Services/ProductService/IProductService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public interface IProductService
44
{
55
List<Product> Products { get; set; }
66
Task GetProducts();
7+
Task<ServiceResponse<Product>> GetProduct(int productId);
78
}
89
}

BlazorEcommerce/Client/Services/ProductService/ProductService.cs

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ public ProductService(HttpClient http)
1111

1212
public List<Product> Products { get; set; } = new List<Product>();
1313

14+
public async Task<ServiceResponse<Product>> GetProduct(int productId)
15+
{
16+
var result = await _http.GetFromJsonAsync<ServiceResponse<Product>>($"api/product/{productId}");
17+
return result;
18+
}
19+
1420
public async Task GetProducts()
1521
{
1622
var result =

BlazorEcommerce/Client/Shared/ProductList.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ else
1111
{
1212
<li class="media my-3">
1313
<div class="media-img-wrapper mr-2">
14-
<a href="#">
14+
<a href="/product/@product.Id">
1515
<img class="media-img" src="@product.ImageUrl" alt="@product.Title" />
1616
</a>
1717
</div>
1818
<div class="media-body">
19-
<a href="#">
19+
<a href="/product/@product.Id">
2020
<h4 class="mb-0">@product.Title</h4>
2121
</a>
2222
<p>@product.Description</p>

BlazorEcommerce/Client/Shared/ProductList.razor.css

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
text-align: center;
1616
}
1717

18-
.price {
19-
color: green;
20-
}
21-
2218
@media (max-width: 1023.98px) {
2319
.media {
2420
flex-direction: column;

BlazorEcommerce/Client/wwwroot/css/app.css

+13
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,16 @@ a, .btn-link {
6262
.blazor-error-boundary::after {
6363
content: "An error has occurred."
6464
}
65+
66+
.price {
67+
color: green;
68+
}
69+
70+
.media {
71+
display: flex;
72+
align-items: flex-start;
73+
}
74+
75+
.media-body {
76+
flex: 1;
77+
}

BlazorEcommerce/Server/Controllers/ProductController.cs

+7
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@ public async Task<ActionResult<ServiceResponse<List<Product>>>> GetProducts()
2020
var result = await _productService.GetProductsAsync();
2121
return Ok(result);
2222
}
23+
24+
[HttpGet("{productId}")]
25+
public async Task<ActionResult<ServiceResponse<Product>>> GetProduct(int productId)
26+
{
27+
var result = await _productService.GetProductAsync(productId);
28+
return Ok(result);
29+
}
2330
}
2431
}

BlazorEcommerce/Server/Data/DataContext.cs

+103-4
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,133 @@ public DataContext(DbContextOptions<DataContext> options) : base(options)
99

1010
protected override void OnModelCreating(ModelBuilder modelBuilder)
1111
{
12+
modelBuilder.Entity<Category>().HasData(
13+
new Category
14+
{
15+
Id = 1,
16+
Name = "Books",
17+
Url = "books"
18+
},
19+
new Category
20+
{
21+
Id = 2,
22+
Name = "Movies",
23+
Url = "movies"
24+
},
25+
new Category
26+
{
27+
Id = 3,
28+
Name = "Video Games",
29+
Url = "video-games"
30+
}
31+
);
32+
1233
modelBuilder.Entity<Product>().HasData(
1334
new Product
1435
{
1536
Id = 1,
1637
Title = "The Hitchhiker's Guide to the Galaxy",
1738
Description = "The Hitchhiker's Guide to the Galaxy[note 1] (sometimes referred to as HG2G,[1] HHGTTG,[2] H2G2,[3] or tHGttG) is a comedy science fiction franchise created by Douglas Adams. Originally a 1978 radio comedy broadcast on BBC Radio 4, it was later adapted to other formats, including stage shows, novels, comic books, a 1981 TV series, a 1984 text-based computer game, and 2005 feature film.",
1839
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/b/bd/H2G2_UK_front_cover.jpg",
19-
Price = 9.99m
40+
Price = 9.99m,
41+
CategoryId = 1
2042
},
2143
new Product
2244
{
2345
Id = 2,
2446
Title = "Ready Player One",
2547
Description = "Ready Player One is a 2011 science fiction novel, and the debut novel of American author Ernest Cline. The story, set in a dystopia in 2045, follows protagonist Wade Watts on his search for an Easter egg in a worldwide virtual reality game, the discovery of which would lead him to inherit the game creator's fortune. Cline sold the rights to publish the novel in June 2010, in a bidding war to the Crown Publishing Group (a division of Random House).[1] The book was published on August 16, 2011.[2] An audiobook was released the same day; it was narrated by Wil Wheaton, who was mentioned briefly in one of the chapters.[3][4]Ch. 20 In 2012, the book received an Alex Award from the Young Adult Library Services Association division of the American Library Association[5] and won the 2011 Prometheus Award.[6]",
2648
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/a/a4/Ready_Player_One_cover.jpg",
27-
Price = 7.99m
49+
Price = 7.99m,
50+
CategoryId = 1
2851
},
2952
new Product
3053
{
3154
Id = 3,
3255
Title = "Nineteen Eighty-Four",
3356
Description = "Nineteen Eighty-Four (also stylised as 1984) is a dystopian social science fiction novel and cautionary tale written by English writer George Orwell. It was published on 8 June 1949 by Secker & Warburg as Orwell's ninth and final book completed in his lifetime. Thematically, it centres on the consequences of totalitarianism, mass surveillance and repressive regimentation of people and behaviours within society.[2][3] Orwell, a democratic socialist, modelled the totalitarian government in the novel after Stalinist Russia and Nazi Germany.[2][3][4] More broadly, the novel examines the role of truth and facts within politics and the ways in which they are manipulated.",
3457
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/c/c3/1984first.jpg",
35-
Price = 6.99m
58+
Price = 6.99m,
59+
CategoryId = 1
60+
},
61+
new Product
62+
{
63+
Id = 4,
64+
CategoryId = 2,
65+
Price = 4.99m,
66+
Title = "The Matrix",
67+
Description = "The Matrix is a 1999 science fiction action film written and directed by the Wachowskis, and produced by Joel Silver. Starring Keanu Reeves, Laurence Fishburne, Carrie-Anne Moss, Hugo Weaving, and Joe Pantoliano, and as the first installment in the Matrix franchise, it depicts a dystopian future in which humanity is unknowingly trapped inside a simulated reality, the Matrix, which intelligent machines have created to distract humans while using their bodies as an energy source. When computer programmer Thomas Anderson, under the hacker alias \"Neo\", uncovers the truth, he \"is drawn into a rebellion against the machines\" along with other people who have been freed from the Matrix.",
68+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/c/c1/The_Matrix_Poster.jpg",
69+
},
70+
new Product
71+
{
72+
Id = 5,
73+
CategoryId = 2,
74+
Price = 3.99m,
75+
Title = "Back to the Future",
76+
Description = "Back to the Future is a 1985 American science fiction film directed by Robert Zemeckis. Written by Zemeckis and Bob Gale, it stars Michael J. Fox, Christopher Lloyd, Lea Thompson, Crispin Glover, and Thomas F. Wilson. Set in 1985, the story follows Marty McFly (Fox), a teenager accidentally sent back to 1955 in a time-traveling DeLorean automobile built by his eccentric scientist friend Doctor Emmett \"Doc\" Brown (Lloyd). Trapped in the past, Marty inadvertently prevents his future parents' meeting—threatening his very existence—and is forced to reconcile the pair and somehow get back to the future.",
77+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/d/d2/Back_to_the_Future.jpg",
78+
},
79+
new Product
80+
{
81+
Id = 6,
82+
CategoryId = 2,
83+
Price = 2.99m,
84+
Title = "Toy Story",
85+
Description = "Toy Story is a 1995 American computer-animated comedy film produced by Pixar Animation Studios and released by Walt Disney Pictures. The first installment in the Toy Story franchise, it was the first entirely computer-animated feature film, as well as the first feature film from Pixar. The film was directed by John Lasseter (in his feature directorial debut), and written by Joss Whedon, Andrew Stanton, Joel Cohen, and Alec Sokolow from a story by Lasseter, Stanton, Pete Docter, and Joe Ranft. The film features music by Randy Newman, was produced by Bonnie Arnold and Ralph Guggenheim, and was executive-produced by Steve Jobs and Edwin Catmull. The film features the voices of Tom Hanks, Tim Allen, Don Rickles, Wallace Shawn, John Ratzenberger, Jim Varney, Annie Potts, R. Lee Ermey, John Morris, Laurie Metcalf, and Erik von Detten. Taking place in a world where anthropomorphic toys come to life when humans are not present, the plot focuses on the relationship between an old-fashioned pull-string cowboy doll named Woody and an astronaut action figure, Buzz Lightyear, as they evolve from rivals competing for the affections of their owner, Andy Davis, to friends who work together to be reunited with Andy after being separated from him.",
86+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/1/13/Toy_Story.jpg",
87+
88+
},
89+
new Product
90+
{
91+
Id = 7,
92+
CategoryId = 3,
93+
Title = "Half-Life 2",
94+
Price = 49.99m,
95+
Description = "Half-Life 2 is a 2004 first-person shooter game developed and published by Valve. Like the original Half-Life, it combines shooting, puzzles, and storytelling, and adds features such as vehicles and physics-based gameplay.",
96+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/2/25/Half-Life_2_cover.jpg",
97+
98+
},
99+
new Product
100+
{
101+
Id = 8,
102+
CategoryId = 3,
103+
Title = "Diablo II",
104+
Price = 9.99m,
105+
Description = "Diablo II is an action role-playing hack-and-slash computer video game developed by Blizzard North and published by Blizzard Entertainment in 2000 for Microsoft Windows, Classic Mac OS, and macOS.",
106+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/d/d5/Diablo_II_Coverart.png",
107+
},
108+
new Product
109+
{
110+
Id = 9,
111+
CategoryId = 3,
112+
Price = 14.99m,
113+
Title = "Day of the Tentacle",
114+
Description = "Day of the Tentacle, also known as Maniac Mansion II: Day of the Tentacle, is a 1993 graphic adventure game developed and published by LucasArts. It is the sequel to the 1987 game Maniac Mansion.",
115+
ImageUrl = "https://upload.wikimedia.org/wikipedia/en/7/79/Day_of_the_Tentacle_artwork.jpg",
116+
},
117+
new Product
118+
{
119+
Id = 10,
120+
CategoryId = 3,
121+
Price = 159.99m,
122+
Title = "Xbox",
123+
Description = "The Xbox is a home video game console and the first installment in the Xbox series of video game consoles manufactured by Microsoft.",
124+
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/4/43/Xbox-console.jpg",
125+
},
126+
new Product
127+
{
128+
Id = 11,
129+
CategoryId = 3,
130+
Price = 79.99m,
131+
Title = "Super Nintendo Entertainment System",
132+
Description = "The Super Nintendo Entertainment System (SNES), also known as the Super NES or Super Nintendo, is a 16-bit home video game console developed by Nintendo that was released in 1990 in Japan and South Korea.",
133+
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/e/ee/Nintendo-Super-Famicom-Set-FL.jpg",
36134
}
37-
);
135+
);
38136
}
39137

40138
public DbSet<Product> Products { get; set; }
139+
public DbSet<Category> Categories { get; set; }
41140
}
42141
}

0 commit comments

Comments
 (0)