Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
 public void InitializeFilmsCache()
 {
     ApplicationCache <Film> .FillCache(_filmRepository.GetAll().ToList());
 }
Esempio n. 11
0
 public void InitializeUsersCache()
 {
     ApplicationCache <UserInfoTO> .FillCache(GetAllUsers());
 }
Esempio n. 12
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);
        }
Esempio n. 13
0
 public void InitializeListsCache()
 {
     ApplicationCache <ListDescriptionTO> .FillCache(GetAllLists("").ToList());
 }