public bool SaveLearnResult(List <FlashcardForLearn> flashcardsForLearn) { bool output = false; var subFlashcardIdList = flashcardsForLearn.Select(x => x.FlashcardSubscriptionId).ToList(); var subFlashcards = _context.SubscribedFlashcards.Where(x => subFlashcardIdList.Contains(x.Id)).ToList(); foreach (var subFlashcard in subFlashcards) { var flashcardResult = flashcardsForLearn.Single(x => x.FlashcardSubscriptionId == subFlashcard.Id); subFlashcard.TrainLevel = flashcardResult.TrainLevel; subFlashcard.MarkedAsHard = subFlashcard.MarkedAsHard; subFlashcard.MarkedAsIgnored = subFlashcard.MarkedAsIgnored; subFlashcard.LastTrainingDate = DateTime.Now; } try { _context.SaveChanges(); output = true; } catch (Exception ex) { _logger.LogError("Error during save learn result", ex); } return(output); }
public async Task <bool> Create(int courseId, IEnumerable <Lesson> lessons) { using (var transaction = _context.Database.BeginTransaction()) { var course = _context.Courses.FirstOrDefault(x => x.Id == courseId); if (course == null) { _logger.LogWarning($"Course with given id { courseId } does not exists"); throw new CourseNotFoundException(); } try { foreach (var lesson in lessons) { lesson.DateCreated = DateTime.Now; lesson.DateModified = DateTime.Now; course.Lessons.Add(lesson); } _context.SaveChanges(); await transaction.CommitAsync(); } catch (Exception ex) { transaction.Rollback(); _logger.LogError(ex, "An error occured during add range of lessons to course"); return(false); } } return(true); }
public async Task <bool> Create(int lessonId, IEnumerable <Flashcard> flashcards) { using (var transaction = _context.Database.BeginTransaction()) { var lesson = _context.Lessons.FirstOrDefault(x => x.Id == lessonId); if (lesson == null) { _logger.LogWarning($"Lesson with given id { lessonId } does not exists"); throw new LessonNotFoundException(); } try { foreach (var flashcard in flashcards) { flashcard.DateCreated = DateTime.Now; flashcard.DateModified = DateTime.Now; flashcard.LessonId = lessonId; _context.Flashcards.Add(flashcard); } _context.SaveChanges(); await transaction.CommitAsync(); } catch (Exception ex) { transaction.Rollback(); _logger.LogError(ex, "An error occurred during add range of flashcards to lesson"); return(false); } } return(true); }
public static void SeedDefaultCourse(FlashcardsDataModel context, int accountId) { if (!context.Courses.Any()) { var course = CreateDefaultCourse(); course.AccountCreatedId = accountId; context.Courses.Add(course); context.SaveChanges(); } }
public bool ClearCourseProgress(int subscribedCourseId, int accountId) { bool output = false; var subscribedCourse = _context.SubscribedCourses.FirstOrDefault(x => x.Id == subscribedCourseId && x.AccountId == accountId); if (subscribedCourse != null) { foreach (var subscribedLesson in subscribedCourse.Lessons) { subscribedLesson.OverallProgress = 0; subscribedLesson.LastTrainingDate = new DateTime(); foreach (var subscribedFlashcard in subscribedLesson.SubscribedFlashcards) { subscribedFlashcard.LastRevisionDate = new DateTime(); subscribedFlashcard.LastTrainingDate = new DateTime(); subscribedFlashcard.TrainLevel = 0; subscribedFlashcard.MarkedAsHard = false; subscribedFlashcard.MarkedAsIgnored = false; } } subscribedCourse.LastActivity = DateTime.Now; subscribedCourse.OverallProgress = 0; try { _context.SaveChanges(); output = true; } catch (Exception ex) { _logger.LogError("An error occured during save. Method name: ClearCourseProgress", ex); } } else { throw new SubscribedCourseNotFoundException(); } return(output); }
public static void SeedAdministratorsAndDefaultCourse(FlashcardsDataModel context) { if (!context.Users.Any()) { var defaultAdministrator = CreateDefaultAdministrator(); var superAdministrator = CreateSuperAdministrator(); context.Users.Add(defaultAdministrator); context.Users.Add(superAdministrator); context.SaveChanges(); SeedDefaultCourse(context, defaultAdministrator.UserInfo.Id); } }
public User Update(int userId, UserForUpdate userForUpdate) { var userFromRepo = _context.Users.Include(x => x.UserInfo).FirstOrDefault(x => x.Id == userId); if (userFromRepo != null) { userFromRepo.UserInfo.FirstName = userForUpdate.FirstName; userFromRepo.UserInfo.LastName = userForUpdate.LastName; userFromRepo.UserInfo.DisplayName = userForUpdate.DisplayName; userFromRepo.UserInfo.City = userForUpdate.City; userFromRepo.UserInfo.Country = userForUpdate.Country; _context.SaveChanges(); return(userFromRepo); } else { throw new UserNotFoundException(); } }
public bool ChangeCourseStatus(int id, CourseStatusEnum status) { var course = _context.Courses.FirstOrDefault(x => x.Id == id); if (course != null) { course.Status = status; try { _context.SaveChanges(); return(true); } catch (Exception ex) { _logger.LogError("Problem with save course", ex); return(false); } } else { return(false); } }