Skip to content

Commit

Permalink
implement the viewing of orders on the account
Browse files Browse the repository at this point in the history
ported over the html and cleaned it up quite a bit. removed useless styling and logic. now it's a much cleaner solution compared to the original

ran into a small issue however, when i originally specified the "on delete" action on the address in the order, i mistakenly specified a one-to-one relationship between the order and address which is inaccurate. one address could appear on many orders but one order cannot contain many addresses so that adjustment was made. what would happen is that the relationship would be severed after one order with the same address used on retrieving the orders from the database. it did continue to work when it is added to the database however
  • Loading branch information
ShaylenReddy42 committed Aug 5, 2022
1 parent 7be304a commit b09d30c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
7 changes: 6 additions & 1 deletion SeelansTyres.Mvc/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ public async Task<IActionResult> Index()

var addresses = await response.Content.ReadFromJsonAsync<IEnumerable<AddressModel>>();

response = await client.GetAsync($"api/orders?customerId={customer.Id}");

var orders = await response.Content.ReadFromJsonAsync<IEnumerable<OrderModel>>();

var accountViewModel = new AccountViewModel
{
Customer = customerModel,
Addresses = addresses!
Addresses = addresses!,
Orders = orders!
};

return View(accountViewModel);
Expand Down
1 change: 1 addition & 0 deletions SeelansTyres.Mvc/ViewModels/AccountViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class AccountViewModel
public CreateAddressModel CreateAddressModel { get; set; } = null!;
public CustomerModel Customer { get; set; } = null!;
public IEnumerable<AddressModel> Addresses { get; set; } = null!;
public IEnumerable<OrderModel> Orders { get; set; } = null!;
}
37 changes: 33 additions & 4 deletions SeelansTyres.Mvc/Views/Account/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,40 @@
<h4 class="seelanstyres-color">My Orders</h4>
<p>Check the status of your orders or browser through your past purchases</p>
<hr class="my-5">
<div class="orders text-center">
<p>You haven't placed any orders yet</p>
<a class="btn btn-dark" asp-controller="Home" asp-action="Shop">Shop Now</a>
<div class="text-center">
@if (Model.Orders.Count() is 0)
{
<p>You haven't placed any orders yet</p>
<a class="btn btn-dark" asp-controller="Home" asp-action="Shop">Shop Now</a>
}
else
{
foreach (var order in Model.Orders)
{
<div class="card card-body mb-2">
<div class="d-flex justify-content-between">
<h5>Order ID: @order.Id</h5>
<small>@order.OrderPlaced</small>
</div>
<p class="m-0">Address: @order.Address!.AddressLine1</p>
<p class="m-0">Total Price: @($"{order.TotalPrice:R0.00}")</p>
<button class="btn btn-dark w-100 mt-2 mb-1" data-bs-toggle="collapse" data-bs-target=@($"#items{order.Id}") aria-controls=@($"items{order.Id}")>View Items</button>
<div class="collapse" id=@($"items{order.Id}")>
@foreach (var item in order.OrderItems)
{
<div class="card card-body">
<p class="m-0">Name: @item.Tyre!.Name</p>
<p class="m-0">Quantity: @item.Quantity</p>
<p class="m-0">Price: @($"{item.Tyre.Price:R0.00}")</p>
<p class="m-0">Total Price: @($"{(item.Tyre.Price * item.Quantity):R0.00}")</p>
</div>
}
</div>
</div>
}
}
</div>
<hr>
<hr class="my-5">
</div>
<div id="addresses-pill" class="tab-pane fade text-start pt-3">
<h4 class="seelanstyres-color">My Addresses</h4>
Expand Down
8 changes: 0 additions & 8 deletions SeelansTyres.Mvc/wwwroot/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@
background-size: cover;
}

.orders p {
margin-top: 100px;
}

.orders a {
margin-bottom: 100px;
}

/* end of the account page css */

/* start of admin page css */
Expand Down
4 changes: 2 additions & 2 deletions SeelansTyres.WebApi/Data/SeelansTyresContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ protected override void OnModelCreating(ModelBuilder builder)
builder
.Entity<Order>()
.HasOne(order => order.Address)
.WithOne()
.OnDelete(DeleteBehavior.ClientNoAction);
.WithMany()
.OnDelete(DeleteBehavior.NoAction);

builder
.Entity<Order>()
Expand Down

0 comments on commit b09d30c

Please sign in to comment.