Skip to content

Instantly share code, notes, and snippets.

@JIOO-phoeNIX
Created January 2, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JIOO-phoeNIX/aa7823d55e832cf505ad3f4956de8bed to your computer and use it in GitHub Desktop.
Save JIOO-phoeNIX/aa7823d55e832cf505ad3f4956de8bed to your computer and use it in GitHub Desktop.
using GraphQLSampleApp.DataAccess.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GraphQLSampleApp.DataAccess.DAO
{
public class EmployeeRepository
{
private readonly SampleAppDbContext _sampleAppDbContext;
public EmployeeRepository(SampleAppDbContext sampleAppDbContext)
{
_sampleAppDbContext = sampleAppDbContext;
}
public List<Employee> GetEmployees()
{
return _sampleAppDbContext.Employee.ToList();
}
public Employee GetEmployeeById(int id)
{
var employee = _sampleAppDbContext.Employee
.Include(e => e.Department)
.Where(e => e.EmployeeId == id)
.FirstOrDefault();
if (employee != null)
return employee;
return null;
}
public List<Employee> GetEmployeesWithDepartment()
{
return _sampleAppDbContext.Employee
.Include(e => e.Department)
.ToList();
}
public async Task<Employee> CreateEmployee(Employee employee)
{
await _sampleAppDbContext.Employee.AddAsync(employee);
await _sampleAppDbContext.SaveChangesAsync();
return employee;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment