Skip to content

Commit

Permalink
Added Employee Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
procodeguide committed Jun 25, 2020
1 parent a532f2b commit 9e9ceb4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 31 deletions.
34 changes: 12 additions & 22 deletions ProCodeGuide.Samples.EFCore/Controllers/EmployeeController.cs
Expand Up @@ -7,68 +7,58 @@
using Microsoft.EntityFrameworkCore;
using ProCodeGuide.Samples.EFCore.DbContexts;
using ProCodeGuide.Samples.EFCore.Model;
using ProCodeGuide.Samples.EFCore.Repository;

namespace ProCodeGuide.Samples.EFCore.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private IApplicationDbContext _dbcontext;
public EmployeeController(IApplicationDbContext dbcontext)
private IEmployeeRepository _employeerepository;
public EmployeeController(IEmployeeRepository employeerepository)
{
_dbcontext = dbcontext;
_employeerepository = employeerepository;
}

[HttpPost]
[Route("Create")]
public async Task<ActionResult> create([FromBody] Employee employee)
{
_dbcontext.Employees.Add(employee);
await _dbcontext.SaveChanges();
return Ok(employee.Id);
int empid = await _employeerepository.Create(employee);
return Ok(empid);
}

[HttpGet]
[Route("GetAll")]
public async Task<ActionResult> GetAll()
{
var employees = await _dbcontext.Employees.ToListAsync<Employee>();
var employees = await _employeerepository.GetAll();
return Ok(employees);
}

[HttpGet]
[Route("GetById/{id}")]
public async Task<ActionResult> GetById(int id)
{
var employee = await _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefaultAsync();
var employee = await _employeerepository.GetById(id);
return Ok(employee);
}

[HttpPut]
[Route("Update/{id}")]
public async Task<IActionResult> Update(int id, Employee employee)
{
var employeeupt = await _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefaultAsync();
if (employeeupt == null) return Ok("Employee does not exists");

employeeupt.Designation = employee.Designation;
employeeupt.Salary = employee.Salary;

await _dbcontext.SaveChanges();
return Ok("Employee details successfully modified");
string resp = await _employeerepository.Update(id, employee);
return Ok(resp);
}

[HttpDelete]
[Route("Delete/{id}")]
public async Task<IActionResult> Delete(int id)
{
var employeedel = await _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefaultAsync();
if (employeedel == null) return Ok("Employee does not exists");

_dbcontext.Employees.Remove(employeedel);
await _dbcontext.SaveChanges();
return Ok("Employee details deleted modified");
var resp = await _employeerepository.Delete(id);
return Ok(resp);
}
}
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
Expand Down
14 changes: 6 additions & 8 deletions ProCodeGuide.Samples.EFCore/Properties/launchSettings.json
@@ -1,5 +1,4 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
Expand All @@ -8,23 +7,22 @@
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"commandName": "Project",
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ProCodeGuide.Samples.EFCore": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "http://localhost:5000"
}
}
}
}
60 changes: 60 additions & 0 deletions ProCodeGuide.Samples.EFCore/Repository/EmployeeRepository.cs
@@ -0,0 +1,60 @@
using Microsoft.EntityFrameworkCore;
using ProCodeGuide.Samples.EFCore.DbContexts;
using ProCodeGuide.Samples.EFCore.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProCodeGuide.Samples.EFCore.Repository
{
public class EmployeeRepository : IEmployeeRepository
{
private IApplicationDbContext _dbcontext;
public EmployeeRepository(IApplicationDbContext dbcontext)
{
_dbcontext = dbcontext;
}

public async Task<int> Create(Employee employee)
{
_dbcontext.Employees.Add(employee);
await _dbcontext.SaveChanges();
return employee.Id;
}

public async Task<List<Employee>> GetAll()
{
var employees = await _dbcontext.Employees.ToListAsync<Employee>();
return employees;
}

public async Task<Employee> GetById(int id)
{
var employee = await _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefaultAsync();
return employee;
}

public async Task<string> Update(int id, Employee employee)
{
var employeeupt = await _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefaultAsync();
if (employeeupt == null) return "Employee does not exists";

employeeupt.Designation = employee.Designation;
employeeupt.Salary = employee.Salary;

await _dbcontext.SaveChanges();
return "Employee details successfully modified";
}

public async Task<string> Delete(int id)
{
var employeedel = _dbcontext.Employees.Where(empid => empid.Id == id).FirstOrDefault();
if (employeedel == null) return "Employee does not exists";

_dbcontext.Employees.Remove(employeedel);
await _dbcontext.SaveChanges();
return "Employee details deleted modified";
}
}
}
18 changes: 18 additions & 0 deletions ProCodeGuide.Samples.EFCore/Repository/IEmployeeRepository.cs
@@ -0,0 +1,18 @@
using ProCodeGuide.Samples.EFCore.DbContexts;
using ProCodeGuide.Samples.EFCore.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProCodeGuide.Samples.EFCore.Repository
{
public interface IEmployeeRepository
{
Task<int> Create(Employee employee);
Task<List<Employee>> GetAll();
Task<Employee> GetById(int id);
Task<string> Update(int id, Employee employee);
Task<string> Delete(int id);
}
}
4 changes: 4 additions & 0 deletions ProCodeGuide.Samples.EFCore/Startup.cs
Expand Up @@ -11,6 +11,8 @@
using Microsoft.Extensions.Logging;
using ProCodeGuide.Samples.EFCore.DbContexts;
using Microsoft.EntityFrameworkCore;
using ProCodeGuide.Samples.EFCore.Repository;
using ProCodeGuide.Samples.EFCore.Model;

namespace ProCodeGuide.Samples.EFCore
{
Expand All @@ -32,6 +34,8 @@ public void ConfigureServices(IServiceCollection services)
ef => ef.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());

services.AddTransient<IEmployeeRepository, EmployeeRepository>();

services.AddControllers();
}

Expand Down

0 comments on commit 9e9ceb4

Please sign in to comment.