Example #1
0
        public TemplateModel CreateTemplate(TemplateModel templateModel)
        {
            List <string> errorMessage = null;

            if (!templateModel.IsValid(out errorMessage))
            {
                throw new ArgumentException("templateModel", String.Join("; ", errorMessage));
            }



            var template = templateModel.MapTemplateModelToTemplate();

            List <int> skillsToAdd = templateModel.Skills.Select(x => x.Id).ToList();

            var skillsFromContext = _context.Skills.
                                    Where(x => skillsToAdd.Contains(x.SkillId)).
                                    ToList();

            template.Skills = skillsFromContext;

            _context.Templates.Add(template);

            _context.SaveChanges();

            templateModel.Id = template.TemplateId;

            return(templateModel);
        }
Example #2
0
        public TemplateModel UpdateTemplate(int templateId, TemplateModel templateModel)
        {
            List <string> errorMessage = null;

            if (!templateModel.IsValid(out errorMessage))
            {
                throw new ArgumentException("templateModel", String.Join("; ", errorMessage));
            }

            var template = _context.Templates
                           .Include(x => x.Skills)
                           .Include(x => x.ApplicationsUsers)
                           .Include(x => x.PerformanceEvaluations)
                           .Where(x => x.TemplateId == templateId)
                           .FirstOrDefault();

            if (template == null)
            {
                throw new ApplicationException(String.Format("Template with id {0} does not exist", templateModel.Id));
            }

            templateModel.canBeEdited = template.PerformanceEvaluations != null && template.PerformanceEvaluations
                                        .Where(x => x.IsSubmitted == false).Count() > 0;

            if (!templateModel.canBeEdited)
            {
                template = templateModel.MapTemplateModelToTemplate(template);


                List <int> skillsToAdd = templateModel.Skills.Select(x => x.Id).ToList();

                template.Skills.Clear();


                var skillsFromContext = _context.Skills.Where(x => skillsToAdd.Contains(x.SkillId)).ToList();

                template.Skills = skillsFromContext;

                _context.SaveChanges();
            }
            else
            {
                throw new InvalidOperationException(String.Format("Template cant be edited"));
            }

            return(template.MapTemplateToTemplateModel(true));
        }