Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a532f2b
commit 9e9ceb4
Showing
6 changed files
with
101 additions
and
31 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
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
60 changes: 60 additions & 0 deletions
60
ProCodeGuide.Samples.EFCore/Repository/EmployeeRepository.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,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
18
ProCodeGuide.Samples.EFCore/Repository/IEmployeeRepository.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,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); | ||
} | ||
} |
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