Skip to content

Commit

Permalink
complete ef
Browse files Browse the repository at this point in the history
  • Loading branch information
sthsuyash committed May 29, 2024
1 parent e9e4ba2 commit 2166b9a
Show file tree
Hide file tree
Showing 28 changed files with 1,229 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Entity_Framework_CRUD.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace Entity_Framework_CRUD.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using Microsoft.AspNetCore.Mvc;
using Entity_Framework_CRUD.Models;

namespace Entity_Framework_CRUD.Controllers
{
public class OfficerController : Controller
{
// setting up the context class on controller
// context class is the one that will be used to interact with the database
private readonly OfficerContext _context;

// constructor to initialize the context class
public OfficerController(OfficerContext context)
{
_context = context;
}

/*
* Read Officer method
*/
[HttpGet]
public IActionResult Index()
{
// data to be displayed in index.cshtml
// data from the database will be converted to list and sent to view
var officerList = _context.tbl_officer.ToList();
return View(officerList);
}

/*
* Create Officer method
* Get and Post methods
*/
[HttpGet]
public IActionResult CreateOfficer()
{
return View();
}

[HttpPost]
public IActionResult CreateOfficer(Officer o)
{
var officer = new Officer()
{
Id = Guid.NewGuid(),
Name = o.Name,
Gender = o.Gender,
Department = o.Department,
Phone = o.Phone,
Position = o.Position
};
_context.tbl_officer.Add(officer);
_context.SaveChanges();
return RedirectToAction("Index");
}

/*
* Details Officer method
* Get method
*/
[HttpGet]
public IActionResult GetOfficer(Guid id)
{
var officer = _context.tbl_officer.Find(id);
if (officer == null)
{
return RedirectToAction("Index");
}
return View(officer);
}

/*
* Update Officer method
* Get and Post methods
*/

/*
* Get method to get the officer details to be updated
*/
[HttpGet]
public IActionResult UpdateOfficer(Guid id)
{
// check if the id of officer sent from view is matched with the id in the database
// if matched, the officer details will be sent to the view
var officer = _context.tbl_officer.FirstOrDefault(x => x.Id == id);
if (officer == null)
{
return RedirectToAction("Index");
}
return View(officer);
}

/*
* Post method to update the officer details
*/
[HttpPost]
public IActionResult UpdateOfficer(Officer o)
{
// check if the officer details are valid
// then save the changes in database
var officer = _context.tbl_officer.Find(o.Id);
if (officer != null)
{
officer.Name = o.Name;
officer.Gender = o.Gender;
officer.Department = o.Department;
officer.Phone = o.Phone;
officer.Position = o.Position;
_context.SaveChanges();
}
return RedirectToAction("Index");
}

/*
* Delete Officer method
* Get and Post methods
*/

/*
* Get method to get the officer details to be deleted
*/
[HttpGet]
public IActionResult DeleteOfficer(Guid id)
{
// check if the id of officer sent from view is matched with the id in the database
// if matched, the officer details will be sent to the view
var officer = _context.tbl_officer.FirstOrDefault(x => x.Id == id);
if (officer == null)
{
return RedirectToAction("Index");
}
return View(officer);
}

/*
* Post method to delete the officer details
*/
[HttpPost]
public IActionResult DeleteOfficer(Officer o)
{
// check if the officer details are valid
// then delete the officer from the database
var officer = _context.tbl_officer.Find(o.Id);
if (officer != null)
{
_context.tbl_officer.Remove(officer);
_context.SaveChanges();
}
return RedirectToAction("Index");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Entity_Framework_CRUD</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entity Framework CRUD", "Entity Framework CRUD.csproj", "{E17321B0-0B28-4866-A72B-D5DD32A016E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E17321B0-0B28-4866-A72B-D5DD32A016E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E17321B0-0B28-4866-A72B-D5DD32A016E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E17321B0-0B28-4866-A72B-D5DD32A016E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E17321B0-0B28-4866-A72B-D5DD32A016E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4D27A5C2-A74B-42D6-A7B1-D28D7035010D}
EndGlobalSection
EndGlobal

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Entity_Framework_CRUD.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "tbl_officer",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Gender = table.Column<string>(type: "nvarchar(max)", nullable: false),
Phone = table.Column<string>(type: "nvarchar(max)", nullable: false),
Department = table.Column<string>(type: "nvarchar(max)", nullable: false),
Position = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_tbl_officer", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "tbl_officer");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <auto-generated />
using System;
using Entity_Framework_CRUD.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace Entity_Framework_CRUD.Migrations
{
[DbContext(typeof(OfficerContext))]
partial class OfficerContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Entity_Framework_CRUD.Models.Officer", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<string>("Department")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Gender")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Phone")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("Position")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("tbl_officer");
});
#pragma warning restore 612, 618
}
}
}
Loading

0 comments on commit 2166b9a

Please sign in to comment.