Skip to content

Commit

Permalink
BlazorServerCourse
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanjofigue97 committed Apr 15, 2024
1 parent e44c848 commit d1d7151
Show file tree
Hide file tree
Showing 43 changed files with 1,645 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace BlazorApp.Models;

public class SearchModel
{
[Required]
public string SearchTerm { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,83 @@ else
<th>First Name</th>
<th>Last Name</th>
<th>Date Of Birth</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var p in people)
{
<tr>
<th>@p.Id</th>
<th>@p.FirstName</th>
<th>@p.LastName</th>
<th>@p.DateOfBirth</th>
<td>@p.Id</td>
<td>@p.FirstName</td>
<td>@p.LastName</td>
<td>@p.DateOfBirth</td>
<td>
<button class="btn btn-primary" @onclick="(() =>
UpdatePerson(p.Id))">
Update
</button>

@if (idToDelete == p.Id)
{
<button class="btn btn-danger mx-2" @onclick="(() => DeletePerson(p.Id))">
Confirm Delete
</button>
}else
{
<button class="btn btn-danger mx-2" @onclick="(() => idToDelete = p.Id)">
Delete
</button>
}
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="col-md-4">
<UpdatePerson Id="1"></UpdatePerson>
</div>
@if (showEditform)
{
<div class="col-md-4">
<UpdatePerson Id="@idToUpdate" OnUpdate="HandleOnUpdate"></UpdatePerson>
</div>

}
</div>
}
}


@code {
private List<IPersonModel> people;
private bool showEditform = false;
private int idToUpdate = 0;
private int idToDelete = 0;

private void HandleOnUpdate(IPersonModel person)
{
showEditform = false;
var p = people.Where(x => x.Id == person.Id).FirstOrDefault();

if (p != null)
{
p.FirstName = person.FirstName;
p.LastName = person.LastName;
p.DateOfBirth = person.DateOfBirth;
}

}

private async Task DeletePerson(int id)
{
personData.DeletePerson(id);
people.Remove(people.Where(x => x.Id == id).FirstOrDefault());
idToDelete = 0;
}

private void UpdatePerson(int id)
{
idToUpdate = id;
showEditform = true;
}

protected override async Task OnParametersSetAsync()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@page "/search"
@inject IPersonDataService personData

<h3>Search Page</h3>
<EditForm Model="@search" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="form-group">
<label>Search</label>
<InputText @bind-Value="search.SearchTerm"/>
</div>
<button class="btn btn-outline-primary" type="submit"> Go</button>
</EditForm>

@if (people is null)
{
<h2>Loading...</h2>
}
else
{
<div class="row">
<div class="col-md-8">
<table class="table table-striped">
<thead class="table-dark">
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date Of Birth</th>
</tr>
</thead>
<tbody>
@foreach (var p in people)
{
<tr>
<td>@p.Id</td>
<td>@p.FirstName</td>
<td>@p.LastName</td>
<td>@p.DateOfBirth</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}

@code {
private SearchModel search = new SearchModel();
private List<IPersonModel> people;

private async Task HandleValidSubmit()
{
people = await personData.SearchPeople(search.SearchTerm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<span class="oi oi-book" aria-hidden="true"></span> Read Peolpe
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="search">
<span class="oi oi-magnifying-glass" aria-hidden="true"></span> Search
</NavLink>
</div>
</nav>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ public interface IPersonDataService
{
Task CreatePerson(IPersonModel person);
void CreatePersonD(IPersonModel person);
Task DeletePerson(int id);
Task<List<IPersonModel>> ReadPeople();
Task<IPersonModel> ReadPeople(int id);
Task<List<IPersonModel>> SearchPeople(string searchTerm);
Task UpdatePerson(IPersonModel person);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using SupportLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace SupportLibrary.Data
{
public class PersonDummyDataService : IPersonDataService
{
private List<IPersonModel> people = new List<IPersonModel>();
private int currentId = 0;

public Task CreatePerson(IPersonModel person)
{
return Task.Run(() =>
{
currentId += 1;
person.Id = currentId;
people.Add(person);
});
}

public void CreatePersonD(IPersonModel person)
{
currentId += 1;
person.Id = currentId;
people.Add(person);
}

public Task DeletePerson(int id)
{
return Task.Run(() =>
{
people.Remove(people.Where(p => p.Id == id).FirstOrDefault());
});
}

public Task<List<IPersonModel>> ReadPeople()
{
return Task.FromResult(people);
}

public Task<IPersonModel> ReadPeople(int id)
{
return Task.FromResult(people.Where(p => p.Id == id).FirstOrDefault());
}

public Task<List<IPersonModel>> SearchPeople(string searchTerm)
{
return Task.FromResult(people.Where(x => x.FirstName.Contains(searchTerm) || x.LastName.Contains(searchTerm)).ToList());
}

public Task UpdatePerson(IPersonModel person)
{
return Task.Run(() =>
{
var p = people.Where(p => p.Id == person.Id).FirstOrDefault();
if (p != null)
{
p.FirstName = person.FirstName;
p.LastName = person.LastName;
p.DateOfBirth = person.DateOfBirth;
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,15 @@ public async Task UpdatePerson(IPersonModel person)
{
await _dataAccess.SaveData("spPeople_Update", person, "SQLDB");
}
public async Task DeletePerson(int id)
{
await _dataAccess.SaveData("spPeople_Delete", new { Id = id}, "SQLDB");
}
public async Task<List<IPersonModel>> SearchPeople(string searchTerm )
{
var peolpe = await _dataAccess.LoadData<PersonModel, dynamic>("spPeople_Search", new { SearchTerm = searchTerm }, "SQLDB");

return peolpe.ToList<IPersonModel>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BlazorApp.Data
{
public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BlazorApp.Data
{
public class WeatherForecastService
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BlazorApp.Models;

public class GuessModel
{
public string Name { get; set; }
public int DeploymentAttemps { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/counter"

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using BlazorApp.Models
<h3>Demo</h3>

<EditForm Model="@guest" OnInvalidSubmit="HandleValidSubmit" >
<InputText @bind-Value="guest.Name"/>
<InputText @bind-Value="guest.DeploymentAttemps" />
<button class="btn btn-primary" type="submit">Submit</button>
</EditForm>



@code {
private GuessModel guest = new();

private void HandleValidSubmit()
{

}
}
Loading

0 comments on commit d1d7151

Please sign in to comment.