예제 #1
0
 public void Add(Report report)
 {
     using (var dbContext = new CustomDBContext(connectionString))
     {
         dbContext.Reports.Add(report);
         dbContext.SaveChanges();
     }
 }
예제 #2
0
 public Report GetById(Guid id)
 {
     using (var dbContext = new CustomDBContext(connectionString))
     {
         return(dbContext.Reports.Where(report => report.Id == id)
                .Include(report => report.Activities
                         .Select(activity => activity.Questions))
                .Include(report => report.FuturePlans).First());
     }
 }
예제 #3
0
        private void RemoveNonexistentFuturePlans(Report report, CustomDBContext dbContext)
        {
            var listOriginalFuturePlans = dbContext.FuturePlans.Where(activity => activity.ReportId == report.Id).ToList();

            foreach (var originalFuturePlan in listOriginalFuturePlans)
            {
                if (!report.FuturePlans.Any(futurePlan => futurePlan.Id == originalFuturePlan.Id))
                {
                    dbContext.FuturePlans.Remove(originalFuturePlan);
                }
            }
        }
예제 #4
0
 private void AddNewAndEditOldFuturePlans(Report report, CustomDBContext dbContext)
 {
     foreach (var futurePlan in report.FuturePlans)
     {
         var originalFuturePlan = dbContext.FuturePlans.Find(futurePlan.Id);
         if (originalFuturePlan == null)
         {
             dbContext.FuturePlans.Add(futurePlan);
         }
         else
         {
             dbContext.Entry(originalFuturePlan).CurrentValues.SetValues(futurePlan);
         }
     }
 }
예제 #5
0
 private void AddNewAndEditOldQuestions(Activity activity, CustomDBContext dbContext)
 {
     foreach (var question in activity.Questions)
     {
         var originalQuestion = dbContext.Questions.Find(question.Id);
         if (originalQuestion == null)
         {
             dbContext.Questions.Add(question);
         }
         else
         {
             dbContext.Entry(originalQuestion).CurrentValues.SetValues(question);
         }
     }
 }
예제 #6
0
        private void AddNewAndEditOldActivities(Report report, CustomDBContext dbContext)
        {
            foreach (var activity in report.Activities)
            {
                var originalActivity = dbContext.Activities.Find(activity.Id);
                if (originalActivity == null)
                {
                    dbContext.Activities.Add(activity);
                }
                else
                {
                    dbContext.Entry(originalActivity).CurrentValues.SetValues(activity);
                }

                AddNewAndEditOldQuestions(activity, dbContext);
            }
        }
예제 #7
0
        private void RemoveNonexistentActivities(Report report, CustomDBContext dbContext)
        {
            var listOriginalActivities = dbContext.Activities.Where(activity => activity.ReportId == report.Id).ToList();

            foreach (var originalActivity in listOriginalActivities)
            {
                Activity newActivity = report.Activities.Where(activity => activity.Id == originalActivity.Id).FirstOrDefault();
                if (newActivity == null)
                {
                    dbContext.Activities.Remove(originalActivity);
                    //Каскад?
                }
                else
                {
                    RemoveNonexistentQuestions(newActivity, dbContext);
                }
            }
        }
예제 #8
0
        public void Edit(Report report)
        {
            using (var dbContext = new CustomDBContext(connectionString))
            {
                var transaction = dbContext.Database.BeginTransaction();

                //Remove
                RemoveNonexistentActivities(report, dbContext);
                RemoveNonexistentFuturePlans(report, dbContext);

                //Edit and add
                var originalReport = dbContext.Reports.Find(report.Id);
                dbContext.Entry(originalReport).CurrentValues.SetValues(report);

                AddNewAndEditOldActivities(report, dbContext);
                AddNewAndEditOldFuturePlans(report, dbContext);

                transaction.Commit();
                dbContext.SaveChanges();
            }
        }
예제 #9
0
        private void RemoveNonexistentQuestions(Activity newActivity, CustomDBContext dbContext)
        {
            var listOriginalQuestions = dbContext.Questions.Where(question => question.ActivityId == newActivity.Id).ToList();

            if (listOriginalQuestions.Any())
            {
                foreach (var originalQuestion in listOriginalQuestions)
                {
                    if (newActivity.Questions == null)
                    {
                        dbContext.Questions.Remove(originalQuestion);
                    }
                    else
                    {
                        Question newQuestion = newActivity.Questions.Where(question => question.Id == originalQuestion.Id).FirstOrDefault();
                        if (newQuestion == null)
                        {
                            dbContext.Questions.Remove(originalQuestion);
                        }
                    }
                }
            }
        }
예제 #10
0
        public IList <Report> SearchForValidation(SearchModel searchModel)
        {
            using (var dbContext = new CustomDBContext(connectionString))
            {
                var query = dbContext.Reports.Select(report => report);
                if (searchModel.DateTo != null)
                {
                    query = query.Where(report => report.Date <= searchModel.DateTo);
                }

                if (searchModel.DateFrom != null)
                {
                    query = query.Where(report => report.Date >= searchModel.DateFrom);
                }

                if (!string.IsNullOrEmpty(searchModel.Title))
                {
                    query = query.Where(report => report.Title == searchModel.Title);
                }

                if (searchModel.InternsId != null)
                {
                    query = query.Where(report => report.InternsId == searchModel.InternsId);
                }

                if (searchModel.MentorsId != null)
                {
                    query = query.Where(report => report.MentorsId == searchModel.MentorsId);
                }

                return(query.Include(report => report.Activities
                                     .Select(activity => activity.Questions))
                       .Include(report => report.FuturePlans)
                       .ToList());
            }
        }
예제 #11
0
        public IList <Report> SearchForUser(SearchModel searchModel)
        {
            using (var dbContext = new CustomDBContext(connectionString))
            {
                var query = dbContext.Reports.Select(report => report);
                if (searchModel.DateTo != null)
                {
                    query = query.Where(report => report.Date <= searchModel.DateTo);
                }

                if (searchModel.DateFrom != null)
                {
                    query = query.Where(report => report.Date >= searchModel.DateFrom);
                }

                if (!string.IsNullOrEmpty(searchModel.Title))
                {
                    query = query.Where(report => report.Title.Contains(searchModel.Title));
                }

                if (searchModel.TypeOccuring != "All")
                {
                    query = query.Where(report => report.TypeOccuring == searchModel.TypeOccuring);
                }

                if (searchModel.InternsId != null)
                {
                    query = query.Where(report => report.InternsId == searchModel.InternsId);
                }

                if (searchModel.MentorsId != null)
                {
                    query = query.Where(report => report.MentorsId == searchModel.MentorsId);
                }

                if (searchModel.TypeOrigin == "Mentor's")
                {
                    query = query.Where(report => report.MentorsId != null);
                }
                else if (searchModel.TypeOrigin == "Intern's")
                {
                    query = query.Where(report => report.MentorsId == null);
                }

                if (searchModel.RequesterUserId != null)
                {
                    query = query.Where(report => !report.IsDraft ||
                                        report.IsDraft && report.MentorsId == null && report.InternsId == searchModel.RequesterUserId ||
                                        report.IsDraft && report.MentorsId != null && report.MentorsId == searchModel.RequesterUserId);
                }
                else
                {
                    query = query.Where(report => !report.IsDraft);
                }

                query = query.OrderBy(report => report.Date);

                return(query.Include(report => report.Activities
                                     .Select(activity => activity.Questions))
                       .Include(report => report.FuturePlans)
                       .ToList());
            }
        }