marco.pms.api/Marco.Pms.DataAccess/Repository/ProjectAllocationRepository.cs

85 lines
2.8 KiB
C#

using Marco.Pms.DataAccess.Data;
using Marco.Pms.DataAccess.Repository.IRepository;
using Marco.Pms.Model.Dtos.Project;
using Marco.Pms.Model.Projects;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Marco.Pms.DataAccess.Repository
{
public class ProjectAllocationRepository : Repository<ProjectAllocation>, IProjectAllocationRepository
{
private readonly ApplicationDbContext _context;
internal DbSet<ProjectAllocation> dbSet;
public ProjectAllocationRepository(ApplicationDbContext db) : base(db)
{
_context = db;
this.dbSet = _context.Set<ProjectAllocation>();
}
public void Update(ProjectAllocation project)
{
_context.ProjectAllocations.Update(project);
}
public async Task<ProjectAllocation> CreateAsync(ProjectAllocation project)
{
await _context.ProjectAllocations.AddAsync(project);
await _context.SaveChangesAsync();
return project;
}
public Task<List<ProjectAllocation>> GetAllAsync(Expression<Func<ProjectAllocation, bool>>? filter = null, string? includeProperties = null)
{
throw new NotImplementedException();
}
public Task<List<ProjectAllocation>> GetAsync(Expression<Func<ProjectAllocation, bool>>? filter = null, string? includeProperties = null)
{
IQueryable<ProjectAllocation> query = _context.ProjectAllocations;
if (filter != null)
{
query.Where(filter);
}
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.ToListAsync();
}
public async Task<ProjectAllocation> UpdateAsync(int id, ProjectAllocation project)
{
var allocation = await _context.ProjectAllocations.FirstOrDefaultAsync(item => item.Id == id);
if (allocation == null)
{
return null;
}
allocation.ProjectId = project.ProjectId;
allocation.EmployeeId = project.EmployeeId;
allocation.AllocationDate = project.AllocationDate;
allocation.ReAllocationDate = project.ReAllocationDate;
allocation.TenantId = project.TenantId;
_context.ProjectAllocations.Update(allocation);
await _context.SaveChangesAsync();
return allocation;
}
}
}