CRUD CustomDescriptions

pull/1/head
DJh2o2 2022-11-24 13:49:16 +07:00
parent f9deadb7e0
commit cb97427af3
13 changed files with 598 additions and 7 deletions

@ -9,6 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazorise.Bootstrap" Version="1.1.3.1" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.1.3.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.18.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">

@ -3,7 +3,7 @@
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject ContactService ContactService
@inject ContactService contactService
<h1>Contacts</h1>
@ -72,7 +72,7 @@ else
protected override async Task OnInitializedAsync()
{
allContacts = await Task.Run(() => ContactService.GetAllContactsAsync());
allContacts = await Task.Run(() => contactService.GetAllContactsAsync());
filteredContacts = allContacts;
}

@ -0,0 +1,62 @@
@page "/CustomDescriptions/Add"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject CustomDescriptionService customDescriptionService
@inject NavigationManager navigationManager
<h2>Add CustomDescription</h2>
<hr />
<form>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="CustomDescriptionId" class="control-label">CustomDescriptionId</label>
<input form="CustomDescriptionId" class="form-control" @bind="@customDescription.CustomDescriptionId" />
</div>
<div class="form-group">
<label for="ProductNumber" class="control-label">ProductNumber</label>
<input form="ProductNumber" class="form-control" @bind="@customDescription.ProductNumber" />
</div>
<div class="form-group">
<label for="OptionNumber" class="control-label">OptionNumber</label>
<input form="OptionNumber" class="form-control" @bind="@customDescription.OptionNumber" />
</div>
<div class="form-group">
<label for="Heading" class="control-label">Heading</label>
<input form="Heading" class="form-control" @bind="@customDescription.Heading" />
</div>
<div class="form-group">
<label for="DescriptionText" class="control-label">DescriptionText</label>
<input form="DescriptionText" class="form-control" @bind="@customDescription.DescriptionText" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" @onclick="@CreateCustomDescription" value="Save"/>
<input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel"/>
</div>
</div>
</div>
</form>
@code {
CustomDescription customDescription = new CustomDescription();
protected async void CreateCustomDescription()
{
customDescription.DataModificationByUser = "Gremlin_BlazorServer";
if (await customDescriptionService.InsertCustomDescriptionAsync(customDescription))
{
navigationManager.NavigateTo("CustomDescriptions/Index");
}
}
void Cancel() => navigationManager.NavigateTo("CustomDescriptions/Index");
}

@ -0,0 +1,66 @@
@page "/CustomDescriptions/Delete/{CustomDescriptionId}"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject CustomDescriptionService customDescriptionService
@inject NavigationManager navigationManager
<h2>Delete Employee</h2>
<hr />
<h3>Are you sure want to delete this?</h3>
<form>
<div class="row">
<div class=" col-md-8">
<div class="form-group">
<label>CustomDescriptionId:</label>
<label>@customDescription.CustomDescriptionId</label>
</div>
<div class="form-group">
<label>ProductNumber:</label>
<label>@customDescription.ProductNumber</label>
</div>
<div class="form-group">
<label>OptionNumber:</label>
<label>@customDescription.OptionNumber</label>
</div>
<div class="form-group">
<label>Heading:</label>
<label>@customDescription.Heading</label>
</div>
<div class="form-group">
<label>DescriptionText:</label>
<label>@customDescription.DescriptionText</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-danger" @onclick="@DeleteCustomDescription" value="Delete" />
<input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel" />
</div>
</div>
</div>
</form>
@code {
[Parameter]
public string customDescriptionId { get; set; } = default!;
CustomDescription? customDescription = new CustomDescription();
protected override async Task OnInitializedAsync()
{
customDescription = await Task.Run(() => customDescriptionService.GetCustomDescriptionAsync(Convert.ToUInt32(customDescriptionId)));
}
protected async void DeleteCustomDescription()
{
await customDescriptionService.DeleteCustomDescriptionAsync(customDescription);
navigationManager.NavigateTo("CustomDescriptions/Index");
}
void Cancel()
{
navigationManager.NavigateTo("CustomDescriptions/Index");
}
}

@ -0,0 +1,72 @@
@page "/CustomDescriptions/Edit/{CustomDescriptionId}"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject CustomDescriptionService customDescriptionservice
@inject NavigationManager navigationManager
<h2>Edit CustomDescription</h2>
<hr />
<form>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="CustomDescriptionId" class="control-label">CustomDescriptionId</label>
<input form="CustomDescriptionId" class="form-control" @bind="@customDescription.CustomDescriptionId" />
</div>
<div class="form-group">
<label for="CustomDescriptionId" class="control-label">CustomDescriptionId</label>
<input form="CustomDescriptionId" class="form-control" @bind="@customDescription.CustomDescriptionId" />
</div>
<div class="form-group">
<label for="ProductNumber" class="control-label">ProductNumber</label>
<input form="ProductNumber" class="form-control" @bind="@customDescription.ProductNumber" />
</div>
<div class="form-group">
<label for="OptionNumber" class="control-label">OptionNumber</label>
<input form="OptionNumber" class="form-control" @bind="@customDescription.OptionNumber" />
</div>
<div class="form-group">
<label for="Heading" class="control-label">Heading</label>
<input form="Heading" class="form-control" @bind="@customDescription.Heading" />
</div>
<div class="form-group">
<label for="DescriptionText" class="control-label">DescriptionText</label>
<input form="DescriptionText" class="form-control" @bind="@customDescription.DescriptionText" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="button" class="btn btn-primary" @onclick="@UpdateCustomDescription" value="Update" />
<input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel" />
</div>
</div>
</div>
</form>
@code {
[Parameter]
public string? customDescriptionId { get; set; }
CustomDescription? customDescription = new CustomDescription();
protected override async Task OnInitializedAsync()
{
customDescription = await Task.Run(() => customDescriptionservice.GetCustomDescriptionAsync(Convert.ToUInt32(customDescriptionId)));
}
protected async void UpdateCustomDescription()
{
customDescription.DataModificationByUser = "Gremlin_BlazorServer";
customDescription.DataModificationDate = DateTime.Now;
customDescription.DataVersionNumber++;
await customDescriptionservice.UpdateCustomDescriptionAsync(customDescription);
navigationManager.NavigateTo("CustomDescriptions/Index");
}
void Cancel() => navigationManager.NavigateTo("CustomDescriptions/Index");
}

@ -0,0 +1,79 @@
@page "/CustomDescriptions/Index"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject CustomDescriptionService customDescriptionservice
<h1>CustomDescriptions</h1>
<div class="text-center bg-blue-100">
<input class="border-4 w-1/3 rounded m-6 p-6 h-8
border-blue-300" @bind-value="searchCustomDescription"
@bind-value:event="oninput" placeholder="Search by ProductNumber"
@onchange="SearchCustomDescription_OnChange"/>
</div>
<NavLink class="nav-link" href="CustomDescriptions/Add">
<span class="oi oi-plus" aria-hidden="true">Add New CustomDescription</span>
</NavLink>
@if (allCustomDescriptions is null)
{
<p><em>Loading... !</em></p>
}
else
{
<p>Es wurden @filteredCustomDescriptions.Count CustomDescriptions gefunden.</p>
@if (allCustomDescriptions != null)
{
<table class="table">
<thead>
<tr>
<th>@nameof(CustomDescription.CustomDescriptionId)</th>
<th>@nameof(CustomDescription.ProductNumber)</th>
<th>@nameof(CustomDescription.OptionNumber)</th>
<th>@nameof(CustomDescription.Heading)</th>
<th>@nameof(CustomDescription.DescriptionText)</th>
</tr>
</thead>
<tbody>
@foreach (CustomDescription customDescription in filteredCustomDescriptions)
{<tr>
<td>@customDescription.CustomDescriptionId</td>
<td>@customDescription.ProductNumber</td>
<td>@customDescription.OptionNumber</td>
<td>@customDescription.Heading</td>
<td>@customDescription.DescriptionText</td>
<td>
<a class="nav-link" href="CustomDescriptions/Edit/@customDescription.CustomDescriptionId">
<span class="oi oi-pencil" aria-hidden="true">Edit</span>
</a>
<a class="nav-link" href="CustomDescriptions/Delete/@customDescription.CustomDescriptionId">
<span class="oi oi-trash" aria-hidden="true">Delete</span>
</a>
</td>
</tr>}
</tbody>
</table>
}
}
@code {
public string searchCustomDescription = "";
List<CustomDescription> allCustomDescriptions = new();
List<CustomDescription> filteredCustomDescriptions = new();
protected override async Task OnInitializedAsync()
{
allCustomDescriptions = await Task.Run(() => customDescriptionservice.GetAllCustomDescriptionsAsync());
filteredCustomDescriptions = allCustomDescriptions;
}
private void SearchCustomDescription_OnChange()
{
filteredCustomDescriptions = allCustomDescriptions.Where(cD => cD.ProductNumber.ToLower().Contains(searchCustomDescription.ToLower())).ToList();
}
}

@ -1,7 +1,37 @@
@page "/"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
<PageTitle>Index</PageTitle>
@inject LoginService loginService
<h1>Herzlich Willkommen bei Gremlin Blazor</h1>
<PageTitle>Gremlin BlazorServer</PageTitle>
Dies ist die neue WebApp für Gremlin.
<CascadingAuthenticationState>
<AuthorizeView>
<Authorized>
<h1>Hello, @registeredUser.UserName!</h1>
<p>You can only see this content if you're authorized.</p>
<button @onclick="SecureMethod">Authorized Only Button</button>
</Authorized>
<NotAuthorized>
<h1>Welcome to Gremlin BlazorServer</h1>
<h2>Authentication Failure!</h2>
<p>You're not signed in.</p>
</NotAuthorized>
</AuthorizeView>
</CascadingAuthenticationState>
@code {
private RegisteredUser registeredUser = new();
private bool IsAuthenticated { get; set; }
//protected async override Task OnInitializedAsync()
//{
// IsAuthenticated = await Task.Run(() => loginService.ValidateUserAsync(registeredUser));
//}
private void SecureMethod()
{
}
}

@ -0,0 +1,141 @@
@page "/Login"
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject LoginService loginService
<h3>Login</h3>
<Container>
<Row>
<Column>
<Tabs SelectedTab="@selectedTab" SelectedTabChanged="@OnSelectedTabChanged" Pills FullWidth>
<Items>
<Tab Name="login">Login</Tab>
<Tab Name="register">Register</Tab>
</Items>
<Content>
<TabPanel Name="login">
<Card>
<CardBody>
<CardTitle>
Welcome Back, Please Login.
</CardTitle>
</CardBody>
<CardBody>
<Validations @ref="@loginValidationsRef" Mode="ValidationMode.Manual" ValidateOnLoad="false">
<Validation Validator="ValidationRule.IsNotEmpty">
<Field>
<FieldLabel>User Name</FieldLabel>
<TextEdit @bind-Text="@userName" Placeholder="Enter user name...">
<Feedback>
<ValidationNone>Please Enter Your User Name. </ValidationNone>
<ValidationSuccess>User Name is good</ValidationSuccess>
<ValidationError>Please Enter A Valid User Name</ValidationError>
</Feedback>
</TextEdit>
</Field>
</Validation>
<Validation Validator="ValidationRule.IsNotEmpty">
<Field>
<FieldLabel>Password</FieldLabel>
<TextEdit @bind-Text="password" Role="TextRole.Password" Placeholder="Enter Password..." />
</Field>
</Validation>
<Field>
<Check TValue="bool" @bind-Checked="@rememberMe">Remember Me</Check>
</Field>
</Validations>
</CardBody>
<CardBody>
<Button Color="Color.Primary" Clicked="@OnLoginClicked">Login</Button>
<Button Color="Color.Secondary">Forgot Password</Button>
</CardBody>
</Card>
</TabPanel>
<TabPanel Name="register">
<Card>
<CardBody>
<CardTitle>
New Here? Create An Account
</CardTitle>
</CardBody>
<CardBody>
<Validations @ref="@registerValidationsRef" Mode="ValidationMode.Manual" ValidateOnLoad="false">
<Validation Validator="ValidationRule.IsNotEmpty">
<Field>
<FieldLabel>Name</FieldLabel>
<TextEdit @bind-Text="userName" Placeholder="Enter Your User Name" />
</Field>
</Validation>
<Validation Validator="ValidationRule.IsNotEmpty">
<Field>
<FieldLabel>Password</FieldLabel>
<TextEdit @bind-Text="password" Role="TextRole.Password" Placeholder="Enter Password" />
<FieldHelp>Password Strength: <Text TextColor="TextColor.Danger">Strong</Text></FieldHelp>
</Field>
</Validation>
</Validations>
</CardBody>
<CardBody>
<Button Color="Color.Primary" Clicked="@OnRegisterClicked">
Create Account
</Button>
</CardBody>
</Card>
</TabPanel>
</Content>
</Tabs>
</Column>
</Row>
</Container>
@code {
Validations loginValidationsRef = new();
Validations registerValidationsRef = new();
private RegisteredUser registeredUser = new();
private string selectedTab = "login";
private string userName = "";
private string password = "";
private bool rememberMe = false;
private Task OnSelectedTabChanged(string name)
{
selectedTab = name;
return Task.CompletedTask;
}
private async Task OnLoginClicked()
{
if (await loginValidationsRef.ValidateAll())
{
RegisteredUser registeredUser = new()
{
UserName = userName,
PasswordHash = password
};
bool IsValidated = await Task.Run(() => loginService.ValidateUserAsync(registeredUser));
await loginValidationsRef.ClearAll();
}
}
private async Task OnRegisterClicked()
{
if (await registerValidationsRef.ValidateAll())
{
registeredUser.UserName = userName;
registeredUser.PasswordHash = password;
_ = await Task.Run(() => loginService.InsertRegisteredUserAsync(registeredUser));
await registerValidationsRef.ClearAll();
}
}
}

@ -1,7 +1,8 @@
using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Services;
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
@ -12,6 +13,11 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<GremlinContext>();
builder.Services.AddScoped<AccountService>();
builder.Services.AddScoped<ContactService>();
builder.Services.AddScoped<CustomDescriptionService>();
builder.Services.AddScoped<LoginService>();
builder.Services.AddBlazorise(options => { options.Immediate = true; }).AddBootstrapProviders().AddFontAwesomeIcons();
builder.Services.AddOptions();
builder.Services.AddAuthorizationCore();
builder.WebHost.UseWebRoot("wwwroot");
builder.WebHost.UseStaticWebAssets();

@ -0,0 +1,56 @@
using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace Gremlin_BlazorServer.Services
{
public class CustomDescriptionService
{
private readonly GremlinContext gremlinContext;
public CustomDescriptionService(GremlinContext gC) => gremlinContext = gC;
public void ConfigureServices(IServiceCollection services)
{
_ = services.AddDbContext<GremlinContext>(ServiceLifetime.Scoped);
}
public async Task<List<CustomDescription>> GetAllCustomDescriptionsAsync() => await gremlinContext.CustomDescriptions.ToListAsync();
public async Task<bool> InsertCustomDescriptionAsync(CustomDescription CustomDescription)
{
try
{
_ = await gremlinContext.CustomDescriptions.AddAsync(CustomDescription);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
return false;
}
}
public async Task<CustomDescription?> GetCustomDescriptionAsync(uint CustomDescriptionId)
{
CustomDescription? CustomDescription = await gremlinContext.CustomDescriptions.FirstOrDefaultAsync(CustomDescription => CustomDescription.CustomDescriptionId.Equals(CustomDescriptionId));
return CustomDescription;
}
public async Task<bool> UpdateCustomDescriptionAsync(CustomDescription CustomDescription)
{
_ = gremlinContext.CustomDescriptions.Update(CustomDescription);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteCustomDescriptionAsync(CustomDescription CustomDescription)
{
_ = gremlinContext.Remove(CustomDescription);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
}
}

@ -0,0 +1,66 @@
using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace Gremlin_BlazorServer.Services
{
public class LoginService
{
private readonly GremlinContext gremlinContext;
public LoginService(GremlinContext gC) => gremlinContext = gC;
private Task<AuthenticationState> AuthenticationStateTask { get; set; } = default!;
public void ConfigureServices(IServiceCollection services)
{
_ = services.AddDbContext<GremlinContext>(ServiceLifetime.Scoped);
}
public async Task<List<RegisteredUser>> GetAllRegisteredUserAsync() => await gremlinContext.RegisteredUser.ToListAsync();
public async Task<bool> InsertRegisteredUserAsync(RegisteredUser registeredUser)
{
try
{
_ = await gremlinContext.RegisteredUser.AddAsync(registeredUser);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
return false;
}
}
public async Task<RegisteredUser?> GetRegisteredUserAsync(string registeredUserName)
{
return await gremlinContext.RegisteredUser.FirstOrDefaultAsync(rU => rU.UserName.Equals(registeredUserName));
}
public async Task<bool> UpdateRegisteredUserAsync(RegisteredUser registeredUser)
{
_ = gremlinContext.RegisteredUser.Update(registeredUser);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteRegisteredUserAsync(RegisteredUser registeredUser)
{
_ = gremlinContext.Remove(registeredUser);
_ = await gremlinContext.SaveChangesAsync();
return true;
}
public async Task<bool> ValidateUserAsync(RegisteredUser registeredUser)
{
_ = await Task.Run(() => GetRegisteredUserAsync(registeredUser.UserName));
AuthenticationState authState = await AuthenticationStateTask;
return authState.User.Identity.IsAuthenticated;
}
}
}

@ -14,6 +14,11 @@
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-4">
<NavLink class="nav-link" href="login">
<span class="oi oi-list-rich" aria-hidden="true"></span>Login
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="Contacts/Index">
<span class="oi oi-list-rich" aria-hidden="true"></span> Contacts
@ -24,6 +29,11 @@
<span class="oi oi-list-rich" aria-hidden="true"></span> Accounts
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="CustomDescriptions/Index">
<span class="oi oi-list-rich" aria-hidden="true"></span> CustomDescriptions
</NavLink>
</div>
</nav>
</div>

@ -8,3 +8,4 @@
@using Microsoft.JSInterop
@using Gremlin_BlazorServer
@using Gremlin_BlazorServer.Shared
@using Blazorise