Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public bool DeleteList(int listId)
        {
            List list = new List();

            if (listId > 0)
            {
                using (var scope = new TransactionScope())
                {
                    var cachedList = ApplicationCache <ListDescriptionTO> .GetCacheItem(d => d.Id == listId);

                    if (cachedList != null)
                    {
                        ApplicationCache <ListDescriptionTO> .RemoveCacheItem(cachedList);
                    }

                    var cachedRating = ApplicationCache <ListFilmRating> .GetCacheItem(d => d.ListId == listId);

                    if (cachedRating != null)
                    {
                        ApplicationCache <ListFilmRating> .RemoveCacheItem(cachedRating);
                    }

                    bool result = _listRepository.Delete(listId);
                    if (result == true)
                    {
                        scope.Complete();
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
        public FilmEntityTO GetFilmById(int filmId)
        {
            var film = new Film();

            var cacheItem = ApplicationCache <Film> .GetCacheItem(d => d.Id == filmId);

            if (cacheItem != null)
            {
                film = cacheItem;
            }
            else
            {
                film = _filmRepository.GetSingleFilm(filmId);
            }

            if (film.Id != 0)
            {
                var actors    = _filmRepository.GetFilmActors(filmId);
                var directors = _filmRepository.GetFilmDirectors(filmId);

                film.Actors    = actors;
                film.Directors = directors;

                Mapper.CreateMap <Actor, ActorEntityTO>();
                Mapper.CreateMap <Director, DirectorEntityTO>();
                Mapper.CreateMap <Film, FilmEntityTO>();

                var filmModel = Mapper.Map <Film, FilmEntityTO>(film);
                return(filmModel);
            }
            return(null);
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public bool CheckIfEmailExists(string email)
        {
            var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.Email == email);

            if (cacheItem != null)
            {
                return(true);
            }
            return(_authRepository.CheckIfEmailExists(email));
        }
Exemplo n.º 13
0
        public ListDescriptionTO GetListById(string userName, int listId)
        {
            ListDescriptionTO result = new ListDescriptionTO();

            var cacheItem = ApplicationCache <ListDescriptionTO> .GetCacheItem(d => d.Id == listId);

            if (cacheItem != null)
            {
                result = cacheItem;
            }
            else
            {
                result = getListDetails(userName, listId);
            }

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

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

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

            return(result);
        }
Exemplo n.º 14
0
        public bool CheckIfFilmExists(int filmId)
        {
            var cacheItem = ApplicationCache <Film> .GetCacheItem(d => d.Id == filmId);

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

            return(_filmRepository.CheckIfFilmExists(filmId));
        }
Exemplo n.º 15
0
        public string GetUserEmail(string userName)
        {
            var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.UserName == userName);

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

            return(_authRepository.GetUserEmail(userName));
        }
Exemplo n.º 16
0
        public bool CheckIfListFilmRatingExists(int listId, int filmId, string userId)
        {
            var cacheItem = ApplicationCache <ListFilmRating> .GetCacheItem(d => d.ListId == listId && d.FilmId == filmId && d.UserId == userId);

            if (cacheItem != null)
            {
                return(true);
            }
            else
            {
                return(checkIfListFilmRatingExists(listId, filmId, userId));
            }
        }
Exemplo n.º 17
0
        public async Task <bool> CheckIfUserExists(string userName)
        {
            var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.UserName == userName);

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

            var result = await _authRepository.CheckIfUserExists(userName);

            return((result) ? true : false);
        }
Exemplo n.º 18
0
        public string GetListCreator(int listId)
        {
            string result = "";

            var cacheItem = ApplicationCache <ListDescriptionTO> .GetCacheItem(d => d.Id == listId);

            if (cacheItem != null)
            {
                result = cacheItem.User.UserName;
            }
            else
            {
                result = _listRepository.GetListCreator(listId);
            }

            return(result);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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);
            }
        }
Exemplo n.º 21
0
        public bool DeleteUser(string userName)
        {
            var result = _authRepository.DeleteUser(userName);

            if (result)
            {
                var cacheItem = ApplicationCache <UserInfoTO> .GetCacheItem(d => d.UserName == userName);

                if (cacheItem != null)
                {
                    ApplicationCache <UserInfoTO> .RemoveCacheItem(cacheItem);
                }
                return(true);
            }

            return(false);
        }
Exemplo n.º 22
0
        public RatingShortDescriptionTO GetRating(int listId, string userName)
        {
            var result = new RatingShortDescriptionTO();

            var cacheItem = ApplicationCache <Rating> .GetCacheItem(d => d.Id == listId && d.User.UserName == userName);

            if (cacheItem != null)
            {
                Mapper.CreateMap <Rating, RatingShortDescriptionTO>();

                result = Mapper.Map <Rating, RatingShortDescriptionTO>(cacheItem);
            }
            else
            {
                result = getRating(listId, userName);
            }

            return(result);
        }
Exemplo n.º 23
0
        public bool CheckIfListExists(int listId)
        {
            var cacheItem = ApplicationCache <ListDescriptionTO> .GetCacheItem(d => d.Id == listId);

            if (cacheItem != null)
            {
                return(true);
            }
            else
            {
                var list = _listRepository.GetSingle(listId);

                if (list != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 24
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);
        }
Exemplo n.º 25
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);
            }
        }
Exemplo n.º 26
0
        public bool CheckIfFavouriteExists(int listId, string userName)
        {
            var cacheItem = ApplicationCache <UserFavouriteListIdsTO> .GetCacheItem(d => d.UserName == userName);

            if (cacheItem != null)
            {
                bool listExists = cacheItem.ListIds.Contains(listId);
                if (listExists == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            var userId = _authRepository.GetUserId(userName);

            return(_authRepository.CheckIfFavouriteExists(listId, userId));
        }
Exemplo n.º 27
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);
            }
        }
Exemplo n.º 28
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);
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
0
        public List <ListUserCreated> GetUserCreatedLists(string userName)
        {
            var listsModel = new List <ListUserCreated>();

            var cacheItems = ApplicationCache <ListDescriptionTO> .GetCacheItems(d => d.User.UserName == userName);

            if (cacheItems.Count > 0)
            {
                Mapper.CreateMap <ListDescriptionTO, ListUserCreated>();
                listsModel = Mapper.Map <List <ListDescriptionTO>, List <ListUserCreated> >(cacheItems);
            }
            else
            {
                var lists = _authRepository.GetUserCreatedLists(userName);

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

            return(listsModel);
        }