first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public DbSet<Employee> Employees { get; set; }
|
||||
public DbSet<Country> Countries { get; set; }
|
||||
public DbSet<JobCategory> JobCategories { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
//seed categories
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 1, Name = "Belgium" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 2, Name = "Germany" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 3, Name = "Netherlands" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 4, Name = "USA" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 5, Name = "Japan" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 6, Name = "China" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 7, Name = "UK" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 8, Name = "France" });
|
||||
modelBuilder.Entity<Country>().HasData(new Country { CountryId = 9, Name = "Brazil" });
|
||||
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 1, JobCategoryName = "Pie research"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 2, JobCategoryName = "Sales"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 3, JobCategoryName = "Management"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 4, JobCategoryName = "Store staff"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 5, JobCategoryName = "Finance"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 6, JobCategoryName = "QA"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 7, JobCategoryName = "IT"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 8, JobCategoryName = "Cleaning"});
|
||||
modelBuilder.Entity<JobCategory>().HasData(new JobCategory(){JobCategoryId = 9, JobCategoryName = "Bakery"});
|
||||
|
||||
modelBuilder.Entity<Employee>().HasData(new Employee
|
||||
{
|
||||
EmployeeId = 1,
|
||||
CountryId = 1,
|
||||
MaritalStatus = MaritalStatus.Single,
|
||||
BirthDate = new DateTime(1979, 1, 16),
|
||||
City = "Brussels",
|
||||
Email = "bethany@bethanyspieshop.com",
|
||||
FirstName = "Bethany",
|
||||
LastName = "Smith",
|
||||
Gender = Gender.Female,
|
||||
PhoneNumber = "324777888773",
|
||||
Smoker = false,
|
||||
Street = "Grote Markt 1",
|
||||
Zip = "1000",
|
||||
JobCategoryId = 1,
|
||||
Comment = "Lorem Ipsum",
|
||||
ExitDate = null,
|
||||
JoinedDate = new DateTime(2015, 3, 1),
|
||||
Latitude = 50.8503,
|
||||
Longitude = 4.3517
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public class CountryRepository : ICountryRepository
|
||||
{
|
||||
private readonly AppDbContext _appDbContext;
|
||||
|
||||
public CountryRepository(AppDbContext appDbContext)
|
||||
{
|
||||
_appDbContext = appDbContext;
|
||||
}
|
||||
|
||||
public IEnumerable<Country> GetAllCountries()
|
||||
{
|
||||
return _appDbContext.Countries;
|
||||
}
|
||||
|
||||
public Country GetCountryById(int countryId)
|
||||
{
|
||||
return _appDbContext.Countries.FirstOrDefault(c => c.CountryId == countryId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public class EmployeeRepository : IEmployeeRepository
|
||||
{
|
||||
private readonly AppDbContext _appDbContext;
|
||||
private Random random = new Random();
|
||||
|
||||
public EmployeeRepository(AppDbContext appDbContext)
|
||||
{
|
||||
_appDbContext = appDbContext;
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> GetAllEmployees()
|
||||
{
|
||||
return _appDbContext.Employees;
|
||||
}
|
||||
|
||||
public Employee GetEmployeeById(int employeeId)
|
||||
{
|
||||
return _appDbContext.Employees.FirstOrDefault(c => c.EmployeeId == employeeId);
|
||||
}
|
||||
|
||||
public Employee AddEmployee(Employee employee)
|
||||
{
|
||||
var addedEntity = _appDbContext.Employees.Add(employee);
|
||||
_appDbContext.SaveChanges();
|
||||
return addedEntity.Entity;
|
||||
}
|
||||
|
||||
public Employee UpdateEmployee(Employee employee)
|
||||
{
|
||||
var foundEmployee = _appDbContext.Employees.FirstOrDefault(e => e.EmployeeId == employee.EmployeeId);
|
||||
|
||||
if (foundEmployee != null)
|
||||
{
|
||||
foundEmployee.CountryId = employee.CountryId;
|
||||
foundEmployee.MaritalStatus = employee.MaritalStatus;
|
||||
foundEmployee.BirthDate = employee.BirthDate;
|
||||
foundEmployee.City = employee.City;
|
||||
foundEmployee.Email = employee.Email;
|
||||
foundEmployee.FirstName = employee.FirstName;
|
||||
foundEmployee.LastName = employee.LastName;
|
||||
foundEmployee.Gender = employee.Gender;
|
||||
foundEmployee.PhoneNumber = employee.PhoneNumber;
|
||||
foundEmployee.Smoker = employee.Smoker;
|
||||
foundEmployee.Street = employee.Street;
|
||||
foundEmployee.Zip = employee.Zip;
|
||||
foundEmployee.JobCategoryId = employee.JobCategoryId;
|
||||
foundEmployee.Comment = employee.Comment;
|
||||
foundEmployee.ExitDate = employee.ExitDate;
|
||||
foundEmployee.JoinedDate = employee.JoinedDate;
|
||||
//foundEmployee.ImageContent = employee.ImageContent;
|
||||
//foundEmployee.ImageName = employee.ImageName;
|
||||
|
||||
_appDbContext.SaveChanges();
|
||||
|
||||
return foundEmployee;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void DeleteEmployee(int employeeId)
|
||||
{
|
||||
var foundEmployee = _appDbContext.Employees.FirstOrDefault(e => e.EmployeeId == employeeId);
|
||||
if (foundEmployee == null) return;
|
||||
|
||||
_appDbContext.Employees.Remove(foundEmployee);
|
||||
_appDbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public interface ICountryRepository
|
||||
{
|
||||
IEnumerable<Country> GetAllCountries();
|
||||
Country GetCountryById(int countryId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public interface IEmployeeRepository
|
||||
{
|
||||
IEnumerable<Employee> GetAllEmployees();
|
||||
Employee GetEmployeeById(int employeeId);
|
||||
Employee AddEmployee(Employee employee);
|
||||
Employee UpdateEmployee(Employee employee);
|
||||
void DeleteEmployee(int employeeId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public interface IJobCategoryRepository
|
||||
{
|
||||
IEnumerable<JobCategory> GetAllJobCategories();
|
||||
JobCategory GetJobCategoryById(int jobCategoryId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using BethanysPieShopHRM.Shared.Domain;
|
||||
|
||||
namespace BethanysPieShopHRM.Api.Models
|
||||
{
|
||||
public class JobCategoryRepository: IJobCategoryRepository
|
||||
{
|
||||
private readonly AppDbContext _appDbContext;
|
||||
|
||||
public JobCategoryRepository(AppDbContext appDbContext)
|
||||
{
|
||||
_appDbContext = appDbContext;
|
||||
}
|
||||
|
||||
public IEnumerable<JobCategory> GetAllJobCategories()
|
||||
{
|
||||
return _appDbContext.JobCategories;
|
||||
}
|
||||
|
||||
public JobCategory GetJobCategoryById(int jobCategoryId)
|
||||
{
|
||||
return _appDbContext.JobCategories.FirstOrDefault(c => c.JobCategoryId == jobCategoryId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user