Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/asp core #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ repositories.config
[Oo]bj/
*.csproj.user
.idea/
*.DotSettings.user
18 changes: 18 additions & 0 deletions sample-app/AspFrontend/ActionFilters/IncludeLayoutDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace AspFrontend.ActionFilters
{
public class IncludeLayoutDataAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext.Result is ViewResult)
{
var bag = (filterContext.Result as ViewResult)!.ViewData;
bag["WaitStaff"] = StaticData.WaitStaff;
bag["ActiveTables"] = Domain.OpenTabQueries!.ActiveTableNumbers();
}
}
}
}
14 changes: 14 additions & 0 deletions sample-app/AspFrontend/AspFrontend.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CafeReadModels\CafeReadModels.csproj" />
<ProjectReference Include="..\Cafe\Cafe.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions sample-app/AspFrontend/Controllers/ChefController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Cafe.Tab;
using System.Text.RegularExpressions;
using AspFrontend.ActionFilters;
using Microsoft.AspNetCore.Mvc;

namespace AspFrontend.Controllers
{
[IncludeLayoutData]
public class ChefController : Controller
{
public ActionResult Index()
{
return View(Domain.ChefTodoListQueries!.GetTodoList());
}

public ActionResult MarkPrepared(Guid id, IFormCollection form)
{
Domain.Dispatcher!.SendCommand(new MarkFoodPrepared
{
Id = id,
MenuNumbers = (from entry in form.Keys.Cast<string>()
where form[entry] != "false"
let m = Regex.Match(entry, @"prepared_\d+_(\d+)")
where m.Success
select int.Parse(m.Groups[1].Value)
).ToList()
});

return RedirectToAction("Index");
}
}
}
33 changes: 33 additions & 0 deletions sample-app/AspFrontend/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Diagnostics;
using AspFrontend.ActionFilters;
using Microsoft.AspNetCore.Mvc;
using AspFrontend.Models;

namespace AspFrontend.Controllers;

[IncludeLayoutData]
public class HomeController : Controller
{
readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
117 changes: 117 additions & 0 deletions sample-app/AspFrontend/Controllers/TabController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Cafe.Tab;
using System.Text.RegularExpressions;
using AspFrontend.ActionFilters;
using AspFrontend.Models;
using Microsoft.AspNetCore.Mvc;

namespace AspFrontend.Controllers
{
[IncludeLayoutData]
public class TabController : Controller
{
public ActionResult Open()
{
return View();
}

[HttpPost]
public ActionResult Open(OpenTab cmd)
{
cmd.Id = Guid.NewGuid();
Domain.Dispatcher!.SendCommand(cmd);
return RedirectToAction("Order", new { id = cmd.TableNumber });
}

public ActionResult Close(int id) {
return View(new CloseModel
{
Id = id
});
}

[HttpPost]
public ActionResult Close(CloseModel model) {
Domain.Dispatcher!.SendCommand(new CloseTab {
Id = Domain.OpenTabQueries!.TabIdForTable(model.Id),
AmountPaid = model.AmountPaid
});
return RedirectToAction("Index", "Home");
}

public ActionResult Status(int id)
{
return View(Domain.OpenTabQueries!.TabForTable(id));
}

public ActionResult Order(int id)
{
return View(new OrderModel
{
Items = (from item in StaticData.Menu
select new OrderModel.OrderItem
{
MenuNumber = item.MenuNumber,
Description = item.Description,
NumberToOrder = 0
}).ToList(),
});
}

[HttpPost]
public ActionResult Order(int id, OrderModel order)
{
var items = new List<Events.Cafe.OrderedItem>();
var menuLookup = StaticData.Menu.ToDictionary(k => k.MenuNumber, v => v);
foreach (var item in order.Items)
for (int i = 0; i < item.NumberToOrder; i++)
items.Add(new Events.Cafe.OrderedItem
{
MenuNumber = item.MenuNumber,
Description = menuLookup[item.MenuNumber].Description,
Price = menuLookup[item.MenuNumber].Price,
IsDrink = menuLookup[item.MenuNumber].IsDrink
});

Domain.Dispatcher!.SendCommand(new PlaceOrder
{
Id = Domain.OpenTabQueries!.TabIdForTable(id),
Items = items
});

return RedirectToAction("Status", new { id = id });
}


public ActionResult MarkServed(int id, IFormCollection form)
{
var tabId = Domain.OpenTabQueries!.TabIdForTable(id);
var menuNumbers = (from entry in form.Keys.Cast<string>()
where form[entry] != "false"
let m = Regex.Match(entry, @"served_\d+_(\d+)")
where m.Success
select int.Parse(m.Groups[1].Value)
).ToList();

var menuLookup = StaticData.Menu.ToDictionary(k => k.MenuNumber, v => v);

var drinks = menuNumbers.Where(n => menuLookup[n].IsDrink).ToList();
if (drinks.Any())
Domain.Dispatcher!.SendCommand(new MarkDrinksServed
{
Id = tabId,
MenuNumbers = drinks
});

var food = menuNumbers.Where(n => !menuLookup[n].IsDrink).ToList();
if (food.Any())
Domain.Dispatcher!.SendCommand(new MarkFoodServed
{
Id = tabId,
MenuNumbers = food
});

return RedirectToAction("Status", new { id = id });
}

}
}
15 changes: 15 additions & 0 deletions sample-app/AspFrontend/Controllers/WaitStaffController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AspFrontend.ActionFilters;
using Microsoft.AspNetCore.Mvc;

namespace AspFrontend.Controllers
{
[IncludeLayoutData]
public class WaitStaffController : Controller
{
public ActionResult Todo(string id)
{
ViewData["Waiter"] = id;
return View(Domain.OpenTabQueries!.TodoListForWaiter(id));
}
}
}
27 changes: 27 additions & 0 deletions sample-app/AspFrontend/Domain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Edument.CQRS;
using CafeReadModels;
using Cafe.Tab;

namespace AspFrontend
{
public static class Domain
{
public static MessageDispatcher? Dispatcher;
public static IOpenTabQueries? OpenTabQueries;
public static IChefTodoListQueries? ChefTodoListQueries;

public static void Setup()
{
if (Dispatcher != null) return;
Dispatcher = new MessageDispatcher(new InMemoryEventStore());

Dispatcher.ScanInstance(new TabAggregate());

OpenTabQueries = new OpenTabs();
Dispatcher.ScanInstance(OpenTabQueries);

ChefTodoListQueries = new ChefTodoList();
Dispatcher.ScanInstance(ChefTodoListQueries);
}
}
}
8 changes: 8 additions & 0 deletions sample-app/AspFrontend/Models/CloseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AspFrontend.Models
{
public class CloseModel
{
public int Id { get; set; }
public decimal AmountPaid { get; set; }
}
}
8 changes: 8 additions & 0 deletions sample-app/AspFrontend/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AspFrontend.Models;

public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
19 changes: 19 additions & 0 deletions sample-app/AspFrontend/Models/OrderModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AspFrontend.Models
{
public class OrderModel
{
public class OrderItem
{
public int MenuNumber { get; set; }
public string Description { get; set; }
public int NumberToOrder { get; set; }
}

public List<OrderItem> Items { get; set; }
}
}
35 changes: 35 additions & 0 deletions sample-app/AspFrontend/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using AspFrontend;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.Use(async (context, next) =>
{
Domain.Setup();
await next(context);
});


app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
37 changes: 37 additions & 0 deletions sample-app/AspFrontend/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54614",
"sslPort": 44352
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5104",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7054;http://localhost:5104",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading