147 lines
4.6 KiB
C#

using Marco.Pms.DataAccess.Data;
using Marco.Pms.DataAccess.Repository;
using Marco.Pms.DataAccess.Repository.IRepository;
using Marco.Pms.Model.Dtos.Project;
using Marco.Pms.Model.Entitlements;
using Marco.Pms.Model.Projects;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
using System.Xml;
using System.Xml.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace BulkyBook.DataAccess.Repository
{
public class ProjectRepository : Repository<Project>, IProjectRepository
{
private readonly ApplicationDbContext _context;
internal DbSet<Project> dbSet;
public ProjectRepository(ApplicationDbContext db) : base(db)
{
_context = db;
this.dbSet = _context.Set<Project>();
}
public void Update(Project project)
{
_context.Projects.Update(project);
}
public Task<List<Project>> GetAllAsync(Expression<Func<Project, bool>>? filter = null, string? includeProperties = null)
{
IQueryable<Project> query = _context.Projects;
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();
// return await _context.Projects.ToListAsync(); ;
}
public Task<List<Project>> GetAsync(Expression<Func<Project, bool>>? filter = null, string? includeProperties = null)
{
IQueryable<Project> query = _context.Projects;
//IQueryable<Project> query2 = _context.Projects;
Expression<Func<Project, bool>> filter1 = x => x.Id == 2;
if (filter != null)
{
query.Where(filter);
//var projects1= query2.Where(filter).ToListAsync();
}
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.ToListAsync();
// return await _context.Projects.ToListAsync(); ;
}
public async Task<Project> CreateAsync(Project projectModel)
{
try
{
await _context.Projects.AddAsync(projectModel);
await _context.SaveChangesAsync();
return projectModel;
}
catch (Exception ex) { throw ex; }
}
public async Task<Project> UpdateAsync(int id, UpdateProjectDto projectDto)
{
var projectModel = await _context.Projects.FirstOrDefaultAsync(item => item.Id == id);
if (projectModel == null)
{
return null;
}
projectModel.ContactPerson = projectDto.ContactPerson;
projectModel.ProjectAddress = projectDto.ProjectAddress;
projectModel.Name = projectDto.Name;
projectModel.EndDate = projectDto.EndDate;
projectModel.StartDate = projectDto.StartDate;
//projectModel.TenantId = projectDto.TenantId;
try
{
await _context.SaveChangesAsync();
}catch(Exception ex )
{
throw ex;
}
return projectModel;
}
//public Task<List<Project>> GetAsync(int id, string? includeProperties = null)
//{
// IQueryable<Project> query = _context.Projects;
// query.Where(c=>c.Id == id);
// // Task<Project> project = _context.Projects.Where(c=>c.Id == id).SingleOrDefaultAsync();
// //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();
// // return await _context.Projects.ToListAsync(); ;
//}
}
}