public void Update(T toEdit)
 {
     using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
     {
         KlanikContext _context = _provider.GetService <KlanikContext>();
         _context.SaveChanges();
     }
 }
        public void Delete(Konsultant toRemove)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                _context.Entry(toRemove).State = EntityState.Deleted;
            }
        }
        private static void CreateOrUpdateProfessionalExperience(KlanikContext _context, ProfessionalExperience exp)
        {
            var profExpExists = _context.ProfessionalExperiences.Any(x => x.Id == exp.Id);

            _context.Entry(exp).State =
                profExpExists ?
                EntityState.Modified :
                EntityState.Added;
        }
        public IEnumerable <T> GetAll()
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                return(_context.Set <T>().ToList());
            }
        }
        public T GetById(Guid id)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var set = _modifiers.Aggregate((IQueryable <T>)_context.Set <T>(),
                                               (current, include) => current.Include(include));

                return(set.FirstOrDefault(x => x.Id == id));
            }
        }
        private static void CreateOrUpdateCompetence(KlanikContext _context, OwnedCompetences comp)
        {
            var ownedCompExists = _context.OwnedCompetences.Any(x => x.CompetenceId == comp.CompetenceId &&
                                                                x.KonsultantId == comp.KonsultantId);
            var compExists = _context.Competences.Any(x => x.Id == comp.CompetenceId);

            _context.Entry(comp).State =
                ownedCompExists ?
                EntityState.Modified :
                EntityState.Added;

            _context.Entry(comp.Competence).State =
                compExists ?
                EntityState.Modified :
                EntityState.Added;
        }
        public void Delete(T toRemove)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var exists = _context.Set <T>().Any(x => x.Id == toRemove.Id);

                if (exists)
                {
                    _context.Entry(toRemove).State = EntityState.Deleted;
                }

                _context.SaveChanges();
            }
        }
        public void Create(T toCreate)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var exists = _context.Set <T>().Any(x => x.Id == toCreate.Id);

                _context.Entry(toCreate).State =
                    exists ?
                    EntityState.Modified :
                    EntityState.Added;

                _context.SaveChanges();
            }
        }
        private static void CreateOrUpdateEducation(KlanikContext _context, PersonalEducation edu)
        {
            var perEduExists = _context.PersonalEducations.Any(x => x.EducationId == edu.EducationId &&
                                                               x.KonsultantId == edu.KonsultantId);

            var eduExists = _context.Educations.Any(x => x.Id == edu.Education.Id);

            _context.Entry(edu).State =
                perEduExists ?
                EntityState.Modified :
                EntityState.Added;

            _context.Entry(edu.Education).State =
                eduExists ?
                EntityState.Modified :
                EntityState.Added;
        }
        private static void CreateOrUpdateCertificate(KlanikContext _context, OwnedCertificate cert)
        {
            var ownedCertExists = _context.OwnedCertificates.Any(x => x.CertificateId == cert.CertificateId &&
                                                                 x.KonsultantId == cert.KonsultantId);

            var certExists = _context.Certificates.Any(x => x.Id == cert.Certificate.Id);

            _context.Entry(cert).State =
                ownedCertExists ?
                EntityState.Modified :
                EntityState.Added;

            _context.Entry(cert.Certificate).State =
                certExists ?
                EntityState.Modified :
                EntityState.Added;
        }
        private static void CreateOrUpdateLanguage(KlanikContext _context, KnownLanguage lang)
        {
            var knownLangExists = _context.KnownLanguages.Any(x => x.LanguageId == lang.LanguageId &&
                                                              x.KonsultantId == lang.KonsultantId);

            var langExists = _context.Languages.Any(x => x.Id == lang.Language.Id);

            _context.Entry(lang).State =
                knownLangExists ?
                EntityState.Modified :
                EntityState.Added;

            _context.Entry(lang.Language).State =
                langExists ?
                EntityState.Modified :
                EntityState.Added;
        }
        public void Create(Konsultant toCreate)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var exists = _context.Konsultants.Any(x => x.Id == toCreate.Id);

                if (exists)
                {
                    Update(toCreate);
                }
                else
                {
                    _context.Add(toCreate);
                }

                _context.SaveChanges();
            }
        }
        private static void CreateOrUpdateProfessionalReference(KlanikContext _context, ProfessionalReference refer)
        {
            var referExists = _context.ProfessionalReferences.Any(x => x.Id == refer.Id);

            _context.Entry(refer).State =
                referExists ?
                EntityState.Modified :
                EntityState.Added;

            if (!referExists)
            {
                var contact = new Contact
                {
                    Mail  = refer.Contacts.Mail,
                    Phone = refer.Contacts.Phone,
                    ProfessionalReference = refer
                };

                _context.Entry(contact).State = EntityState.Added;
            }
        }
        public void Update(Konsultant toEdit)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();
                foreach (var comp in toEdit.Competences)
                {
                    CreateOrUpdateCompetence(_context, comp);
                }

                foreach (var edu in toEdit.Educations)
                {
                    CreateOrUpdateEducation(_context, edu);
                }

                foreach (var cert in toEdit.Certificates)
                {
                    CreateOrUpdateCertificate(_context, cert);
                }

                foreach (var lang in toEdit.Languages)
                {
                    CreateOrUpdateLanguage(_context, lang);
                }

                foreach (var exp in toEdit.ProfessionalExperiences)
                {
                    CreateOrUpdateProfessionalExperience(_context, exp);
                }

                foreach (var refer in toEdit.ProfessionalReferences)
                {
                    CreateOrUpdateProfessionalReference(_context, refer);
                }

                _context.SaveChanges();
            }
        }
        public Konsultant GetById(Guid id)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                return(_context.Konsultants
                       .Include(k => k.Languages)
                       .ThenInclude(l => l.Language)
                       .Include(k => k.Competences)
                       .ThenInclude(c => c.Competence)
                       .Include(k => k.Educations)
                       .ThenInclude(e => e.Education)
                       .Include(k => k.Certificates)
                       .ThenInclude(c => c.Certificate)
                       .Include(k => k.ProfessionalExperiences)
                       .ThenInclude(p => p.Accomplishments)
                       .Include(k => k.ProfessionalExperiences)
                       .ThenInclude(p => p.TechnicalEnvironments)
                       .Include(k => k.ProfessionalReferences)
                       .ThenInclude(p => p.Contacts)
                       .FirstOrDefault(x => x.Id == id));
            }
        }