Пример #1
0
        public List <ListFavouriteTO> GetUserFavourites(string userName)
        {
            var listIds = new List <int>();

            var cacheItem = ApplicationCache <UserFavouriteListIdsTO> .GetCacheItem(d => d.UserName == userName);

            if (cacheItem != null)
            {
                listIds = cacheItem.ListIds;
            }
            else
            {
                listIds = _authRepository.GetUserFavourites(userName);
                ApplicationCache <UserFavouriteListIdsTO> .AddCacheItem(new UserFavouriteListIdsTO()
                {
                    UserName = userName,
                    ListIds  = listIds
                });
            }

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

            if (listIds.Count > 0)
            {
                var lists = _listRepository.GetMany(listIds);

                if (lists != null)
                {
                    Mapper.CreateMap <List, ListFavouriteTO>();
                    listsModel = Mapper.Map <List <List>, List <ListFavouriteTO> >(lists);
                }
            }

            return(listsModel);
        }
Пример #2
0
        public UserInfoTO GetUserInfo(string userName)
        {
            var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.UserName == userName);

            if (cacheItem != null)
            {
                return(cacheItem);
            }

            var user = _authRepository.FindUserByUserName(userName);

            if (user != null)
            {
                var userRoles = _authRepository.GetUserRoles(user.UserName);

                UserInfoTO userInfo = new UserInfoTO
                {
                    Id       = user.Id,
                    UserName = user.UserName,
                    Email    = user.Email,
                    Roles    = userRoles
                };

                ApplicationCache <UserInfoTO> .AddCacheItem(userInfo);

                return(userInfo);
            }
            return(null);
        }
Пример #3
0
        public bool ChangeEmail(ChangeEmailTO emailDetails)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var result = _authRepository.ChangeEmail(emailDetails.OldEmail, emailDetails.NewEmail);

                if (result)
                {
                    scope.Complete();
                    var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.Email == emailDetails.OldEmail);

                    if (cacheItem != null)
                    {
                        var newCacheItem = new UserInfoTO()
                        {
                            Id       = cacheItem.Id,
                            Email    = emailDetails.NewEmail,
                            UserName = cacheItem.UserName,
                            Roles    = cacheItem.Roles
                        };

                        ApplicationCache <UserInfoTO> .RemoveCacheItem(cacheItem);

                        ApplicationCache <UserInfoTO> .AddCacheItem(newCacheItem);
                    }

                    return(true);
                }

                return(false);
            }
        }
Пример #4
0
        public async Task <bool> RegisterUser(RegisterUserTO registerUserTO)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userModel = new ApplicationUser
                {
                    UserName = registerUserTO.UserName,
                    Email    = registerUserTO.Email
                };

                var resultUserId = await _authRepository.RegisterUser(userModel, registerUserTO.Password);

                if (resultUserId != "")
                {
                    scope.Complete();

                    ApplicationCache <UserInfoTO> .AddCacheItem(new UserInfoTO()
                    {
                        Id       = resultUserId,
                        UserName = registerUserTO.UserName,
                        Email    = registerUserTO.Email,
                        Roles    = new List <string>()
                    });

                    return(true);
                }

                return(false);
            }
        }
Пример #5
0
        private ListDescriptionTO getListDetails(string userName, int listId)
        {
            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);
                }
                // calculate votes count
                Dictionary <int, int> votes = _listRepository.CalculateVotes(list.Id, filmIds);

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

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

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

                ApplicationCache <ListDescriptionTO> .AddCacheItem(listModel);

                return(listModel);
            }

            return(null);
        }
Пример #6
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);
            }
        }
Пример #7
0
        private RatingShortDescriptionTO getRating(int listId, string userName)
        {
            var rating = _listRepository.GetRating(listId, userName);

            if (rating.List.Id != 0)
            {
                ApplicationCache <Rating> .AddCacheItem(rating);

                Mapper.CreateMap <Rating, RatingShortDescriptionTO>();

                var ratingModel = Mapper.Map <Rating, RatingShortDescriptionTO>(rating);
                ratingModel.UserName = userName;
                return(ratingModel);
            }

            return(null);
        }
Пример #8
0
        private bool checkIfListFilmRatingExists(int listId, int filmId, string userId)
        {
            bool voteExists = _listRepository.CheckIfListFilmVoteExists(listId, filmId, userId);

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

                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #9
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);
            }
        }
Пример #10
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;
                        }
                    }
                }
            }
        }
Пример #11
0
        public bool UpdateRate(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)
                        {
                            rate = cacheItem;
                        }
                        else
                        {
                            rate = _listRepository.GetRating(rating.ListId, rating.UserName);
                        }
                    }
                    else
                    {
                        rate = _listRepository.GetRating(rating.ListId, rating.UserName);
                    }


                    if (rate != null)
                    {
                        //remove old rating cache item
                        ApplicationCache <Rating> .RemoveCacheItem(rate);

                        rate.Rate = rating.Rate;
                        // add updated rating to cache
                        ApplicationCache <Rating> .AddCacheItem(rate);

                        //update cache in db
                        _listRepository.UpdateRate(rating.ListId, rating.UserName, rating.Rate);
                        scope.Complete();
                    }
                }

                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();
                    success = true;
                }
            }
            return(success);
        }