Kurcze tak wygląda całe moje "repozytorium" które jest tak naprawdę DAO tak to przynajmniej rozumiem to czy tak naprawdę potrzebne bedzię mi repozytorium ?
a jeśli tak to jak mam je zaimplementować bo tego nie rozumiem :(
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Work.Aggregated.Tenure;
using Work.Core.Domain;
using Work.Data;
using Work.Repositories.Interfaces;
using Work.Web.Essentials.ServiceResponse;
namespace Work.Repositories
{
public class TenureRepository : ITenureRepository
{
private readonly ApplicationDbContext db;
private readonly IWorkApplicationRepository workApplicationRepository;
public TenureRepository(ApplicationDbContext _db, IWorkApplicationRepository workApplicationRepository)
{
db = _db;
this.workApplicationRepository = workApplicationRepository;
}
public async Task AddAsync(Tenure tenure)
{
await db.Tenures.AddAsync(tenure);
db.SaveChanges();
}
public async Task ChangeStatusAsync(Tenure tenure)
{
var status = !tenure.IsActive;
tenure.SetStatus(status);
db.Entry(tenure).State = EntityState.Modified;
await db.SaveChangesAsync();
}
public async Task DeleteAsync(Tenure tenure)
{
db.Entry(tenure).State = EntityState.Deleted;
await db.SaveChangesAsync();
}
public async Task EditAsync(Tenure tenure)
{
db.Entry(tenure).State = EntityState.Modified;
await db.SaveChangesAsync();
}
public async Task<PagedResult<TenureList>> GetAllActiveAsync(string userId, int? page, Expression<Func<TenureList, bool>> predicate)
{
var tenures = db.Tenures.Include(x=>x.Company).Where(x => x.IsActive == true && x.Company.IsActive == true && x.IsPremium == false).Select(x=> new TenureList
{
Id = x.Id,
Name = x.Name,
Address = x.Address,
CreatedAt = x.ConvertTenureDate(),
IsActive = x.IsActive,
CategoryId = x.CategoryId,
City = x.City,
CompanyId = x.CompanyId,
CompanyName = x.GetCompanyName(),
IsPremium = x.SetPremium(),
LogoUrl = x.GetCompanyLogoUrl(),
Rate = x.Rate,
WorkTimeTypes = x.WorkTimeTypes,
IsApply = workApplicationRepository.CheckIsApply(userId, x.Id)
});
tenures = tenures.Where(predicate);
var tenuresCount = tenures.Count();
page = page - 1;
var pageSize = 10;
int startPage = 1;
int endPage = 5;
int totalItems = tenures.Count();
int totalElements = tenuresCount;
int totalPages = (int)Math.Ceiling((decimal)totalItems / pageSize);
int currentPage = page ?? 1;
if (currentPage >= 5 && currentPage <= (totalPages - 4))
{
startPage = currentPage - 1;
endPage = currentPage + 1;
}
else if (currentPage > (totalPages - 4))
{
startPage = totalPages - 4;
endPage = totalPages;
}
if (startPage <= 0)
{
startPage = 1;
}
if (endPage >= totalPages)
{
endPage = totalPages;
}
var result = await tenures
.OrderByDescending(x => x.CreatedAt)
.Skip((int)currentPage * pageSize)
.Take(pageSize)
.ToListAsync();
return new PagedResult<TenureList>
{
Results = result,
PageSize = pageSize,
TotalCount = totalPages,
StartPage = startPage,
EndPage = endPage,
CurrentPage = currentPage,
ResultCount = totalItems,
TotalElements = totalElements
};
}
public async Task<IReadOnlyList<TenureListAdmin>> GetAllAsync()
{
return await db.Tenures.Include(x => x.Company).Select(x => new TenureListAdmin
{
Id = x.Id,
CreatedAt = x.CreatedAt.ToString("dd/MM/yyyy"),
IsActive = x.IsActive,
CategoryId = x.CategoryId,
City = x.City,
CompanyId = x.CompanyId,
CompanyName = x.GetCompanyName(),
Name = x.Name,
Rate = x.Rate,
ApplyCount = x.ApplyCount
}).ToListAsync();
}
public async Task<IReadOnlyList<Tenure>> GetAllPremiumAsync()
{
return await db.Tenures.Where(x => x.IsActive == true && x.Company.IsActive == true && x.IsPremium == true).Include(x => x.Company).OrderByDescending(x => x.CreatedAt).ToListAsync();
}
public async Task<Tenure> GetAsync(long id)
{
return await db.Tenures.Include(x => x.Company).SingleOrDefaultAsync(x => x.Id == id);
}
public async Task<IEnumerable<DailyTenureAdd>> GetDailyTenureAdd()
{
var items = new List<DailyTenureAdd>();
for (int i = -7; i <= 0; i++)
{
var date = DateTime.Now.AddDays(i).Date;
var item = await db.Tenures.GroupBy(x => date).Select(x =>
new DailyTenureAdd
{
CountTenures = x.Where(z=>z.CreatedAt.Date.ToString("dd/MM/yyyy") == date.Date.ToString("dd/MM/yyyy")).Count(),
CreatedAt = x.Key.ToString("dd/MM/yyyy")
}).SingleOrDefaultAsync();
items.Add(item);
}
return items;
}
public async Task<TenureDetails> GetDetailsAsync(long id)
{
return await db.Tenures.Include(x => x.Company).Select(x=> new TenureDetails {
Id = x.Id,
Address = x.Address,
AgreementTypes = x.ConvertAgreementTypes(),
CreatedAt = x.ConvertTenureDate(),
Description = x.Description,
CategoryId = x.CategoryId,
City = x.City,
CompanyId = x.CompanyId,
CompanyName = x.GetCompanyName(),
ExpirationDate = x.ExpirationDate,
IsActive = x.IsActive,
IsPremium = x.SetPremium(),
LogoUrl = x.GetCompanyLogoUrl(),
Name = x.Name,
Rate = x.Rate,
WorkTimeTypes = x.ConvertWorkTimeTypes()
}).SingleOrDefaultAsync(x => x.Id == id);
}
public async Task<IReadOnlyList<SimpleTenureList>> GetSimpleTenuresAsync(long id)
{
var tenure = await GetAsync(id);
var payload = await db.Tenures.Include(x => x.Company).Where(x => x.Name.ToLower().Contains(tenure.Name.ToLower()) && x.IsActive == true && x.Id != id).Select(x => new SimpleTenureList
{
Id = x.Id,
Name = x.Name,
City = x.City,
CompanyName = x.GetCompanyName(),
Rate = x.Rate
}).ToListAsync();
return payload;
}
public async Task<IReadOnlyList<TenureList>> GetTenureCompanyAsync(long id)
{
return await db.Tenures.Include(x => x.Company).Where(x => x.Company.Id == id && x.IsActive == true).Select(x => new TenureList
{
Id = x.Id,
Name = x.Name,
Address = x.Address,
CreatedAt = x.ConvertTenureDate(),
IsActive = x.IsActive,
CategoryId = x.CategoryId,
City = x.City,
CompanyId = x.CompanyId,
CompanyName = x.GetCompanyName(),
IsPremium = x.SetPremium(),
LogoUrl = x.GetCompanyLogoUrl(),
Rate = x.Rate,
WorkTimeTypes = x.WorkTimeTypes
}).OrderByDescending(x=>x.CreatedAt).ToListAsync();
}
public async Task<int> GetTenureCountAsync()
{
return await db.Tenures.Where(x => x.IsActive == true && x.Company.IsActive == true).CountAsync();
}
public async Task IncrementApplyCount(long id)
{
var tenure = await db.Tenures.FirstOrDefaultAsync(x => x.Id == id);
tenure.IncrementApplyCount();
db.Entry(tenure).State = EntityState.Modified;
await db.SaveChangesAsync();
}
}
}