Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
muhmuhhum committed Oct 12, 2020
1 parent 55aeb11 commit cdb6c76
Show file tree
Hide file tree
Showing 35 changed files with 1,337 additions and 14 deletions.
2 changes: 1 addition & 1 deletion EinsamerWanderer.API/Controllers/ItemController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EinsamerWanderer.API.Model;
using EinsamerWanderer.API.Queries;
using EinsamerWanderer.API.RestContract.V1;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -11,6 +10,7 @@
using EinsamerWanderer.API.Commands;
using EinsamerWanderer.API.Request;
using EinsamerWanderer.API.Response;
using EinsamerWanderer.Shared.RestContract.V1;

namespace EinsamerWanderer.API.Controllers
{
Expand Down
17 changes: 15 additions & 2 deletions EinsamerWanderer.API/DbContexts/EinsamerWandererDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using EinsamerWanderer.API.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EinsamerWanderer.API.DbContext
{
public class EinsamerWandererDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public EinsamerWandererDbContext(DbContextOptions<EinsamerWandererDbContext> options)
private ILoggerFactory _loggerFactory;

public EinsamerWandererDbContext(DbContextOptions<EinsamerWandererDbContext> options,
ILoggerFactory loggerFactory)
: base(options)
{ }
{
_loggerFactory = loggerFactory;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.UseLoggerFactory(_loggerFactory);
base.OnConfiguring(optionsBuilder);
}

public DbSet<Item> Items { get; set; }
}
Expand Down
11 changes: 11 additions & 0 deletions EinsamerWanderer.API/Model/Character.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace EinsamerWanderer.API.Model
{
public class Character
{
public Guid Id { get; set; }
public string Name { get; set; }
public long Experience { get; set; }
}
}
16 changes: 7 additions & 9 deletions EinsamerWanderer.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
using Serilog;

namespace EinsamerWanderer.API
Expand All @@ -23,21 +20,15 @@ public Startup(IConfiguration configuration)
Configuration = configuration;
}

public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => { builder.AddConsole(); });

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<EinsamerWandererDbContext>(opt =>
{
opt.UseLoggerFactory(MyLoggerFactory);
opt.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"));
opt.EnableSensitiveDataLogging(true);
});

services.AddControllers();
services.AddSwaggerGen();
services.AddMediatR(typeof(Startup));
Expand All @@ -63,6 +54,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRouting();

if (env.IsDevelopment())
{
app.UseCors(
options => options.SetIsOriginAllowed(x => _ = true).AllowAnyMethod().AllowAnyHeader().AllowCredentials()
);
}

app.UseAuthorization();

app.UseEndpoints(endpoints =>
Expand Down
8 changes: 8 additions & 0 deletions EinsamerWanderer.Shared/Request/CreateItemRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EinsamerWanderer.Shared.Request
{
public class CreateItemRequest
{
public string Name { get; set; }
public double Value { get; set; }
}
}
8 changes: 8 additions & 0 deletions EinsamerWanderer.Shared/Request/UpdateItemRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EinsamerWanderer.Shared.Request
{
public class UpdateItemRequest
{
public string Name { get; set; }
public double Value { get; set; }
}
}
11 changes: 11 additions & 0 deletions EinsamerWanderer.Shared/Response/ItemResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace EinsamerWanderer.Shared.Request
{
public class ItemResponse
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Value { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EinsamerWanderer.API.RestContract.V1
namespace EinsamerWanderer.Shared.RestContract.V1
{
public partial class Routes
{
Expand Down
10 changes: 10 additions & 0 deletions EinsamerWanderer.Web/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
19 changes: 19 additions & 0 deletions EinsamerWanderer.Web/EinsamerWanderer.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EinsamerWanderer.Shared\EinsamerWanderer.Shared.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions EinsamerWanderer.Web/Pages/AddItem.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@page "/addItem"
@using EinsamerWanderer.Shared.Request
@using EinsamerWanderer.Shared.RestContract.V1
@inject HttpClient Http


<h3>AddItem</h3>

<EditForm Model="@_createItemRequest" OnValidSubmit="@HandleValidSubmit">
<input id="Name" @bind="_createItemRequest.Name" />
<input id="Value" @bind="_createItemRequest.Value" />
<button type="submit">Submit</button>
</EditForm>

@code {
private CreateItemRequest _createItemRequest = new CreateItemRequest();

private void HandleValidSubmit()
{
Http.PostAsJsonAsync("http://localhost:5000" + Routes.Item.Create, _createItemRequest);
}

}
32 changes: 32 additions & 0 deletions EinsamerWanderer.Web/Pages/ChangeItem.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@page "/changeItem/{itemId}"
@using EinsamerWanderer.Shared.Request
@using EinsamerWanderer.Shared.RestContract.V1
@inject HttpClient Http
@inject NavigationManager NavigationManager
<h3>ChangeItem</h3>

<EditForm Model="@_updateItemRequest" OnValidSubmit="@HandleValidSubmit">
<input id="Name" @bind="_updateItemRequest.Name" />
<input id="Value" @bind="_updateItemRequest.Value" />
<button type="submit">Submit</button>
</EditForm>

@code {
[Parameter]
public string ItemId { get; set; }
private UpdateItemRequest _updateItemRequest = new UpdateItemRequest();
protected override async Task OnInitializedAsync()
{
var response = await Http.GetFromJsonAsync<ItemResponse>(
"http://localhost:5000" + Routes.Item.Get.Replace("{itemId}", ItemId));
_updateItemRequest.Value = response.Value;
_updateItemRequest.Name = response.Name;
}

private async void HandleValidSubmit()
{
await Http.PutAsJsonAsync("http://localhost:5000" + Routes.Item.Update.Replace("{itemId}", ItemId), _updateItemRequest);
NavigationManager.NavigateTo("/items");
}

}
7 changes: 7 additions & 0 deletions EinsamerWanderer.Web/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />
60 changes: 60 additions & 0 deletions EinsamerWanderer.Web/Pages/Items.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@using EinsamerWanderer.Shared.Request
@using EinsamerWanderer.Shared.RestContract.V1

@inject NavigationManager NavigationManager
@inject HttpClient Http
@page "/Items"
<h3>Items</h3>

<div class="justify-content-center ">
<button @onclick="NavigateToAdd" class="btn btn-info">Add</button>
<table class="table table-responsive w-50">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Value</th>
</tr>
</thead>
<tbody>
@foreach (var item in _itemResponses)
{
var varItem = item;
<tr>
<td>@item.Name</td>
<td>@item.Value</td>
<td>
<button @onclick="@(() => NavigateToChange(varItem.Id))" class="btn btn-light btn-lg">Change</button>
</td>
<td>
<button @onclick="@(() => RemoveItem(varItem.Id))" class="btn btn-light">X</button>
</td>
</tr>
}
</tbody>
</table>
</div>
@code {
private List<ItemResponse> _itemResponses = new List<ItemResponse>();

protected override async Task OnInitializedAsync()
{
_itemResponses = await Http.GetFromJsonAsync<List<ItemResponse>>("http://localhost:5000" + Routes.Item.GetAll);
}

private void NavigateToAdd()
{
NavigationManager.NavigateTo("/AddItem");
}

private void NavigateToChange(Guid itemId)
{
NavigationManager.NavigateTo($"/ChangeItem/{itemId.ToString()}");
}

private async Task RemoveItem(Guid itemId)
{
await Http.DeleteAsync("http://localhost:5000" + Routes.Item.Delete.Replace("{itemId}", itemId.ToString()));
_itemResponses.Remove(_itemResponses.FirstOrDefault(x => x.Id == itemId));
}

}
25 changes: 25 additions & 0 deletions EinsamerWanderer.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace EinsamerWanderer.Web
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
}
}
}
29 changes: 29 additions & 0 deletions EinsamerWanderer.Web/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56973",
"sslPort": 44393
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"EinsamerWanderer.Web": {
"commandName": "Project",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:5001;http://localhost:5005",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
15 changes: 15 additions & 0 deletions EinsamerWanderer.Web/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@inherits LayoutComponentBase

<div class="sidebar">
<NavMenu />
</div>

<div class="main">
<div class="top-row px-4">
<a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
</div>

<div class="content px-4">
@Body
</div>
</div>
Loading

0 comments on commit cdb6c76

Please sign in to comment.