aboutsummaryrefslogtreecommitdiffstats
path: root/src/Pages/Status.cshtml.cs
blob: ed15120c011cfb1d1f82dc9621be2914a9d21f72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using VSH.Data;
using VSH.Data.Database;

namespace VSH.Pages;

public class Status : PageModel
{
	private readonly MainDbContext _context;
	public Order CurrentOrder { get; private set; }
	public List<StatusProduct> CurrentOrderProducts { get; }

	public Status(MainDbContext context) {
		_context = context;
		CurrentOrderProducts = new List<StatusProduct>();
	}

	public ActionResult OnGet(string orderReference) {
		try {
			CurrentOrder = _context.Orders.SingleOrDefault(o => o.OrderReference == orderReference);
			if (CurrentOrder == default) return Page();

			foreach (var orderProduct in CurrentOrder.Products) {
				var dbProduct = _context.Products.Include(c => c.Category)
										.SingleOrDefault(p => p.Id == orderProduct.Id);
				if (dbProduct == default) continue;
				CurrentOrderProducts.Add(new StatusProduct(dbProduct, orderProduct));
			}

			return Page();
		} catch (Exception e) {
			Console.WriteLine(e);
		}

		return Redirect("/errors/500");
	}

	public class StatusProduct
	{
		public StatusProduct(Product dbProdcut, OrderProduct orderProduct) {
			DbProdcut = dbProdcut;
			OrderProduct = orderProduct;
		}

		public Product DbProdcut { get; }
		public OrderProduct OrderProduct { get; }
	}
}