Files
Prog4_Beadando/WD7UVN_HFT_2023241.Repository/CRUD.cs

202 lines
6.7 KiB
C#
Raw Normal View History

2023-11-22 11:39:16 +01:00
using System;
2023-11-22 11:47:56 +01:00
using System.Linq;
2023-11-22 11:39:16 +01:00
using Microsoft.EntityFrameworkCore;
using WD7UVN_HFT_2023241.Models;
using WD7UVN_HFT_2023241.Repository;
namespace WD7UVN_HFT_2023241.Repository
{
2023-11-22 14:02:02 +01:00
public interface ICRUD : ICustomerRepository, IEmployeeRepository, IMaintainerTeamRepository, IServiceRepository { }
public interface ICustomerRepository
{
public void CreateCustomer(Customer customer);
public Customer ReadCustomer(int customerId);
public IQueryable<Customer> ReadAllCustomers();
public void UpdateCustomer(Customer updatedCustomer);
public void DeleteCustomer(int customerId);
}
public class CustomerRepository : ICustomerRepository
2023-11-22 11:39:16 +01:00
{
2023-11-22 14:02:02 +01:00
public void CreateCustomer(Customer customer)
2023-11-22 11:39:16 +01:00
{
Database.Context.Customers.Add(customer);
Database.Context.SaveChanges();
}
2023-11-22 14:02:02 +01:00
public Customer ReadCustomer(int customerId)
2023-11-22 11:39:16 +01:00
{
return Database.Context.Customers.Find(customerId);
}
2023-11-22 11:47:56 +01:00
public IQueryable<Customer> ReadAllCustomers()
{
return Database.Context.Customers.AsQueryable();
}
2023-11-22 14:02:02 +01:00
public void UpdateCustomer(Customer updatedCustomer)
2023-11-22 11:39:16 +01:00
{
Customer existingCustomer = Database.Context.Customers.Find(updatedCustomer.ID);
if (existingCustomer != null)
{
Database.Context.Entry(existingCustomer).CurrentValues.SetValues(updatedCustomer);
Database.Context.SaveChanges();
}
}
2023-11-22 14:02:02 +01:00
public void DeleteCustomer(int customerId)
2023-11-22 11:39:16 +01:00
{
Customer customerToDelete = Database.Context.Customers.Find(customerId);
if (customerToDelete != null)
{
Database.Context.Customers.Remove(customerToDelete);
Database.Context.SaveChanges();
}
}
}
2023-11-22 14:02:02 +01:00
public interface IMaintainerTeamRepository
{
public void CreateMaintainerTeam(MaintainerTeam maintainerTeam);
public MaintainerTeam ReadMaintainerTeam(int maintainerTeamId);
public IQueryable<MaintainerTeam> ReadAllMaintainerTeams();
public void UpdateMaintainerTeam(MaintainerTeam updatedMaintainerTeam);
public void DeleteMaintainerTeam(int maintainerTeamId);
}
public class MaintainerTeamRepository : IMaintainerTeamRepository
2023-11-22 11:39:16 +01:00
{
2023-11-22 14:02:02 +01:00
public void CreateMaintainerTeam(MaintainerTeam maintainerTeam)
2023-11-22 11:39:16 +01:00
{
Database.Context.Maintainers.Add(maintainerTeam);
Database.Context.SaveChanges();
}
2023-11-22 14:02:02 +01:00
public MaintainerTeam ReadMaintainerTeam(int maintainerTeamId)
2023-11-22 11:39:16 +01:00
{
return Database.Context.Maintainers.Find(maintainerTeamId);
}
2023-11-22 11:47:56 +01:00
public IQueryable<MaintainerTeam> ReadAllMaintainerTeams()
{
return Database.Context.Maintainers.AsQueryable();
}
2023-11-22 14:02:02 +01:00
public void UpdateMaintainerTeam(MaintainerTeam updatedMaintainerTeam)
2023-11-22 11:39:16 +01:00
{
MaintainerTeam existingMaintainerTeam = Database.Context.Maintainers.Find(updatedMaintainerTeam.ID);
if (existingMaintainerTeam != null)
{
Database.Context.Entry(existingMaintainerTeam).CurrentValues.SetValues(updatedMaintainerTeam);
Database.Context.SaveChanges();
}
}
2023-11-22 14:02:02 +01:00
public void DeleteMaintainerTeam(int maintainerTeamId)
2023-11-22 11:39:16 +01:00
{
MaintainerTeam maintainerTeamToDelete = Database.Context.Maintainers.Find(maintainerTeamId);
if (maintainerTeamToDelete != null)
{
Database.Context.Maintainers.Remove(maintainerTeamToDelete);
Database.Context.SaveChanges();
}
}
}
2023-11-22 14:02:02 +01:00
public interface IServiceRepository
2023-11-22 11:39:16 +01:00
{
2023-11-22 14:02:02 +01:00
public void CreateService(Service service);
public Service ReadService(int serviceId);
public IQueryable<Service> ReadAllServices();
public void UpdateService(Service updatedService);
public void DeleteService(int serviceId);
}
public class ServiceRepository : IServiceRepository
{
public void CreateService(Service service)
2023-11-22 11:39:16 +01:00
{
Database.Context.Services.Add(service);
Database.Context.SaveChanges();
}
2023-11-22 14:02:02 +01:00
public Service ReadService(int serviceId)
2023-11-22 11:39:16 +01:00
{
return Database.Context.Services.Find(serviceId);
}
2023-11-22 11:47:56 +01:00
public IQueryable<Service> ReadAllServices()
{
return Database.Context.Services.AsQueryable();
}
2023-11-22 14:02:02 +01:00
public void UpdateService(Service updatedService)
2023-11-22 11:39:16 +01:00
{
Service existingService = Database.Context.Services.Find(updatedService.ID);
if (existingService != null)
{
Database.Context.Entry(existingService).CurrentValues.SetValues(updatedService);
Database.Context.SaveChanges();
}
}
2023-11-22 14:02:02 +01:00
public void DeleteService(int serviceId)
2023-11-22 11:39:16 +01:00
{
Service serviceToDelete = Database.Context.Services.Find(serviceId);
if (serviceToDelete != null)
{
Database.Context.Services.Remove(serviceToDelete);
Database.Context.SaveChanges();
}
}
}
2023-11-22 14:02:02 +01:00
public interface IEmployeeRepository
{
public void CreateEmployee(Employee employee);
public Employee ReadEmployee(int employeeId);
public IQueryable<Employee> ReadAllEmployees();
public void UpdateEmployee(Employee updatedEmployee);
public void DeleteEmployee(int employeeId);
}
public class EmployeeRepository : IEmployeeRepository
2023-11-22 11:39:16 +01:00
{
2023-11-22 14:02:02 +01:00
public void CreateEmployee(Employee employee)
2023-11-22 11:39:16 +01:00
{
Database.Context.Employees.Add(employee);
Database.Context.SaveChanges();
}
2023-11-22 14:02:02 +01:00
public Employee ReadEmployee(int employeeId)
2023-11-22 11:39:16 +01:00
{
return Database.Context.Employees.Find(employeeId);
}
2023-11-22 11:47:56 +01:00
public IQueryable<Employee> ReadAllEmployees()
{
return Database.Context.Employees.AsQueryable();
}
2023-11-22 14:02:02 +01:00
public void UpdateEmployee(Employee updatedEmployee)
2023-11-22 11:39:16 +01:00
{
Employee existingEmployee = Database.Context.Employees.Find(updatedEmployee.ID);
if (existingEmployee != null)
{
Database.Context.Entry(existingEmployee).CurrentValues.SetValues(updatedEmployee);
Database.Context.SaveChanges();
}
}
2023-11-22 14:02:02 +01:00
public void DeleteEmployee(int employeeId)
2023-11-22 11:39:16 +01:00
{
Employee employeeToDelete = Database.Context.Employees.Find(employeeId);
if (employeeToDelete != null)
{
Database.Context.Employees.Remove(employeeToDelete);
Database.Context.SaveChanges();
}
}
}
}