Skip to content

Instantly share code, notes, and snippets.

@gzamudio
Last active March 14, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gzamudio/f09e78dabd0462b5f52979eb1b258617 to your computer and use it in GitHub Desktop.
Save gzamudio/f09e78dabd0462b5f52979eb1b258617 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using EduitorNetCore.Models.Database;
using EduitorNetCore.Models.VOs.Reduceds;
using EduitorNetCore.Repositories.Database;
using EduitorNetCore.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace EduitorNetCore.Repositories
{
public class CategoryRepository : Repository<Category>, ICategoryRepository
{
public CategoryRepository(DataBaseContext context) : base(context) { }
public IEnumerable<Category> GetAllIncludeInCategoriesId(IEnumerable<int> categoryIds)
{
return Context.Categories.Where(c => categoryIds.Contains(c.Id));
}
// we need to use this GetAll method
public IEnumerable<Category> GetAllWithRelatedEntities() => Context.Categories.Include(c => c.Resources);
}
}
private List<SkillsByCategoryVO> GetSkillsByCategoryAndRole(User currentUser, string startingDate, int? schoolId, string role)
{
return UnitOfWork.CategoryRepository.GetAllWithRelatedEntities()
.Select(c => new SkillsByCategoryVO(new CategoryVO(c),
c.Resources.Where(r => r.ResourceAccesses.Where(ra =>
ra.UpdatedAt >= DateTime.ParseExact(startingDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
&& (VerifyConditionByRole(role, schoolId, ra))).Any())
.SelectMany(r => r.SkillResources
.GroupBy(sr => sr.SkillId)
.Select(srgroup => new SkillPercentageVO(
new SkillVO(srgroup.Select(sr => sr.Skill).FirstOrDefault()),
100 * ((float)srgroup.Count() / r.SkillResources.Count)))).ToList())).ToList();
}
private bool VerifyConditionByRole(string role, int? schoolId, ResourceAccess ra)
{
if (role == "Admin")
return (schoolId == null || ra.User.SchoolGroup.Schools.Where(s => s.Id == schoolId).Any());
else if (role == "Teacher")
return ra.User.TeacherSchools.Where(ts => ts.SchoolId == schoolId).Any();
else if (role == "Manager")
return ra.User.ManagerSchools.Where(ts => ts.SchoolId == schoolId).Any();
throw new ArgumentException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment