public int Add(Course course, List<int> wordSuitesId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.Courses.Add(course);
         context.SaveChanges();
         AddWordSuitesToCourse(course.Id, wordSuitesId);
         context.SaveChanges();
         return course.Id;
     }
 }
 public void Delete(int courseId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.Courses.Remove(context.Courses.First(x => x.Id == courseId));
         context.SaveChanges();
     }
 }
 public void Add(IncomingUser user)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.IncomingUsers.AddOrUpdate(user);
         context.SaveChanges();
     }
 }
 public int Add(WordSuite wordSuite)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.WordSuites.AddOrUpdate(wordSuite);
         context.SaveChanges();
         return wordSuite.Id;
     }
 }
 public int Add(WordTranslation wordTranslation)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.WordTranslations.Add(wordTranslation);
         context.SaveChanges();
         return wordTranslation.Id;
     }
 }
Exemple #6
0
 public int Add(Tag tag)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         context.Tags.Add(tag);
         context.SaveChanges();
         return tag.Id;
     }
 }
 public void ChangePassword(ForgotPasswordUserModel model)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var targetUser =
                 context.Users.First(user => user.Id.ToString() == model.Id);
         targetUser.Password = _passwordHasher.HashPassword(model.Password);
         context.SaveChanges();
     }
 }
 public bool Add(GroupModel groupModel)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var newGroup = _mapper.MapToGroup(groupModel);
         context.Groups.Add(newGroup);
         context.SaveChanges();
         return true;
     }
 }
 public bool IncrementProgress(int wordSuiteId, int wordTranslationId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var wordProgress = context.WordProgresses.First(x => (x.WordSuiteId == wordSuiteId
                     && x.WordTranslationId == wordTranslationId));
         ++(wordProgress.Progress);
         context.WordProgresses.AddOrUpdate(wordProgress);
         context.SaveChanges();
         return true;
     }
 }
 public bool Delete(int id)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var wordProgresses = context.WordProgresses;
         wordProgresses.RemoveRange(wordProgresses.Where(wp => wp.WordSuiteId == id));
         var wordSuites = context.WordSuites;
         wordSuites.Remove(wordSuites.FirstOrDefault(ws => ws.Id == id));
         context.SaveChanges();
         return true;
     }
 }
Exemple #11
0
 public void AddToken(IncomingUser user)
 {
     if (Exists(user))
     {
         using (var context = new WorldOfWordsDatabaseContext())
         {
             var searchedUser = context.IncomingUsers.First(e => e.Id == user.Id);
             searchedUser.Token = user.Token;
             context.SaveChanges();
         }
     }
 }
Exemple #12
0
 public int Add(Word word)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         if (Exists(word.Value, word.LanguageId) > 0)
         {
             return -1;
         }
         context.Words.Add(word);
         context.SaveChanges();
         return word.Id;
     }
 }
 public bool AddRange(List<WordProgress> wordProgressRange)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var wordProgresses = context.WordProgresses;
         foreach (var wordProgress in wordProgressRange)
         {
             wordProgresses.AddOrUpdate(wordProgress);
         }
         context.SaveChanges();
         return true;
     }
 }
 public void EditCourse(Course courseModel, List<int> wordSuitesId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var course = context.Courses.First(x => x.Id == courseModel.Id);
         course.Name = courseModel.Name;
         var wordSuites = course.WordSuites.
             Where(ws => (!wordSuitesId.Contains(ws.Id) && ws.PrototypeId == null)).ToList();
         RemoveWordSuitesFromCourse(course.Id, wordSuites);
         wordSuitesId.RemoveAll(x => course.WordSuites.Select(ws => ws.Id).Contains(x));
         AddWordSuitesToCourse(course.Id, wordSuitesId);
         context.SaveChanges();
     }
 }
 public bool EnrollUsersToGroup(List<User> users, int groupId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var enrollmentsToAdd = users.Select(user => new Enrollment
         {
             GroupId = groupId,
             UserId = user.Id,
             Date = DateTime.Now
         }).ToList();
         context.Enrollments.AddRange(enrollmentsToAdd);
         context.SaveChanges();
         return true;
     }
 }
 /// <summary>
 /// Adds a new language to the database.
 /// </summary>
 /// <param name="language">The language to be added to the database.</param>
 /// <returns>The id of a new database record, or -1, if such language already exists.</returns>
 public int AddLanguage(Language language)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         if (context
             .Languages
             .Any(l => l.Name == language.Name))
         {
             return -1;
         }
         context.Languages.Add(language);
         context.SaveChanges();
         return language.Id;
     }
 }
Exemple #17
0
 public bool ChangeRolesOfUser(User user)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         User userToChange = context.Users.Include("Roles").Where(item => item.Id == user.Id).FirstOrDefault();
         userToChange.Roles = new List<Role>();
         var queryOfNeededIds = user.Roles.Select(item => item.Id);
         var toAdd = context.Roles.Where(item => queryOfNeededIds.Contains(item.Id)).ToList();
         foreach (var a in toAdd)
         {
             userToChange.Roles.Add(a);
         }
         context.SaveChanges();
     }
     return true;
 }
 public bool DeleteById(int enrollmentId)
 {
     try
     {
         using (var context = new WorldOfWordsDatabaseContext())
         {
             context.Enrollments.Remove(context.Enrollments.FirstOrDefault(e => e.Id == enrollmentId));
             context.SaveChanges();
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
        /// <summary>
        /// Removes language from the database.
        /// </summary>
        /// <param name="id"> ID of the language to be removed from the database.</param>
        /// <returns>True, if language successfully deleted, false, if no language with such ID</returns>
        public bool RemoveLanguage(int id)
        {
            using (var context = new WorldOfWordsDatabaseContext())
            {
                var language = context.Languages.SingleOrDefault(l => l.Id == id);

                if (language != null)
                {
                    if (language.Courses.Count == 0)
                    {
                        if (language.WordSuites.Count != 0)
                        {
                            var wordSuites = context.WordSuites.Where(ws => ws.LanguageId == id);
                            var wordProgresses = context.WordProgresses.Where(wp => wordSuites.Select(ws => ws.Id).Contains(wp.WordSuiteId));
                            context.WordProgresses.RemoveRange(wordProgresses);
                            context.WordSuites.RemoveRange(wordSuites);
                        }
                        if (language.Words.Count != 0)
                        {
                            var wordTranslations = context.WordTranslations.Where(wt => wt.OriginalWord.LanguageId == id);
                            context.WordTranslations.RemoveRange(wordTranslations);

                            var words = context.Words.Where(w => w.LanguageId == id);
                            context.Words.RemoveRange(words);
                        }

                        context.Languages.Remove(language);
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
 public bool CopyWordsuitesForUsersByGroup(List<User> users, int groupId)
 {
     try
     {
         using (var context = new WorldOfWordsDatabaseContext())
         {
             Group group = context.Groups.FirstOrDefault(g => g.Id == groupId);
             if (group == null)
             {
                 return false;
             }
             List<WordSuite> wordsuitesToCopy = group.Course.WordSuites.Where(w => w.PrototypeId == null).ToList();
             List<WordSuite> wordsuitesToAdd = new List<WordSuite>();
             foreach (var user in users)
             {
                 wordsuitesToAdd.AddRange(wordsuitesToCopy.Select(w => new WordSuite
                     {
                         Name = w.Name,
                         LanguageId = w.LanguageId,
                         Threshold = w.Threshold,
                         QuizResponseTime = w.QuizResponseTime,
                         QuizStartTime = null,
                         OwnerId = user.Id,
                         PrototypeId = w.Id,
                         Courses = new[] { group.Course }
                     }));
             }
             context.WordSuites.AddRange(wordsuitesToAdd);
             context.SaveChanges();
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
 public bool CopyProgressesForUsersInGroup(List<User> users, int groupId)
 {
     try
     {
         using(var context = new WorldOfWordsDatabaseContext())
         {
             var group = context.Groups.FirstOrDefault(g => g.Id == groupId);
             if(group == null)
             {
                 return false;
             }
             var originalWordsuites = group.Course.WordSuites.Where(w => w.PrototypeId == null);
             var progressesToAdd = new List<WordProgress>();
             foreach(var user in users)
             {
                 foreach(var wordsuite in originalWordsuites)
                 {
                     var copiedWordSuite = group.Course.WordSuites.FirstOrDefault(w => w.PrototypeId == wordsuite.Id && w.OwnerId == user.Id);
                     progressesToAdd.AddRange(copiedWordSuite.PrototypeWordSuite.WordProgresses.Select(wp => new WordProgress
                         {
                             WordSuiteId = copiedWordSuite.Id,
                             WordTranslationId = wp.WordTranslationId,
                             Progress = 0
                         }));
                 }
             }
             context.WordProgresses.AddRange(progressesToAdd);
             context.SaveChanges();
         }
         return true;
     }
     catch
     {
         return false;
     }
 }
Exemple #22
0
 public bool EditUserPassword(string newPassword, int userId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var user = context.Users.FirstOrDefault(us => us.Id == userId);
         if (user != null)
         {
             user.Password = _passwordHasher.HashPassword(newPassword);
             context.SaveChanges();
             return true;
         }
         return false;
     }
 }
Exemple #23
0
 public bool CheckUserAuthorization(UserWithPasswordModel userLoginApi)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var user = context.Users.FirstOrDefault(u => ((u.Email).ToLower() == (userLoginApi.Email).ToLower()));
         if ((user != null) && (VerifyPasswords(user.Password, userLoginApi.Password)))
         {
             userLoginApi.Id = user.Id;
             userLoginApi.Roles = user.Roles.Select(x => x.Name);
             user.HashedToken = _token.Sha256Hash(_token.RandomString(ConstContainer.HashLength));
             userLoginApi.HashToken = user.HashedToken;
             context.Users.AddOrUpdate(user);
             context.SaveChanges();
             return true;
         }
         return false;
     }
 }
 public void SetTime(int id)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var suite = context
             .WordSuites
             .First(ws => ws.Id == id);
         suite.QuizStartTime = DateTime.Now;
         context.SaveChanges();
     }
 }
 private void RemoveWordSuitesFromCourse(int courseId, List<WordSuite> wordSuites)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var course = context.Courses.First(x => x.Id == courseId);
         foreach (var wordSuite in wordSuites)
         {
             course.WordSuites.Remove(wordSuite);
         }
         context.SaveChanges();
     }
 }
 private void AddWordSuitesToCourse(int courseId, List<int> wordSuitesId)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         var course = context.Courses.First(x => x.Id == courseId);
         foreach (var wordSuiteId in wordSuitesId)
         {
             course.WordSuites.Add(context.WordSuites.FirstOrDefault(x => x.Id == wordSuiteId));
         }
         context.SaveChanges();
     }
 }
        public bool Update(WordSuite wordSuite)
        {
            using (var context = new WorldOfWordsDatabaseContext())
            {
                var oldWordSuite = context.WordSuites.First(ws => ws.Id == wordSuite.Id);
                oldWordSuite.Name = wordSuite.Name;
                oldWordSuite.Threshold = wordSuite.Threshold;
                oldWordSuite.QuizResponseTime = wordSuite.QuizResponseTime;

                context.WordSuites.AddOrUpdate(oldWordSuite);
                context.SaveChanges();
            }
            return true;
        }
 public bool RemoveByStudent(WordProgress wordProgress)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         if (IsStudentWord(wordProgress))
         {
             context
             .WordProgresses
             .Remove(context
                 .WordProgresses
                 .Single(wp => wp.WordSuiteId == wordProgress.WordSuiteId &&
                               wp.WordTranslationId == wordProgress.WordTranslationId));
             context.SaveChanges();
             return true;
         }
         return false;
     }
 }
 public bool RemoveRange(List<WordProgress> wordProgressRange)
 {
     using (var context = new WorldOfWordsDatabaseContext())
     {
         wordProgressRange.ForEach(wp =>
             context
             .WordProgresses
             .Remove(context
                     .WordProgresses
                     .Single(dbWp => dbWp.WordSuiteId == wp.WordSuiteId
                                     && dbWp.WordTranslationId == wp.WordTranslationId)));
         context.SaveChanges();
     }
     return true;
 }
 public bool RemoveProgressesForEnrollment(int enrollmentId)
 {
     using(var context = new WorldOfWordsDatabaseContext())
     {
         try
         {
             Enrollment enrollment = context.Enrollments.FirstOrDefault(e => e.Id == enrollmentId);
             if (enrollment == null)
             {
                 return false;
             }
             List<int> originalWordsuitesIds = enrollment.Group.Course.WordSuites.Where(w => w.PrototypeId == null).Select(w => w.Id).ToList();
             var wordsuitesToDeleteFrom = enrollment.Group.Course.WordSuites
                 .Where(w => w.OwnerId == enrollment.User.Id && w.PrototypeId != null
                     && originalWordsuitesIds.Contains((int)w.PrototypeId)).ToList();
             context.WordProgresses.RemoveRange(wordsuitesToDeleteFrom.SelectMany(ws => ws.WordProgresses));
             context.SaveChanges();
             return true;
         }
         catch
         {
             return false;
         }
     }
 }