Skip to content

Commit

Permalink
aggiunte crud con db mysql in cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
umbertocicciaa committed Sep 15, 2024
1 parent ac3b3c6 commit 7bbb350
Show file tree
Hide file tree
Showing 16 changed files with 574 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ jobs:

- name: Build with dotnet
run: dotnet build --configuration Release


- name: Replace DB password in appsettings.json
run: sed -i 's/{DB_PASSWORD}/${{ secrets.DB_PASSWORD }}/g' ./CloudProject/appsettings.json

- name: dotnet publish
run: dotnet publish ./CloudProject/CloudProject.csproj -c Release -f net8.0 -o ${{github.workspace}}/myapp

Expand Down
4 changes: 4 additions & 0 deletions CloudProject.Tests/CloudProject.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
<ItemGroup>
<ProjectReference Include="..\CloudProject\CloudProject.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions CloudProject/CloudProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.5.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions CloudProject/Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@

<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
<nav class="flex-column">

<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="exam">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Exam
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
Expand All @@ -25,5 +32,7 @@
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
</NavLink>
</div>


</nav>
</div>
89 changes: 89 additions & 0 deletions CloudProject/Components/Pages/Exam.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

@page "/exam"
@rendermode InteractiveServer
@using CloudProject.Services

<h3>Exam Management</h3>


@if (_isLoading)
{
<div>Loading...</div>
}
else
{

<div>
<h4>Add/Edit Exam</h4>
<input @bind="_currentExam.CourseName" placeholder="Name"/>
<input type="date" @bind="_currentExam.ExamDate"/>
<button @onclick="@SaveExam">Save</button>
</div>

<div>
<h4>Exams</h4>
<table>
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var exam in _exams)
{
<tr>
<td>@exam.CourseName</td>
<td>@exam.ExamDate.ToString()</td>
<td>
<button @onclick="() => EditExam(exam)">Edit</button>
<button @onclick="() => DeleteExam(exam.ExamId)">Delete</button>
</td>
</tr>
}
</tbody>
</table>
</div>
}

@code {
[Inject]
private IExamService ExamService { get; set; } = null!;
private bool _isLoading = true;
private IEnumerable<Models.Exam> _exams = new List<Models.Exam>();
private Models.Exam _currentExam = new Models.Exam();

protected override async Task OnInitializedAsync()
{
_exams = await ExamService.GetAllExamsAsync();
_isLoading = false;
}

private async Task SaveExam()
{
if (_currentExam.ExamId == 0)
await ExamService.CreateExamAsync(_currentExam);
else
await ExamService.UpdateExamAsync(_currentExam);

_currentExam = new Models.Exam();
_exams = await ExamService.GetAllExamsAsync();
}

private void EditExam(Models.Exam exam)
{
_currentExam = new Models.Exam
{
ExamId = exam.ExamId,
CourseName = exam.CourseName,
ExamDate = exam.ExamDate
};
}

private async Task DeleteExam(int id)
{
await ExamService.DeleteExamAsync(id);
_exams = await ExamService.GetAllExamsAsync();
}
}
14 changes: 14 additions & 0 deletions CloudProject/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CloudProject.Models;
using Microsoft.EntityFrameworkCore;

namespace CloudProject.Data;

public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public DbSet<Exam> Exams { get; set; }
}
66 changes: 66 additions & 0 deletions CloudProject/Migrations/20240915130130_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions CloudProject/Migrations/20240915130130_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CloudProject.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");

migrationBuilder.CreateTable(
name: "Exams",
columns: table => new
{
ExamId = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
CourseName = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
ExamDate = table.Column<DateTime>(type: "datetime(6)", nullable: false),
Location = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
DurationMinutes = table.Column<int>(type: "int", nullable: false),
Instructor = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_Exams", x => x.ExamId);
})
.Annotation("MySql:CharSet", "utf8mb4");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Exams");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7bbb350

Please sign in to comment.