Skip to content

Commit 03c7bf9

Browse files
committed
feat(Product): persiste os dados no banco
1 parent e740918 commit 03c7bf9

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/BlazingShop/Products/CreateProduct/CreateProductPage.razor

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
</div>
4545
</div>
4646

47+
48+
@if (!string.IsNullOrEmpty(_errorMessage))
49+
{
50+
<div class="alert alert-danger" role="alert">
51+
@_errorMessage
52+
</div>
53+
}
54+
4755
<button type="submit" class="btn btn-success">Save</button>
4856
<a href="/products" class="btn btn-secondary">Cancel</a>
4957

src/BlazingShop/Products/CreateProduct/CreateProductPage.razor.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BlazingShop.Data;
2+
using BlazingShop.Models;
23
using BlazingShop.Products.Common;
34
using Microsoft.AspNetCore.Components;
45
using Microsoft.EntityFrameworkCore;
@@ -8,6 +9,7 @@ namespace BlazingShop.Products.CreateProduct;
89
public partial class CreateProductPage
910
{
1011
private readonly CreateProductInput _model = new();
12+
private string _errorMessage = string.Empty;
1113

1214
private IEnumerable<GetCategoriesQuery> _categories = [];
1315

@@ -21,5 +23,25 @@ protected override async Task OnInitializedAsync()
2123
.ToListAsync();
2224
}
2325

24-
async Task HandleSubmitAsync() { }
26+
public async Task HandleSubmitAsync()
27+
{
28+
try
29+
{
30+
var category = await Context.Categories.FindAsync(_model.CategoryId);
31+
32+
if (category is null)
33+
{
34+
_errorMessage = $"Categoria com identificador {_model.CategoryId} não encontrada";
35+
return;
36+
}
37+
38+
var product = new Product(_model.Title, _model.Description, _model.Image, _model.Price, category);
39+
await Context.Products.AddAsync(product);
40+
}
41+
catch (Exception ex)
42+
{
43+
_errorMessage = ex.Message;
44+
throw;
45+
}
46+
}
2547
}

0 commit comments

Comments
 (0)