Skip to content

Commit 3341b6c

Browse files
authored
Merge pull request #5 from SensitTechnologies/work-stations-crud
Adding a service to manage work stations and Blazor pages to view, edit, and create them.
2 parents a13052c + 85edd5d commit 3341b6c

File tree

6 files changed

+335
-0
lines changed

6 files changed

+335
-0
lines changed

MESS/MESS.Blazor/Components/Layout/NavMenu.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
</NavLink>
3333
</div>
3434

35+
<div class="nav-item px-3">
36+
<NavLink class="nav-link" href="work-stations" Match="NavLinkMatch.All">
37+
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Work Stations
38+
</NavLink>
39+
</div>
40+
3541
<div class="nav-item px-3">
3642
<NavLink class="nav-link" href="counter">
3743
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
@page "/work-stations/create"
2+
@page "/work-stations/edit/{WorkStationId:int}"
3+
@using MESS.Data.Models
4+
@using MESS.Services.WorkStation
5+
@using Serilog
6+
@inject IWorkStationService WorkStationService
7+
@inject NavigationManager NavigationManager
8+
9+
<PageTitle>@(IsEditMode ? "Edit Work Station" : "Create Work Station")</PageTitle>
10+
11+
<h3>@(IsEditMode ? "Edit" : "Create")</h3>
12+
13+
<h2>Work Station</h2>
14+
15+
<hr />
16+
<div class="row">
17+
<div class="col-md-4">
18+
<EditForm Model="WorkStation" OnValidSubmit="SaveWorkStation">
19+
<DataAnnotationsValidator />
20+
<ValidationSummary class="text-danger" role="alert"/>
21+
22+
<div class="mb-3">
23+
<label for="name" class="form-label">Name:</label>
24+
<InputText id="name" @bind-Value="WorkStation.Name" class="form-control" />
25+
<ValidationMessage For="() => WorkStation.Name" class="text-danger" />
26+
</div>
27+
<div class="mb-3 form-check">
28+
<InputCheckbox id="isActive" @bind-Value="WorkStation.IsActive" class="form-check-input" />
29+
<label for="isActive" class="form-check-label">Active</label>
30+
</div>
31+
32+
<button type="submit" class="btn btn-primary">@(IsEditMode ? "Save Changes" : "Create")</button>
33+
</EditForm>
34+
</div>
35+
</div>
36+
37+
<div>
38+
<a href="/work-stations">Back to List</a>
39+
</div>
40+
41+
@code {
42+
[Parameter]
43+
public int? WorkStationId { get; set; }
44+
45+
private bool IsEditMode => WorkStationId.HasValue;
46+
private WorkStation WorkStation { get; set; } = new()
47+
{
48+
Name = ""
49+
};
50+
51+
protected override async Task OnInitializedAsync()
52+
{
53+
if (IsEditMode)
54+
{
55+
// Fetch work station for editing
56+
if (WorkStationId != null)
57+
{
58+
var existingWorkStation = await WorkStationService.FindWorkStationByIdAsync(WorkStationId.Value);
59+
if (existingWorkStation != null)
60+
{
61+
WorkStation = existingWorkStation;
62+
}
63+
else
64+
{
65+
// Handle case where work station does not exist (e.g., navigate back)
66+
NavigationManager.NavigateTo("/work-stations");
67+
}
68+
}
69+
}
70+
}
71+
72+
private async Task SaveWorkStation()
73+
{
74+
try
75+
{
76+
if (IsEditMode)
77+
{
78+
await WorkStationService.ModifyWorkStationAsync(WorkStation);
79+
}
80+
else
81+
{
82+
await WorkStationService.AddWorkStationAsync(WorkStation);
83+
}
84+
85+
NavigationManager.NavigateTo("/work-stations");
86+
}
87+
catch (Exception ex)
88+
{
89+
Log.Error(ex, "Error saving work station.");
90+
}
91+
}
92+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
@page "/work-stations"
2+
@using MESS.Data.Models
3+
@using MESS.Services.WorkStation
4+
@inject IWorkStationService WorkStationService
5+
@inject NavigationManager NavigationManager
6+
7+
<PageTitle>Work Station List</PageTitle>
8+
9+
<h3>Work Station List</h3>
10+
11+
@if (WorkStations.Count == 0)
12+
{
13+
<p>No work stations currently available.</p>
14+
}
15+
else
16+
{
17+
<table class="table table-striped">
18+
<thead>
19+
<tr>
20+
<th>ID</th>
21+
<th>Name</th>
22+
<th>Active</th>
23+
<th>Actions</th>
24+
</tr>
25+
</thead>
26+
<tbody>
27+
@foreach (var workStation in WorkStations)
28+
{
29+
<tr>
30+
<td>@workStation.Id</td>
31+
<td>@workStation.Name</td>
32+
<td>@(workStation.IsActive ? "Yes" : "No")</td>
33+
<td>
34+
<button class="btn btn-primary btn-sm" @onclick="() => EditWorkStation(workStation.Id)">Edit</button>
35+
<button class="btn btn-danger btn-sm" @onclick="() => RemoveWorkStation(workStation.Id)">Delete</button>
36+
</td>
37+
</tr>
38+
<tr>
39+
<td colspan="5">
40+
<details>
41+
<summary>View Products</summary>
42+
<div class="p-3">
43+
<h5>Products</h5>
44+
@if (workStation.Products.Any())
45+
{
46+
<ul>
47+
@foreach (var product in workStation.Products)
48+
{
49+
<li>@product.Name</li>
50+
}
51+
</ul>
52+
}
53+
else
54+
{
55+
<p>No products available.</p>
56+
}
57+
</div>
58+
</details>
59+
</td>
60+
</tr>
61+
}
62+
</tbody>
63+
</table>
64+
}
65+
66+
<button class="btn btn-success" @onclick="GoToCreate">Create New Work Station</button>
67+
68+
@code {
69+
private List<WorkStation> WorkStations { get; set; } = new();
70+
71+
protected override async Task OnInitializedAsync()
72+
{
73+
await LoadWorkStations();
74+
}
75+
76+
private async Task LoadWorkStations()
77+
{
78+
WorkStations = (await WorkStationService.GetAllWorkStationsAsync()).ToList();
79+
}
80+
81+
private void GoToCreate()
82+
{
83+
NavigationManager.NavigateTo("/work-stations/create");
84+
}
85+
86+
private void EditWorkStation(int id)
87+
{
88+
NavigationManager.NavigateTo($"/work-stations/edit/{id}");
89+
}
90+
91+
private async Task RemoveWorkStation(int id)
92+
{
93+
await WorkStationService.RemoveWorkStationAsync(id);
94+
await LoadWorkStations(); // Refresh the list after deletion
95+
}
96+
}

MESS/MESS.Blazor/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MESS.Data.Seed;
55
using MESS.Services.ProductionLog;
66
using MESS.Services.WorkInstruction;
7+
using MESS.Services.WorkStation;
78
using Microsoft.EntityFrameworkCore;
89
using Serilog;
910
using Serilog.Events;
@@ -29,6 +30,10 @@
2930
// Add services to the container.
3031
builder.Services.AddRazorComponents()
3132
.AddInteractiveServerComponents();
33+
builder.Services.AddTransient<IWorkInstructionService, WorkInstructionService>();
34+
builder.Services.AddTransient<IProductionLogService, ProductionLogService>();
35+
builder.Services.AddHttpClient();
36+
builder.Services.AddTransient<IWorkStationService, WorkStationService>();
3237

3338
var logLevel = builder.Environment.IsDevelopment() ? LogEventLevel.Debug : LogEventLevel.Warning;
3439

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace MESS.Services.WorkStation;
2+
3+
using MESS.Data.Models;
4+
5+
public interface IWorkStationService
6+
{
7+
/// <summary>
8+
/// Adds a new Work Station to the database.
9+
/// </summary>
10+
/// <param name="workStation">The product to add.</param>
11+
/// <returns>A task representing the asynchronous operation.</returns>
12+
Task AddWorkStationAsync(Data.Models.WorkStation workStation);
13+
14+
/// <summary>
15+
/// Finds a WorkStation by its unique ID, including related Products.
16+
/// </summary>
17+
/// <param name="id">The ID of the WorkStation to find.</param>
18+
/// <returns>The WorkStation if found; otherwise, null.</returns>
19+
Task<WorkStation?> FindWorkStationByIdAsync(int id);
20+
21+
/// <summary>
22+
/// Retrieves all WorkStations from the database, including related Products.
23+
/// </summary>
24+
/// <returns>A list of all products.</returns>
25+
Task<IEnumerable<WorkStation>> GetAllWorkStationsAsync();
26+
27+
/// <summary>
28+
/// Updates an existing WorkStation in the database.
29+
/// </summary>
30+
/// <param name="workStation">The WorkStation to update.</param>
31+
/// <returns>A task representing the asynchronous operation.</returns>
32+
Task ModifyWorkStationAsync(WorkStation workStation);
33+
34+
/// <summary>
35+
/// Removes a WorkStation from the database by its unique ID.
36+
/// </summary>
37+
/// <param name="id">The ID of the WorkStation to remove.</param>
38+
/// <returns>A task representing the asynchronous operation.</returns>
39+
Task RemoveWorkStationAsync(int id);
40+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using MESS.Data.Context;
2+
using Microsoft.EntityFrameworkCore;
3+
using Serilog;
4+
5+
namespace MESS.Services.WorkStation;
6+
7+
using Data.Models;
8+
9+
public class WorkStationService : IWorkStationService
10+
{
11+
private readonly ApplicationContext _context;
12+
13+
public WorkStationService(ApplicationContext context)
14+
{
15+
_context = context;
16+
}
17+
18+
public async Task AddWorkStationAsync(WorkStation workStation)
19+
{
20+
try
21+
{
22+
await _context.WorkStations.AddAsync(workStation);
23+
await _context.SaveChangesAsync();
24+
Log.Information("Work Station successfully created. ID: {WorkStationId}", workStation.Id);
25+
}
26+
catch (Exception e)
27+
{
28+
Log.Error(e, "An error occured while adding WorkStation");
29+
}
30+
31+
}
32+
33+
public async Task<WorkStation?> FindWorkStationByIdAsync(int id)
34+
{
35+
try
36+
{
37+
var workStation = await _context.WorkStations
38+
.Include(p => p.Products)
39+
.FirstOrDefaultAsync(p => p.Id == id);
40+
41+
Log.Information("Work Station Successfully Found. ID: {WorkStationId}", workStation?.Id);
42+
43+
return workStation;
44+
}
45+
catch (Exception e)
46+
{
47+
Log.Warning(e, "Unable to find work station for ID. ID: {InputId}", id);
48+
return null;
49+
}
50+
}
51+
52+
public async Task<IEnumerable<WorkStation>> GetAllWorkStationsAsync()
53+
{
54+
try
55+
{
56+
return await _context.WorkStations
57+
.Include(p => p.Products)
58+
.ToListAsync();
59+
}
60+
catch(Exception e)
61+
{
62+
Log.Warning(e, "Exception occured while getting all work stations. \n" +
63+
"Returning empty work station list.");
64+
return new List<WorkStation>();
65+
}
66+
}
67+
68+
public async Task ModifyWorkStationAsync(WorkStation workStation)
69+
{
70+
try
71+
{
72+
_context.WorkStations.Update(workStation);
73+
await _context.SaveChangesAsync();
74+
}
75+
catch (Exception e)
76+
{
77+
Log.Error(e, "Exception occured while modifying a work station. ID: {InputId}",
78+
workStation.Id);
79+
}
80+
}
81+
82+
public async Task RemoveWorkStationAsync(int id)
83+
{
84+
var workStation = await _context.WorkStations.FindAsync(id);
85+
if (workStation != null)
86+
{
87+
_context.WorkStations.Remove(workStation);
88+
await _context.SaveChangesAsync();
89+
Log.Information("Work station successfully removed. ID: {WorkStationId}", workStation.Id);
90+
}
91+
else
92+
{
93+
Log.Warning("Work station for removal not found. ID: {InputID}", id);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)