public List<Contract_AnimeGroup> GetAllGroups(int userID) { List<Contract_AnimeGroup> grps = new List<Contract_AnimeGroup>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { DateTime start = DateTime.Now; AnimeGroupRepository repGroups = new AnimeGroupRepository(); AnimeGroup_UserRepository repUserGroups = new AnimeGroup_UserRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); TimeSpan ts = DateTime.Now - start; logger.Info("GetAllGroups (Database) in {0} ms", ts.TotalMilliseconds); start = DateTime.Now; // user records AnimeGroup_UserRepository repGroupUser = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecordList = repGroupUser.GetByUserID(session, userID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User grpUser in userRecordList) dictUserRecords[grpUser.AnimeGroupID] = grpUser; foreach (AnimeGroup ag in allGrps) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(ag.AnimeGroupID)) userRec = dictUserRecords[ag.AnimeGroupID]; // calculate stats Contract_AnimeGroup contract = ag.ToContract(userRec); grps.Add(contract); } grps.Sort(); ts = DateTime.Now - start; logger.Info("GetAllGroups (Contracts) in {0} ms", ts.TotalMilliseconds); } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return grps; }
public string DeleteUser(int userID) { JMMUserRepository repUsers = new JMMUserRepository(); try { JMMUser jmmUser = repUsers.GetByID(userID); if (jmmUser == null) return "User not found"; // make sure that at least one user is an admin if (jmmUser.IsAdmin == 1) { bool adminExists = false; List<JMMUser> users = repUsers.GetAll(); foreach (JMMUser userOld in users) { if (userOld.IsAdmin == 1) { if (userOld.JMMUserID != jmmUser.JMMUserID) adminExists = true; } } if (!adminExists) return "At least one user must be an administrator"; } repUsers.Delete(userID); // delete all user records AnimeSeries_UserRepository repSeries = new AnimeSeries_UserRepository(); foreach (AnimeSeries_User ser in repSeries.GetByUserID(userID)) repSeries.Delete(ser.AnimeSeries_UserID); AnimeGroup_UserRepository repGroup = new AnimeGroup_UserRepository(); foreach (AnimeGroup_User grp in repGroup.GetByUserID(userID)) repGroup.Delete(grp.AnimeGroup_UserID); AnimeEpisode_UserRepository repEpisode = new AnimeEpisode_UserRepository(); foreach (AnimeEpisode_User ep in repEpisode.GetByUserID(userID)) repEpisode.Delete(ep.AnimeEpisode_UserID); VideoLocal_UserRepository repVids = new VideoLocal_UserRepository(); foreach (VideoLocal_User vid in repVids.GetByUserID(userID)) repVids.Delete(vid.VideoLocal_UserID); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); return ex.Message; } return ""; }
public List<Contract_AnimeEpisode> GetContinueWatchingFilter(int userID, int maxRecords) { List<Contract_AnimeEpisode> retEps = new List<Contract_AnimeEpisode>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { GroupFilterRepository repGF = new GroupFilterRepository(); JMMUserRepository repUsers = new JMMUserRepository(); JMMUser user = repUsers.GetByID(session, userID); if (user == null) return retEps; // find the locked Continue Watching Filter GroupFilter gf = null; List<GroupFilter> lockedGFs = repGF.GetLockedGroupFilters(session); if (lockedGFs != null) { // if it already exists we can leave foreach (GroupFilter gfTemp in lockedGFs) { if (gfTemp.FilterType == (int)GroupFilterType.ContinueWatching) { gf = gfTemp; break; } } } if (gf == null) return retEps; // Get all the groups // it is more efficient to just get the full list of groups and then filter them later AnimeGroupRepository repGroups = new AnimeGroupRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); // get all the user records AnimeGroup_UserRepository repUserRecords = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecords = repUserRecords.GetByUserID(session, userID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User userRec in userRecords) dictUserRecords[userRec.AnimeGroupID] = userRec; // get all the groups in this filter for this user HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[user.JMMUserID][gf.GroupFilterID]; List<Contract_AnimeGroup> comboGroups = new List<Contract_AnimeGroup>(); foreach (AnimeGroup grp in allGrps) { if (groups.Contains(grp.AnimeGroupID)) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(grp.AnimeGroupID)) userRec = dictUserRecords[grp.AnimeGroupID]; Contract_AnimeGroup rec = grp.ToContract(userRec); comboGroups.Add(rec); } } // apply sorting List<SortPropOrFieldAndDirection> sortCriteria = GroupFilterHelper.GetSortDescriptions(gf); comboGroups = Sorting.MultiSort<Contract_AnimeGroup>(comboGroups, sortCriteria); if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(user.JMMUserID)) && (StatsCache.Instance.StatUserGroupFilter[user.JMMUserID].ContainsKey(gf.GroupFilterID))) { AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); foreach (Contract_AnimeGroup grp in comboGroups) { List<AnimeSeries> sers = repSeries.GetByGroupID(session, grp.AnimeGroupID); // sort the series by air date List<SortPropOrFieldAndDirection> sortCriteria2 = new List<SortPropOrFieldAndDirection>(); sortCriteria2.Add(new SortPropOrFieldAndDirection("AirDate", false, SortType.eDateTime)); sers = Sorting.MultiSort<AnimeSeries>(sers, sortCriteria2); List<int> seriesWatching = new List<int>(); foreach (AnimeSeries ser in sers) { if (!user.AllowedSeries(ser)) continue; bool useSeries = true; if (seriesWatching.Count > 0) { if (ser.GetAnime().AnimeType == (int)enAnimeType.TVSeries) { // make sure this series is not a sequel to an existing series we have already added foreach (AniDB_Anime_Relation rel in ser.GetAnime().GetRelatedAnime()) { if (rel.RelationType.ToLower().Trim().Equals("sequel") || rel.RelationType.ToLower().Trim().Equals("prequel")) useSeries = false; } } } if (!useSeries) continue; Contract_AnimeEpisode ep = GetNextUnwatchedEpisode(session, ser.AnimeSeriesID, userID); if (ep != null) { retEps.Add(ep); // Lets only return the specified amount if (retEps.Count == maxRecords) return retEps; if (ser.GetAnime().AnimeType == (int)enAnimeType.TVSeries) seriesWatching.Add(ser.AniDB_ID); } } } } } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return retEps; }
public List<Contract_AnimeGroup> GetAnimeGroupsForFilter(int groupFilterID, int userID, bool getSingleSeriesGroups) { List<Contract_AnimeGroup> retGroups = new List<Contract_AnimeGroup>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { DateTime start = DateTime.Now; GroupFilterRepository repGF = new GroupFilterRepository(); JMMUserRepository repUsers = new JMMUserRepository(); JMMUser user = repUsers.GetByID(session, userID); if (user == null) return retGroups; GroupFilter gf = null; if (groupFilterID == -999) { // all groups gf = new GroupFilter(); gf.GroupFilterName = "All"; } else { gf = repGF.GetByID(session, groupFilterID); if (gf == null) return retGroups; } //Contract_GroupFilterExtended contract = gf.ToContractExtended(user); AnimeGroupRepository repGroups = new AnimeGroupRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); AnimeGroup_UserRepository repUserRecords = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecords = repUserRecords.GetByUserID(session, userID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User userRec in userRecords) dictUserRecords[userRec.AnimeGroupID] = userRec; TimeSpan ts = DateTime.Now - start; string msg = string.Format("Got groups for filter DB: {0} - {1} in {2} ms", gf.GroupFilterName, allGrps.Count, ts.TotalMilliseconds); logger.Info(msg); start = DateTime.Now; AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); List<AnimeSeries> allSeries = new List<AnimeSeries>(); if (getSingleSeriesGroups) allSeries = repSeries.GetAll(session); if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(user.JMMUserID)) && (StatsCache.Instance.StatUserGroupFilter[user.JMMUserID].ContainsKey(gf.GroupFilterID))) { HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[user.JMMUserID][gf.GroupFilterID]; foreach (AnimeGroup grp in allGrps) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(grp.AnimeGroupID)) userRec = dictUserRecords[grp.AnimeGroupID]; if (groups.Contains(grp.AnimeGroupID)) { Contract_AnimeGroup contractGrp = grp.ToContract(userRec); if (getSingleSeriesGroups) { if (contractGrp.Stat_SeriesCount == 1) { AnimeSeries ser = GetSeriesForGroup(grp.AnimeGroupID, allSeries); if (ser != null) contractGrp.SeriesForNameOverride = ser.ToContract(ser.GetUserRecord(session, userID)); } } retGroups.Add(contractGrp); } } } ts = DateTime.Now - start; msg = string.Format("Got groups for filter EVAL: {0} - {1} in {2} ms", gf.GroupFilterName, retGroups.Count, ts.TotalMilliseconds); logger.Info(msg); return retGroups; } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return retGroups; }
public List<MetroContract_Anime_Summary> GetAnimeContinueWatching(int maxRecords, int jmmuserID) { List<MetroContract_Anime_Summary> retAnime = new List<MetroContract_Anime_Summary>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { GroupFilterRepository repGF = new GroupFilterRepository(); JMMUserRepository repUsers = new JMMUserRepository(); JMMUser user = repUsers.GetByID(session, jmmuserID); if (user == null) return retAnime; // find the locked Continue Watching Filter GroupFilter gf = null; List<GroupFilter> lockedGFs = repGF.GetLockedGroupFilters(session); if (lockedGFs != null) { // if it already exists we can leave foreach (GroupFilter gfTemp in lockedGFs) { if (gfTemp.GroupFilterName.Equals(Constants.GroupFilterName.ContinueWatching, StringComparison.InvariantCultureIgnoreCase)) { gf = gfTemp; break; } } } if (gf == null) return retAnime; // Get all the groups // it is more efficient to just get the full list of groups and then filter them later AnimeGroupRepository repGroups = new AnimeGroupRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); // get all the user records AnimeGroup_UserRepository repUserRecords = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecords = repUserRecords.GetByUserID(session, jmmuserID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User userRec in userRecords) dictUserRecords[userRec.AnimeGroupID] = userRec; // get all the groups in this filter for this user HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[user.JMMUserID][gf.GroupFilterID]; List<Contract_AnimeGroup> comboGroups = new List<Contract_AnimeGroup>(); foreach (AnimeGroup grp in allGrps) { if (groups.Contains(grp.AnimeGroupID)) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(grp.AnimeGroupID)) userRec = dictUserRecords[grp.AnimeGroupID]; Contract_AnimeGroup rec = grp.ToContract(userRec); comboGroups.Add(rec); } } // apply sorting List<SortPropOrFieldAndDirection> sortCriteria = GroupFilterHelper.GetSortDescriptions(gf); comboGroups = Sorting.MultiSort<Contract_AnimeGroup>(comboGroups, sortCriteria); if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(user.JMMUserID)) && (StatsCache.Instance.StatUserGroupFilter[user.JMMUserID].ContainsKey(gf.GroupFilterID))) { AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); foreach (Contract_AnimeGroup grp in comboGroups) { JMMServiceImplementation imp = new JMMServiceImplementation(); foreach (AnimeSeries ser in repSeries.GetByGroupID(session, grp.AnimeGroupID)) { if (!user.AllowedSeries(ser)) continue; AnimeSeries_User serUser = ser.GetUserRecord(session, jmmuserID); Contract_AnimeEpisode ep = imp.GetNextUnwatchedEpisode(session, ser.AnimeSeriesID, jmmuserID); if (ep != null) { AniDB_Anime anidb_anime = ser.GetAnime(session); MetroContract_Anime_Summary summ = new MetroContract_Anime_Summary(); summ.AnimeID = ser.AniDB_ID; summ.AnimeName = ser.GetSeriesName(session); summ.AnimeSeriesID = ser.AnimeSeriesID; summ.BeginYear = anidb_anime.BeginYear; summ.EndYear = anidb_anime.EndYear; //summ.PosterName = anidb_anime.GetDefaultPosterPathNoBlanks(session); if (serUser != null) summ.UnwatchedEpisodeCount = serUser.UnwatchedEpisodeCount; else summ.UnwatchedEpisodeCount = 0; ImageDetails imgDet = anidb_anime.GetDefaultPosterDetailsNoBlanks(session); summ.ImageType = (int)imgDet.ImageType; summ.ImageID = imgDet.ImageID; retAnime.Add(summ); // Lets only return the specified amount if (retAnime.Count == maxRecords) return retAnime; } else logger.Info(string.Format("GetAnimeContinueWatching:Skipping Anime - no episodes: {0}", ser.AniDB_ID)); } } } } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return retAnime; }
public List<Contract_AnimeGroup> GetAllGroups(int userID) { List<Contract_AnimeGroup> grps = new List<Contract_AnimeGroup>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { AnimeGroupRepository repGroups = new AnimeGroupRepository(); AnimeGroup_UserRepository repUserGroups = new AnimeGroup_UserRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); // user records AnimeGroup_UserRepository repGroupUser = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecordList = repGroupUser.GetByUserID(session, userID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User grpUser in userRecordList) dictUserRecords[grpUser.AnimeGroupID] = grpUser; foreach (AnimeGroup ag in allGrps) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(ag.AnimeGroupID)) userRec = dictUserRecords[ag.AnimeGroupID]; // calculate stats Contract_AnimeGroup contract = ag.ToContract(userRec); contract.ServerPosterPath = ag.GetPosterPathNoBlanks(session); grps.Add(contract); } grps.Sort(); } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return grps; }
public List<Contract_AnimeEpisode> GetContinueWatchingFilter(int userID, int maxRecords) { List<Contract_AnimeEpisode> retEps = new List<Contract_AnimeEpisode>(); try { using (var session = JMMService.SessionFactory.OpenSession()) { GroupFilterRepository repGF = new GroupFilterRepository(); JMMUserRepository repUsers = new JMMUserRepository(); JMMUser user = repUsers.GetByID(session, userID); if (user == null) return retEps; // find the locked Continue Watching Filter GroupFilter gf = null; List<GroupFilter> lockedGFs = repGF.GetLockedGroupFilters(session); if (lockedGFs != null) { // if it already exists we can leave foreach (GroupFilter gfTemp in lockedGFs) { if (gfTemp.GroupFilterName.Equals(Constants.GroupFilterName.ContinueWatching, StringComparison.InvariantCultureIgnoreCase)) { gf = gfTemp; break; } } } if (gf == null) return retEps; // Get all the groups // it is more efficient to just get the full list of groups and then filter them later AnimeGroupRepository repGroups = new AnimeGroupRepository(); List<AnimeGroup> allGrps = repGroups.GetAll(session); // get all the user records AnimeGroup_UserRepository repUserRecords = new AnimeGroup_UserRepository(); List<AnimeGroup_User> userRecords = repUserRecords.GetByUserID(session, userID); Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>(); foreach (AnimeGroup_User userRec in userRecords) dictUserRecords[userRec.AnimeGroupID] = userRec; // get all the groups in this filter for this user HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[user.JMMUserID][gf.GroupFilterID]; List<Contract_AnimeGroup> comboGroups = new List<Contract_AnimeGroup>(); foreach (AnimeGroup grp in allGrps) { if (groups.Contains(grp.AnimeGroupID)) { AnimeGroup_User userRec = null; if (dictUserRecords.ContainsKey(grp.AnimeGroupID)) userRec = dictUserRecords[grp.AnimeGroupID]; Contract_AnimeGroup rec = grp.ToContract(userRec); comboGroups.Add(rec); } } // apply sorting List<SortPropOrFieldAndDirection> sortCriteria = GroupFilterHelper.GetSortDescriptions(gf); comboGroups = Sorting.MultiSort<Contract_AnimeGroup>(comboGroups, sortCriteria); if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(user.JMMUserID)) && (StatsCache.Instance.StatUserGroupFilter[user.JMMUserID].ContainsKey(gf.GroupFilterID))) { AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); foreach (Contract_AnimeGroup grp in comboGroups) { foreach (AnimeSeries ser in repSeries.GetByGroupID(session, grp.AnimeGroupID)) { if (!user.AllowedSeries(ser)) continue; Contract_AnimeEpisode ep = GetNextUnwatchedEpisode(session, ser.AnimeSeriesID, userID); if (ep != null) { retEps.Add(ep); // Lets only return the specified amount if (retEps.Count == maxRecords) return retEps; } } } } } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } return retEps; }