Beispiel #1
0
        public List <UserInfoTO> GetAllUsers()
        {
            var cache = ApplicationCache <UserInfoTO> .GetCache();

            if (cache.Count > 0)
            {
                return(cache);
            }

            List <ApplicationUser> users = _authRepository.GetAllUsers();

            List <UserInfoTO> usersModel = new List <UserInfoTO>();

            if (users != null)
            {
                Mapper.CreateMap <ApplicationUser, UserInfoTO>();
                usersModel = Mapper.Map <List <ApplicationUser>, List <UserInfoTO> >(users);
            }

            foreach (var user in usersModel)
            {
                user.Roles = _authRepository.GetUserRoles(user.UserName);
            }

            ApplicationCache <UserInfoTO> .FillCache(usersModel);

            return(usersModel);
        }
Beispiel #2
0
        public bool AddRating(int listId, int rate, string userId)
        {
            if (rate == -1 || rate == 1)
            {
                using (var scope = new TransactionScope())
                {
                    _listRepository.AddRating(listId, rate, userId);
                    scope.Complete();
                }

                var cache = ApplicationCache <ListDescriptionTO> .GetCache();

                if (cache.Count() > 0)
                {
                    var cacheItem = cache.FirstOrDefault(d => d.Id == listId);
                    if (cacheItem != null)
                    {
                        //update likes
                        cacheItem.Likes = _listRepository.GetLikesCount(listId);
                        //update dislikes
                        cacheItem.DisLikes = _listRepository.GetDislikesCount(listId);
                        var listIndex = cache.FindIndex(d => d.Id == listId);
                        // replace object
                        cache[listIndex] = cacheItem;

                        ApplicationCache <ListDescriptionTO> .FillCache(cache);
                    }
                }
                return(true);
            }
            return(false);
        }
Beispiel #3
0
        public bool RemoveUserFavourite(int listId, string userName)
        {
            bool status = _authRepository.RemoveUserFavourite(listId, userName);

            if (status == true)
            {
                var cache = ApplicationCache <UserFavouriteListIdsTO> .GetCache();

                if (cache.Count > 0)
                {
                    var cacheItem = ApplicationCache <UserFavouriteListIdsTO> .GetCacheItem(d => d.UserName == userName);

                    if (cacheItem != null)
                    {
                        int cacheItemIndex = cache.FindIndex(d => d.UserName == userName);
                        cacheItem.ListIds.Remove(listId);
                        cache[cacheItemIndex] = cacheItem;
                        ApplicationCache <UserFavouriteListIdsTO> .FillCache(cache);
                    }
                }

                return(true);
            }
            return(false);
        }
Beispiel #4
0
        public bool DeleteRating(RatingShortDescriptionTO rating)
        {
            var success = false;

            if (rating.Rate == -1 || rating.Rate == 1)
            {
                using (var scope = new TransactionScope())
                {
                    var rate = new Rating();

                    var cache = ApplicationCache <Rating> .GetCache();

                    if (cache.Count() > 0)
                    {
                        var cacheItem = cache.Where(d => d.List.Id == rating.ListId && d.User.UserName == rating.UserName).FirstOrDefault();

                        if (cacheItem != null)
                        {
                            ApplicationCache <Rating> .RemoveCacheItem(cacheItem);
                        }
                    }

                    success = _listRepository.DeleteRating(rating.ListId, rating.UserName);
                    scope.Complete();
                }

                if (success == true)
                {
                    using (var scope = new TransactionScope())
                    {
                        _listRepository.UpdateLikesDislikesCount(rating.ListId, false, false, true);

                        //update cache ListDescriptionTO
                        var cache = ApplicationCache <ListDescriptionTO> .GetCache();

                        if (cache.Count() > 0)
                        {
                            var cacheItem = cache.FirstOrDefault(d => d.Id == rating.ListId);
                            if (cacheItem != null)
                            {
                                //update likes
                                cacheItem.Likes = _listRepository.GetLikesCount(rating.ListId);
                                //update dislikes
                                cacheItem.DisLikes = _listRepository.GetDislikesCount(rating.ListId);
                                var listIndex = cache.FindIndex(d => d.Id == rating.ListId);
                                // replace object
                                cache[listIndex] = cacheItem;

                                ApplicationCache <ListDescriptionTO> .FillCache(cache);
                            }
                        }

                        scope.Complete();
                    }
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
        public bool AddListToFavourites(string userName, int listId)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (userName != "")
                {
                    var result = _authRepository.AddListToFavourites(userName, listId);

                    if (result)
                    {
                        scope.Complete();

                        var cache = ApplicationCache <UserFavouriteListIdsTO> .GetCache();

                        if (cache != null)
                        {
                            var cacheItem = ApplicationCache <UserFavouriteListIdsTO> .GetCacheItem(d => d.UserName == userName);

                            if (cacheItem != null)
                            {
                                var cacheItemIndex = cache.FindIndex(d => d.UserName == userName);
                                cacheItem.ListIds.Add(listId);
                                cache[cacheItemIndex] = cacheItem;
                                ApplicationCache <UserFavouriteListIdsTO> .FillCache(cache);
                            }
                            else
                            {
                                var newUserFavs = new UserFavouriteListIdsTO()
                                {
                                    UserName = userName,
                                    ListIds  = new List <int>()
                                };
                                newUserFavs.ListIds.Add(listId);

                                ApplicationCache <UserFavouriteListIdsTO> .AddCacheItem(newUserFavs);
                            }
                        }

                        return(true);
                    }

                    return(false);
                }
                return(false);
            }
        }
Beispiel #6
0
        public List <UserIdNameTO> GetAllUsersNames()
        {
            List <UserIdNameTO> usersModel = new List <UserIdNameTO>();
            var cache = ApplicationCache <UserInfoTO> .GetCache();

            if (cache.Count > 0)
            {
                Mapper.CreateMap <UserInfoTO, UserIdNameTO>();
                usersModel = Mapper.Map <List <UserInfoTO>, List <UserIdNameTO> >(cache);

                return(usersModel);
            }

            List <ApplicationUser> users = _authRepository.GetAllUsers();

            Mapper.CreateMap <ApplicationUser, UserIdNameTO>();
            usersModel = Mapper.Map <List <ApplicationUser>, List <UserIdNameTO> >(users);

            return(usersModel);
        }
Beispiel #7
0
        public bool DeleteFilmVote(int listId, int filmId, string userId)
        {
            var success = false;

            if (listId > 0)
            {
                using (var scope = new TransactionScope())
                {
                    _listRepository.DeleteFilmVote(listId, filmId, userId);
                    scope.Complete();
                    success = true;

                    var cache = ApplicationCache <ListDescriptionTO> .GetCache();

                    if (cache.Count() > 0)
                    {
                        // find list
                        var list      = cache.Find(d => d.Id == listId);
                        var listIndex = cache.FindIndex(d => d.Id == listId);
                        // find film
                        list.Films.Where(d => d.Id == filmId).FirstOrDefault().Votes  -= 1;
                        list.Films.Where(d => d.Id == filmId).FirstOrDefault().isVoted = false;
                        // replace object
                        cache[listIndex] = list;

                        ApplicationCache <ListDescriptionTO> .FillCache(cache);

                        var listFilmRatingCache = ApplicationCache <ListFilmRating> .GetCache();

                        var cacheItem = listFilmRatingCache.SingleOrDefault(d => d.ListId == listId && d.FilmId == filmId && d.UserId == userId);
                        if (cacheItem != null)
                        {
                            listFilmRatingCache.Remove(cacheItem);
                        }
                        ApplicationCache <ListFilmRating> .FillCache(listFilmRatingCache);
                    }
                }
            }
            return(success);
        }
Beispiel #8
0
        public bool AddFilmVote(int listId, int filmId, string userId)
        {
            try
            {
                using (var scope = new TransactionScope())
                {
                    _listRepository.AddFilmVote(listId, filmId, userId);
                    scope.Complete();

                    var cache = ApplicationCache <ListDescriptionTO> .GetCache();

                    if (cache.Count() > 0)
                    {
                        // find list
                        var list      = cache.Find(d => d.Id == listId);
                        var listIndex = cache.FindIndex(d => d.Id == listId);
                        // find film
                        list.Films.Where(d => d.Id == filmId).FirstOrDefault().Votes  += 1;
                        list.Films.Where(d => d.Id == filmId).FirstOrDefault().isVoted = true;
                        // replace object
                        cache[listIndex] = list;

                        ApplicationCache <ListDescriptionTO> .FillCache(cache);

                        ApplicationCache <ListFilmRating> .AddCacheItem(new ListFilmRating()
                        {
                            ListId = listId,
                            FilmId = filmId,
                            UserId = userId
                        });
                    }

                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }
Beispiel #9
0
        public IEnumerable <FilmShortDescriptionTO> GetAllFilms()
        {
            var films = new List <Film>();
            var cache = ApplicationCache <Film> .GetCache();

            if (cache.Count() > 0)
            {
                films = cache;
            }
            else
            {
                films = _filmRepository.GetAll().ToList();
                ApplicationCache <Film> .FillCache(films);
            }

            List <FilmShortDescriptionTO> filmsModel = new List <FilmShortDescriptionTO>();

            if (films.Any())
            {
                Mapper.CreateMap <Film, FilmShortDescriptionTO>();
                filmsModel = Mapper.Map <List <Film>, List <FilmShortDescriptionTO> >(films);
            }
            return(filmsModel);
        }
Beispiel #10
0
        public bool UpdateFilm(int filmId, FilmEntityTO filmEntity)
        {
            var success = false;

            // check post message if is not empty
            if (filmEntity != null)
            {
                using (var scope = new TransactionScope())
                {
                    // check if given film exists in database
                    // zmienic na metode getbyId....
                    var film = _filmRepository.GetSingleFilm(filmId);

                    if (film != null)
                    {
                        if ((film.Name != filmEntity.Name) && filmEntity.Name != null)
                        {
                            film.Name = filmEntity.Name;
                        }
                        if ((film.OriginalName != filmEntity.OriginalName) && filmEntity.OriginalName != null)
                        {
                            film.OriginalName = filmEntity.OriginalName;
                        }
                        if ((film.ReleaseDate != filmEntity.ReleaseDate) && filmEntity.ReleaseDate != null)
                        {
                            film.ReleaseDate = filmEntity.ReleaseDate;
                        }
                        if ((film.Genre != filmEntity.Genre) && filmEntity.Genre != null)
                        {
                            film.Genre = filmEntity.Genre;
                        }
                        if ((film.Description != filmEntity.Description) && filmEntity.Description != null)
                        {
                            film.Description = filmEntity.Description;
                        }
                        if ((film.Cover != filmEntity.Cover) && filmEntity.Cover != null)
                        {
                            film.Cover = filmEntity.Cover;
                        }
                        if ((film.FilmwebUrl != filmEntity.FilmwebUrl) && filmEntity.FilmwebUrl != null)
                        {
                            film.FilmwebUrl = filmEntity.FilmwebUrl;
                        }

                        _filmRepository.UpdateFilm(film);
                        scope.Complete();
                        success = true;
                    }

                    // update cache
                    var cache = ApplicationCache <Film> .GetCache();

                    if (cache.Count() > 0)
                    {
                        var cacheItem = ApplicationCache <Film> .GetCacheItem(d => d.Id == filmId);

                        if (cacheItem != null)
                        {
                            int cacheItemIndex = cache.FindIndex(d => d.Id == filmId);

                            #region update properties
                            if ((film.Name != filmEntity.Name) && filmEntity.Name != null)
                            {
                                cacheItem.Name = filmEntity.Name;
                            }
                            if ((film.OriginalName != filmEntity.OriginalName) && filmEntity.OriginalName != null)
                            {
                                cacheItem.OriginalName = filmEntity.OriginalName;
                            }
                            if ((film.ReleaseDate != filmEntity.ReleaseDate) && filmEntity.ReleaseDate != null)
                            {
                                cacheItem.ReleaseDate = filmEntity.ReleaseDate;
                            }
                            if ((film.Genre != filmEntity.Genre) && filmEntity.Genre != null)
                            {
                                cacheItem.Genre = filmEntity.Genre;
                            }
                            if ((film.Description != filmEntity.Description) && filmEntity.Description != null)
                            {
                                cacheItem.Description = filmEntity.Description;
                            }
                            if ((film.Cover != filmEntity.Cover) && filmEntity.Cover != null)
                            {
                                cacheItem.Cover = filmEntity.Cover;
                            }
                            if ((film.FilmwebUrl != filmEntity.FilmwebUrl) && filmEntity.FilmwebUrl != null)
                            {
                                cacheItem.FilmwebUrl = filmEntity.FilmwebUrl;
                            }
                            #endregion

                            cache[cacheItemIndex] = cacheItem;
                            ApplicationCache <Film> .FillCache(cache);
                        }
                    }
                }
            }
            return(success);
        }
Beispiel #11
0
        private void addNewestListToCache(int listId, bool?isUpdate)
        {
            var list = _listRepository.GetSingle(listId);

            if (list != null)
            {
                list.Films = _listRepository.GetListFilms(listId);

                Mapper.CreateMap <Film, FilmListDetailsTO>();
                Mapper.CreateMap <ApplicationUser, UserNameTO>();
                Mapper.CreateMap <List, ListDescriptionTO>();

                var listModel = Mapper.Map <List, ListDescriptionTO>(list);

                foreach (var film in listModel.Films)
                {
                    string[] shortDesc = film.Description.Split(' ');
                    Array.Resize(ref shortDesc, 15);
                    film.Description = string.Join(" ", shortDesc);
                }

                List <int> filmIds = new List <int>();
                foreach (var film in listModel.Films)
                {
                    filmIds.Add(film.Id);
                }

                foreach (var film in listModel.Films)
                {
                    film.Votes   = 0;
                    film.isVoted = false;
                }

                listModel.Films.OrderByDescending(d => d.Votes);

                var user = new UserNameTO
                {
                    UserName = _listRepository.GetListCreator(listId)
                };
                listModel.User     = user;
                listModel.Likes    = 0;
                listModel.DisLikes = 0;
                listModel.Voted    = 0;

                var cache = ApplicationCache <ListDescriptionTO> .GetCache();

                if (isUpdate == false)
                {
                    if (cache.Count() > 0)
                    {
                        ApplicationCache <ListDescriptionTO> .AddCacheItem(listModel);
                    }
                }
                else
                {
                    if (cache.Count() > 0)
                    {
                        var cacheItem = cache.FirstOrDefault(d => d.Id == listId);
                        if (cacheItem != null)
                        {
                            int cacheItemIndex = cache.IndexOf(cacheItem);
                            cache[cacheItemIndex] = listModel;
                        }
                    }
                }
            }
        }
Beispiel #12
0
        public SearchTO Search(string searchString, string userName)
        {
            var films = new List <Film>();
            List <FilmShortDescriptionTO> filmsModel = new List <FilmShortDescriptionTO>();

            var filmsCache = ApplicationCache <Film> .GetCache();

            if (searchString.ToLower().Contains("filmy z"))
            {
                // get actor name
                var actorName = searchString.Remove(0, 8);
                // get search actor id
                var filmIds = _filmRepository.GetActorFilmIds(actorName);
                // get films with mentioned above actor
                if (filmIds.Count > 0)
                {
                    films = _filmRepository.GetMany(filmIds);
                }
            }
            else
            {
                var cachedFilms        = filmsCache.Where(d => d.Name.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
                var cachedFilmsByGenre = filmsCache.Where(d => d.Genre.IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0).Take(30).ToList();
                if (cachedFilms.Count > 0)
                {
                    films = cachedFilms;
                    // get count of search films
                    int filmsCount = films.Count;
                    // calculate how many films we need to download (by genre)
                    int additionalFilmsGet = 30 - filmsCount;
                    if (additionalFilmsGet > 0)
                    {
                        films.AddRange(cachedFilmsByGenre.Take(additionalFilmsGet));
                    }
                }
                else if (cachedFilmsByGenre.Count > 0)
                {
                    films = cachedFilmsByGenre;
                }
                else
                {
                    films = _listRepository.SearchFilms(searchString);
                }
            }

            foreach (var film in films)
            {
                filmsModel.Add(new FilmShortDescriptionTO()
                {
                    Id           = film.Id,
                    Name         = film.Name,
                    OriginalName = film.OriginalName,
                    ReleaseDate  = film.ReleaseDate,
                    Genre        = film.Genre,
                    Description  = film.Description,
                    Cover        = film.Cover,
                    FilmwebUrl   = film.FilmwebUrl
                });
            }

            List <ListSearchDetailsTO> listsResult = new List <ListSearchDetailsTO>();
            // get lists
            var lists = _listRepository.SearchLists(searchString);

            foreach (var list in lists)
            {
                // get list films
                list.Films = _listRepository.GetListFilms(list.Id);

                // map List to ListSearchDetailsTO
                var singleListDetails = new ListSearchDetailsTO();
                singleListDetails.Id       = list.Id;
                singleListDetails.Name     = list.Name;
                singleListDetails.Likes    = list.Likes;
                singleListDetails.DisLikes = list.Dislikes;
                singleListDetails.Films    = new List <FilmSearchDetailsTO>();

                // calculate votes count for each film
                List <int> filmIds = new List <int>();
                foreach (var film in list.Films)
                {
                    filmIds.Add(film.Id);
                }
                Dictionary <int, int> votes = _listRepository.CalculateVotes(singleListDetails.Id, filmIds);

                foreach (var film in list.Films)
                {
                    FilmSearchDetailsTO singleFilmSearchDetails = new FilmSearchDetailsTO();
                    singleFilmSearchDetails.Id           = film.Id;
                    singleFilmSearchDetails.Name         = film.Name;
                    singleFilmSearchDetails.OriginalName = film.OriginalName;
                    singleFilmSearchDetails.ReleaseDate  = film.ReleaseDate;
                    singleFilmSearchDetails.Genre        = film.Genre;
                    singleFilmSearchDetails.Description  = film.Description;
                    singleFilmSearchDetails.Cover        = film.Cover;
                    singleFilmSearchDetails.FilmwebUrl   = film.FilmwebUrl;

                    if (votes.ContainsKey(film.Id))
                    {
                        singleFilmSearchDetails.Votes = votes[film.Id];
                    }
                    else
                    {
                        singleFilmSearchDetails.Votes = 0;
                    }

                    singleListDetails.Films.Add(singleFilmSearchDetails);
                }

                listsResult.Add(singleListDetails);
            }

            if (userName != "")
            {
                string userId = "";
                //get user id
                userId = _authRepository.GetUserId(userName);

                foreach (var list in listsResult)
                {
                    List <int> votedFilmsIds = new List <int>();
                    if (userId != "")
                    {
                        //check if user already voted for films
                        votedFilmsIds = _listRepository.CheckIfFilmsAreVoted(list.Id, userId);
                        // check if user already liked/dislikes list
                        list.Voted = _listRepository.CheckIfListIsVoted(list.Id, userId);
                        // check if user already added list to favourites
                        list.IsFavourite = _authRepository.CheckIfFavouriteExists(list.Id, userId);

                        foreach (var film in list.Films)
                        {
                            if (votedFilmsIds.Contains(film.Id))
                            {
                                film.isVoted = true;
                            }
                            else
                            {
                                film.isVoted = false;
                            }
                        }
                    }
                    list.Films.OrderByDescending(d => d.Votes);
                }
            }

            listsResult.OrderByDescending(d => d.Likes);

            var searchResult = new SearchTO()
            {
                SearchString = searchString,
                Films        = filmsModel,
                Lists        = listsResult
            };

            return(searchResult);
        }
Beispiel #13
0
        public IEnumerable <ListDescriptionTO> GetAllLists(string userName)
        {
            List <ListDescriptionTO> result = new List <ListDescriptionTO>();

            var cache = ApplicationCache <ListDescriptionTO> .GetCache();

            if (cache.Count() > 0)
            {
                result = cache;
                foreach (var list in result)
                {
                    var filmsToSort = list.Films.ToList();
                    filmsToSort.Sort((x, y) => y.Votes - x.Votes);
                    list.Films = filmsToSort;
                }
            }
            else
            {
                var lists = _listRepository.GetAll();

                List <ListDescriptionTO> listsModel = new List <ListDescriptionTO>();

                if (lists.Any())
                {
                    foreach (var list in lists)
                    {
                        list.Films = _listRepository.GetListFilms(list.Id);
                    }

                    Mapper.CreateMap <Film, FilmListDetailsTO>();
                    Mapper.CreateMap <ApplicationUser, UserNameTO>();
                    Mapper.CreateMap <List, ListDescriptionTO>();

                    listsModel = Mapper.Map <List <List>, List <ListDescriptionTO> >(lists);

                    foreach (var list in listsModel)
                    {
                        foreach (var film in list.Films)
                        {
                            string[] shortDesc = film.Description.Split(' ');
                            Array.Resize(ref shortDesc, 15);
                            film.Description = string.Join(" ", shortDesc);
                        }

                        List <int> filmIds = new List <int>();
                        foreach (var film in list.Films)
                        {
                            filmIds.Add(film.Id);
                        }
                        // calculate votes count
                        Dictionary <int, int> votes = _listRepository.CalculateVotes(list.Id, filmIds);

                        foreach (var film in list.Films)
                        {
                            if (votes.ContainsKey(film.Id))
                            {
                                film.Votes = votes[film.Id];
                            }
                            else
                            {
                                film.Votes = 0;
                            }
                        }

                        var user = new UserNameTO
                        {
                            UserName = _listRepository.GetListCreator(list.Id)
                        };
                        list.User = user;
                        list.Films.OrderByDescending(d => d.Votes);
                    }
                }
                ApplicationCache <ListDescriptionTO> .FillCache(listsModel);

                result = listsModel;
            }

            if (userName != "")
            {
                string userId = "";
                //get user id
                userId = _authRepository.GetUserId(userName);

                foreach (var list in result)
                {
                    List <int> votedFilmsIds = new List <int>();
                    if (userId != "")
                    {
                        //check if user already voted for films
                        votedFilmsIds = _listRepository.CheckIfFilmsAreVoted(list.Id, userId);
                        // check if user already liked/dislikes list
                        list.Voted = _listRepository.CheckIfListIsVoted(list.Id, userId);
                        // check if user already added list to favourites
                        list.IsFavourite = _authRepository.CheckIfFavouriteExists(list.Id, userId);

                        foreach (var film in list.Films)
                        {
                            if (votedFilmsIds.Contains(film.Id))
                            {
                                film.isVoted = true;
                            }
                            else
                            {
                                film.isVoted = false;
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var list in result)
                {
                    foreach (var film in list.Films)
                    {
                        film.isVoted = false;
                    }
                    list.Voted = 0;
                }
            }

            result.OrderByDescending(d => d.Likes);

            return(result);
        }