コード例 #1
0
        public ActionResult EditLearningDay(EditLearningDayViewModel viewModel, string command)
        {
            viewModel = getData(viewModel);
            Debug.WriteLine("Edit learning day, command: " + command);

            string currentUserId = User.Identity.GetUserId();

            if (viewModel.LearningDay.User.Id != currentUserId)
            {
                return(View("Error")); // Cannot edit someone elses learning day.
            }

            if ((command == "Add Topic" || command == "Add New Topic") && viewModel.LearningDay.Topics.Where(tp => !tp.Remove).Count() >= 4)
            {
                ModelState.AddModelError("", "Maximum number (4) of topics per day reached.");
                return(View(viewModel));
            }

            if (command == "Add Topic")
            {
                TopicDay topic = topicManager.createTopicDay(viewModel.AddTopicId,
                                                             viewModel.LearningDay.LearningDayId,
                                                             viewModel.LearningDay.UserId);
                topic.NewlyCreated = true;
                // Forcefully loading the lazy Topic, so that it can be displayed.
                topic.Topic = topicManager.getTopicById(topic.TopicId);
                if (topic.Topic.ParentTopicId == null)
                {
                    topic.Topic.parentTopic = null;
                }
                else
                {
                    topic.Topic.parentTopic = topicManager.getTopicById((int)topic.Topic.ParentTopicId);
                }


                var duplicateTopicDays = viewModel.LearningDay.Topics.Where(topicDay => topicDay.TopicId == viewModel.AddTopicId);
                if (duplicateTopicDays.Count() > 0)
                {
                    // If the topic was marked for removal and is being added again, then it should just be visible again.
                    if (duplicateTopicDays.First().Remove)
                    {
                        duplicateTopicDays.First().Remove = false;
                    }
                    else
                    {
                        ModelState.AddModelError("", "The learning day already has this topic.");
                    }
                    return(View(viewModel));
                }
                viewModel.LearningDay.Topics.Add(topic);
            }
            else if (command == "Add New Topic")
            {
                Topic    newTopic = topicManager.createTopic(viewModel.NewTopicTitle, viewModel.NewTopicDescription, viewModel.NewTopicParentId);
                TopicDay topicDay = topicManager.createTopicDay(newTopic.TopicsId, viewModel.LearningDay.LearningDayId, viewModel.LearningDay.UserId);
                topicDay.NewlyCreated = true;
                topicDay.Topic        = newTopic; // topicManager.getTopicById(newTopic.TopicsId);
                // Forcefully loading the lazy Topic, so that it can be displayed.
                if (newTopic.ParentTopicId != null)
                {
                    topicDay.Topic.parentTopic = topicManager.getTopicById((int)newTopic.ParentTopicId);
                }

                viewModel.LearningDay.Topics.Add(topicDay);
            }
            else if (command == "Add Reference")
            {
                LDayReferences reference = new LDayReferences()
                {
                    LearningDayId = viewModel.LearningDay.LearningDayId,
                    UserId        = viewModel.LearningDay.UserId
                };
                viewModel.LearningDay.References.Add(reference);
            }
            else if (command == "Remove Selected Topics" || command == "Remove Selected References")
            {// Do nothing, as the objects are marked for deletion and remain in learning day until saved.
            }
            else if (command == "Cancel/Refresh")
            {
                viewModel.LearningDay = dayManager.getLearningDayById(viewModel.LearningDay.LearningDayId);
            }
            else
            {
                LearningDay oldDay = dayManager.getLearningDayById(viewModel.LearningDay.LearningDayId);
                if (viewModel.LearningDay.Date <= DateTime.Today.Date && viewModel.LearningDay.Date != oldDay.Date)
                {
                    ModelState.AddModelError("", "Cannot change learning day date to past date or today.");
                    return(View(viewModel));
                }
                else if (!dayManager.canAddOrUpdateDay(viewModel.LearningDay))
                {
                    ModelState.AddModelError("", "Cannot change learning day date, maximum days (3) in target quarter reached.");
                    return(View(viewModel));
                }
                else if (dayManager.isDateTaken(viewModel.LearningDay))
                {
                    ModelState.AddModelError("", "Cannot change learning day date, date already taken by another day.");
                    return(View(viewModel));
                }
                else if (viewModel.LearningDay.Title.IsNullOrWhiteSpace())
                {
                    ModelState.AddModelError("", "Learning day title is required and may not be empty.");
                    return(View(viewModel));
                }
                else if (viewModel.LearningDay.Topics.All(topicDay => topicDay.Remove))
                {
                    ModelState.AddModelError("", "Learning day must have at least one topic assigned. Cannot delete all topics.");
                    viewModel.LearningDay.Topics.ForEach(topic => topic.Remove = false);
                    return(View(viewModel));
                }

                if (!dayManager.updateLearningDay(viewModel.LearningDay, viewModel.LearningDay.RowVersion))
                {
                    TempData["UpdateLD"] = "The day was already modified by another user. If this message persists, click 'Refresh'.";
                    //LearningDay newDay = dayManager.getLearningDayById(learningDay.LearningDayId);
                    return(RedirectToAction("EditLearningDay", new { id = viewModel.LearningDay.LearningDayId }));
                }
                return(RedirectToAction("Schedule", new { userId = viewModel.LearningDay.UserId }));
            }

            return(View(viewModel));
        }
コード例 #2
0
 public bool updateLearningDay(LearningDay changedDay, byte[] RowState)
 {
     using (var context = new ApplicationDbContext())
     {
         // Don't update if the new day has no topics assigned. Learning days must have at least 1 topic.
         if (changedDay.Topics.Count == 0)
         {
             return(false);
         }
         try
         {
             var update = context.learningDays.Single(x => x.LearningDayId == changedDay.LearningDayId);
             update.Date        = changedDay.Date;
             update.Title       = changedDay.Title;
             update.Description = changedDay.Description;
             context.Entry(update).OriginalValues["RowVersion"] = RowState;
             foreach (var topic in changedDay.Topics)
             {
                 if (topic.Remove)
                 {
                     if (!topic.NewlyCreated)
                     {
                         TopicDay toRemove = context.topicDay.Single(x => x.TopicId == topic.TopicId &&
                                                                     x.LearningDayId == topic.LearningDayId &&
                                                                     x.UserId == topic.UserId);
                         context.topicDay.Remove(toRemove);
                     }
                 }
                 else
                 {
                     // A duplicate topic might be created if the virtual topic is not null.
                     topic.Topic = null;
                     context.topicDay.AddOrUpdate(topic);
                 }
             }
             foreach (var reference in changedDay.References)
             {
                 if (reference.Remove)
                 {
                     if (reference.ReferenceId > 0)
                     {
                         LDayReferences toRemove = context.lDayReferences.First(x => x.LearningDayId == reference.LearningDayId &&
                                                                                x.UserId == reference.UserId &&
                                                                                x.ReferenceUrl == reference.ReferenceUrl);
                         context.lDayReferences.Remove(toRemove);
                     }
                 }
                 else
                 {
                     context.lDayReferences.AddOrUpdate(reference);
                 }
             }
             context.learningDays.AddOrUpdate(update);
             context.SaveChanges();
             return(true);
         }
         catch (DbUpdateConcurrencyException ex)
         {
             return(false);
         }
         catch (DbUpdateException ex)
         {
             Exception tempEx = ex;
             // Exception may be nested. Might need to go deep for the exception message.
             while (tempEx.InnerException != null)
             {
                 tempEx = tempEx.InnerException;
             }
             Debug.WriteLine(tempEx.Message);
             return(false);
         }
     }
 }