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, IProjectRepository { private readonly ApplicationDbContext _context; internal DbSet dbSet; public ProjectRepository(ApplicationDbContext db) : base(db) { _context = db; this.dbSet = _context.Set(); } public void Update(Project project) { _context.Projects.Update(project); } public Task> GetAllAsync(Expression>? filter = null, string? includeProperties = null) { IQueryable 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> GetAsync(Expression>? filter = null, string? includeProperties = null) { IQueryable query = _context.Projects; //IQueryable query2 = _context.Projects; Expression> 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 CreateAsync(Project projectModel) { try { await _context.Projects.AddAsync(projectModel); await _context.SaveChangesAsync(); return projectModel; } catch (Exception ex) { throw ex; } } public async Task 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> GetAsync(int id, string? includeProperties = null) //{ // IQueryable query = _context.Projects; // query.Where(c=>c.Id == id); // // Task 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(); ; //} } }