-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: msivasubramaniaan <[email protected]>
- Loading branch information
1 parent
e851f53
commit b795458
Showing
19 changed files
with
545 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace RazorPagesContacts.Data; | ||
|
||
public class AppConfiguration | ||
{ | ||
public string DatabaseProvider { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace RazorPagesContacts.Data; | ||
|
||
public class AppDbContext : DbContext | ||
{ | ||
public AppDbContext(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Customer> Customers { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace RazorPagesContacts.Data; | ||
|
||
public class Customer | ||
{ | ||
public int Id { get; set; } | ||
|
||
[Required, StringLength(100)] | ||
public string Name { get; set; } | ||
} |
42 changes: 42 additions & 0 deletions
42
resources/projects/dotnet8.0/app/Migrations/20191107110533_Initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
resources/projects/dotnet8.0/app/Migrations/20191107110533_Initial.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
|
||
namespace RazorPagesContacts.Migrations | ||
{ | ||
public partial class Initial : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Customers", | ||
columns: table => new | ||
{ | ||
Id = table.Column<int>(nullable: false) | ||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), | ||
Name = table.Column<string>(maxLength: 100, nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Customers", x => x.Id); | ||
}); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Customers"); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
resources/projects/dotnet8.0/app/Migrations/AppDbContextModelSnapshot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// <auto-generated /> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
using RazorPagesContacts.Data; | ||
|
||
namespace RazorPagesContacts.Migrations | ||
{ | ||
[DbContext(typeof(AppDbContext))] | ||
partial class AppDbContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) | ||
.HasAnnotation("ProductVersion", "3.0.0") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 63); | ||
|
||
modelBuilder.Entity("RazorPagesContacts.Data.Customer", b => | ||
{ | ||
b.Property<int>("Id") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("integer") | ||
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); | ||
b.Property<string>("Name") | ||
.IsRequired() | ||
.HasColumnType("character varying(100)") | ||
.HasMaxLength(100); | ||
b.HasKey("Id"); | ||
b.ToTable("Customers"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@page | ||
@model RazorPagesContacts.Pages.CreateModel | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers | ||
|
||
<html> | ||
<body> | ||
<p> | ||
Enter your name. | ||
</p> | ||
<div asp-validation-summary="All"></div> | ||
<form method="POST"> | ||
<div>Name: <input asp-for="Customer.Name" /></div> | ||
<input type="submit" /> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using RazorPagesContacts.Data; | ||
|
||
namespace RazorPagesContacts.Pages; | ||
|
||
public class CreateModel : PageModel | ||
{ | ||
private readonly AppDbContext _db; | ||
|
||
public CreateModel(AppDbContext db) | ||
{ | ||
_db = db; | ||
} | ||
|
||
[BindProperty] | ||
public Customer Customer { get; set; } | ||
|
||
public async Task<IActionResult> OnPostAsync() | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return Page(); | ||
} | ||
|
||
_db.Customers.Add(Customer); | ||
await _db.SaveChangesAsync(); | ||
return RedirectToPage("/Index"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@page "{id:int}" | ||
@model RazorPagesContacts.Pages.EditModel | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers | ||
|
||
@{ | ||
ViewData["Title"] = "Edit Customer"; | ||
} | ||
|
||
<h1>Edit Customer - @Model.Customer.Id</h1> | ||
<form method="post"> | ||
<div asp-validation-summary="All"></div> | ||
<input asp-for="Customer.Id" type="hidden" /> | ||
<div> | ||
<label asp-for="Customer.Name"></label> | ||
<div> | ||
<input asp-for="Customer.Name" /> | ||
<span asp-validation-for="Customer.Name" ></span> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<button type="submit">Save</button> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.EntityFrameworkCore; | ||
using RazorPagesContacts.Data; | ||
|
||
namespace RazorPagesContacts.Pages; | ||
|
||
public class EditModel : PageModel | ||
{ | ||
private readonly AppDbContext _db; | ||
|
||
public EditModel(AppDbContext db) | ||
{ | ||
_db = db; | ||
} | ||
|
||
[BindProperty] | ||
public Customer Customer { get; set; } | ||
|
||
public async Task<IActionResult> OnGetAsync(int id) | ||
{ | ||
Customer = await _db.Customers.FindAsync(id); | ||
|
||
if (Customer == null) | ||
{ | ||
return RedirectToPage("/Index"); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
public async Task<IActionResult> OnPostAsync() | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return Page(); | ||
} | ||
|
||
_db.Attach(Customer).State = EntityState.Modified; | ||
|
||
try | ||
{ | ||
await _db.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
throw new Exception($"Customer {Customer.Id} not found!"); | ||
} | ||
|
||
return RedirectToPage("/Index"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@page | ||
@model RazorPagesContacts.Pages.IndexModel | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers | ||
|
||
<h1>Contacts from @Model.AppConfiguration.DatabaseProvider database</h1> | ||
<form method="post"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Name</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@foreach (var contact in Model.Customers) | ||
{ | ||
<tr> | ||
<td>@contact.Id</td> | ||
<td>@contact.Name</td> | ||
<td> | ||
<a asp-page="./Edit" asp-route-id="@contact.Id">edit</a> | ||
<button type="submit" asp-page-handler="delete" | ||
asp-route-id="@contact.Id">delete</button> | ||
</td> | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
|
||
<a asp-page="./Create">Create</a> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using RazorPagesContacts.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace RazorPagesContacts.Pages; | ||
|
||
public class IndexModel : PageModel | ||
{ | ||
private readonly AppDbContext _db; | ||
|
||
public IndexModel(AppDbContext db, AppConfiguration appConfig) | ||
{ | ||
_db = db; | ||
AppConfiguration = appConfig; | ||
} | ||
|
||
public IList<Customer> Customers { get; private set; } | ||
|
||
public AppConfiguration AppConfiguration { get; } | ||
|
||
public async Task OnGetAsync() | ||
{ | ||
Customers = await _db.Customers.AsNoTracking().ToListAsync(); | ||
} | ||
|
||
public async Task<IActionResult> OnPostDeleteAsync(int id) | ||
{ | ||
var contact = await _db.Customers.FindAsync(id); | ||
|
||
if (contact != null) | ||
{ | ||
_db.Customers.Remove(contact); | ||
await _db.SaveChangesAsync(); | ||
} | ||
|
||
return RedirectToPage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using RazorPagesContacts; | ||
|
||
var builder = Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.Run(); |
Oops, something went wrong.