Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteFavorite(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Book book = await _db.Books.FindAsync(id);

            if (book == null)
            {
                return(NotFound());
            }
            AppUser user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                return(NotFound());
            }
            FavoriteBook favoriteBook = _db.FavoriteBooks.FirstOrDefault(fb => fb.AppUserId == user.Id && fb.BookId == book.Id);

            _db.FavoriteBooks.Remove(favoriteBook);
            await _db.SaveChangesAsync();

            return(RedirectToAction(User.Identity.Name, "profil"));
        }
        /// <summary>
        /// Will apply the "filter" just favorites books or all
        /// </summary>
        /// <param name="isToggled"></param>
        public void OnFilterSwitch(bool isToggled)
        {
            ///if the toggle is true, will be generate an collection of favorite books

            if (isToggled)
            {
                if (FavoritePairedBooks == null || FavoritePairedBooks.Count() != FavoriteBook.Count())
                {
                    if (FavoriteBook == null)
                    {
                        IsLabelVisible = true;
                        isListVisible  = !IsLabelVisible;
                    }
                    else
                    {
                        isListVisible  = true;
                        IsLabelVisible = !isListVisible;
                    }
                    FavoritePairedBooks = CreateBookPairs(FavoriteBook);
                }
                BookList = FavoritePairedBooks;
            }
            ///if false the main list will be set with the collection of all books
            else
            {
                BookList = AllBooks;
            }
        }
        public FavoriteBook PutFavorite(FavoriteBook favoriteBook)
        {
            var entity = _context.UserBooks.SingleOrDefault(item => item.Id == favoriteBook.Id && item.UserId == _userId);

            if (entity == null)
            {
                UserBook newBook = new UserBook
                {
                    Id         = favoriteBook.Id,
                    IsFavorite = favoriteBook.IsFavorite,
                    UserId     = _userId
                };
                _context.UserBooks.Add(newBook);
            }
            else
            {
                entity.IsFavorite = favoriteBook.IsFavorite;
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw;
            }

            return(favoriteBook);
        }
Ejemplo n.º 4
0
 public IActionResult Favorite(int id)
 {
     try
     {
         var          userID     = int.Parse(_userManager.GetUserId(HttpContext.User));//Alterar passar id da sessao
         var          bookExiste = _context.FavoriteBooks.FirstOrDefault(x => x.BookID == id && x.UserID == userID);
         FavoriteBook favorite   = new FavoriteBook();
         if (bookExiste == null)
         {
             favorite.BookID          = id;
             favorite.FavoriteDate    = DateTime.Now;
             favorite.UserID          = userID;
             favorite.FavoriteEnabled = true;
             _context.FavoriteBooks.Add(favorite);
         }
         else
         {
             bookExiste.FavoriteEnabled = !bookExiste.FavoriteEnabled;
             _context.FavoriteBooks.Update(bookExiste);
         }
         //var bookLike = await _context.BookLike.FindAsync(id);
         _context.SaveChanges();
         return(Json("Marcado como favorito!"));
     }
     catch (Exception ex)
     {
         return(Json(ex.Message));
     }
 }
Ejemplo n.º 5
0
        public IList <long> CreateFavoriteBook(long bookId, string title, IList <long> labelIds)
        {
            var now  = DateTime.UtcNow;
            var user = TryGetUser();
            var book = m_favoritesRepository.Load <Book>(bookId);

            var labels           = GetFavoriteLabelsAndCheckAuthorization(labelIds, user.Id);
            var labelsDictionary = labels.ToDictionary(x => x.Id);
            var itemsToSave      = new List <FavoriteBook>();

            foreach (var labelId in labelIds)
            {
                var label = labelsDictionary[labelId];
                label.LastUseTime = now;

                var favoriteItem = new FavoriteBook
                {
                    Book          = book,
                    CreateTime    = now,
                    User          = user,
                    FavoriteLabel = label,
                    Title         = title
                };
                itemsToSave.Add(favoriteItem);
            }

            var result = m_favoritesRepository.CreateAll(itemsToSave);

            return(result.Cast <long>().ToList());
        }
        public async Task <IActionResult> PutFavoriteBook([FromRoute] int id, [FromBody] FavoriteBook favoriteBook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != favoriteBook.id)
            {
                return(BadRequest());
            }

            _context.Entry(favoriteBook).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FavoriteBookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostFavoriteBook([FromBody] FavoriteBook favoriteBook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.FavoriteBook.Add(favoriteBook);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetFavoriteBook", new { id = favoriteBook.id }, favoriteBook));
        }
Ejemplo n.º 8
0
        public async Task DeleteBookFromFavoritesAsync(FavoriteBook book)
        {
            var entity = await dbContext.Set <FavoriteBook>()
                         .Where(fb => fb.BookId == book.BookId && fb.UserId == book.UserId)
                         .FirstOrDefaultAsync();

            if (entity != null)
            {
                dbContext.Set <FavoriteBook>().Remove(entity);
                await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Ejemplo n.º 9
0
        public FavoriteBook Builder()
        {
            var favoriteBook = new FavoriteBook(bookId: _bookId.HasValue() ? _bookId : "test",
                                                link: _link.HasValue() ? _link : "test",
                                                title: _title.HasValue() ? _title : "test",
                                                thumbnail: _thumbnail.HasValue() ? _thumbnail : "test",
                                                description: _description.HasValue() ? _description : "dest",
                                                authors: _authors.HasValue() ? _authors : "test",
                                                user: _user != null ? _user : new UserBuilder().Builder());

            favoriteBook.WithId();

            return(favoriteBook);
        }
Ejemplo n.º 10
0
        public void Add(FavoriteBookDto dto)
        {
            if (!dto.IsValid())
            {
                NotifyValidationError(dto);
                return;
            }

            var authors      = string.Join(";", dto.Authors.Select(x => x));
            var favoriteBook = new FavoriteBook(bookId: dto.BookId, link: dto.Link, title: dto.Title, thumbnail: dto.Thumbnail, description: dto.Description, authors: authors, user: dto.User);

            _favoriteBookRepository.Add(favoriteBook);
            Commit();
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> AddFavorite(int?id, string actionName, string queryString)
        {
            if (id == null)
            {
                return(NotFound());
            }
            Book book = await _db.Books.FindAsync(id);

            if (book == null)
            {
                return(NotFound());
            }
            AppUser user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                return(NotFound());
            }
            FavoriteBook fbook        = _db.FavoriteBooks.FirstOrDefault(fb => fb.AppUserId == user.Id && fb.BookId == book.Id);
            FavoriteBook favoriteBook = new FavoriteBook();

            if (fbook == null)
            {
                favoriteBook.BookId    = book.Id;
                favoriteBook.AppUserId = user.Id;
                await _db.FavoriteBooks.AddAsync(favoriteBook);

                await _db.SaveChangesAsync();
            }
            if (actionName.Contains("/kitab/"))
            {
                return(RedirectToRoute(new
                {
                    controller = "kitab",
                    action = book.Slug
                }));
            }
            else if (actionName.Contains("BestSellers"))
            {
                return(Redirect("/BestSellers/Index" + queryString));
            }
            else
            {
                return(Redirect("/NewBooks/Index" + queryString));
            }
        }
Ejemplo n.º 12
0
        public virtual IList <FavoriteBook> GetFavoriteLabeledBooks(IList <long> bookIds, int userId)
        {
            FavoriteBook  favoriteItemAlias  = null;
            FavoriteLabel favoriteLabelAlias = null;

            using (var session = GetSession())
            {
                return(session.QueryOver(() => favoriteItemAlias)
                       .JoinAlias(() => favoriteItemAlias.FavoriteLabel, () => favoriteLabelAlias)
                       .Fetch(x => x.FavoriteLabel).Eager
                       .WhereRestrictionOn(() => favoriteItemAlias.Book.Id).IsInG(bookIds)
                       .And(() => favoriteLabelAlias.User.Id == userId)
                       .OrderBy(() => favoriteLabelAlias.Name).Asc
                       .OrderBy(() => favoriteItemAlias.Title).Asc
                       .List());
            }
        }
Ejemplo n.º 13
0
        public async Task <bool> MarkAsFavorite(long book, long user)
        {
            try {
                var fav = await context.FavoriteBooks
                          .Where(f => f.UserId == user && f.BookId == book)
                          .SingleOrDefaultAsync();

                if (fav == null && user != 0 && book != 0)
                {
                    var favorite = new FavoriteBook(user, book);
                    await context.FavoriteBooks.AddAsync(favorite);
                }
                return((await context.SaveChangesAsync()) > 0);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
Ejemplo n.º 14
0
        public async Task <ActionResult <UserDetailedViewModel> > RemoveBook(BaseViewModel bookId)
        {
            var userId       = AuthHelper.GetUserId(User);
            var favoriteBook = new FavoriteBook
            {
                UserId = userId,
                BookId = bookId.Id
            };

            await userService.DeleteBookFromFavoritesAsync(favoriteBook);

            var userViewModel = await GetCurrentUserAsync();

            if (userViewModel == null)
            {
                return(BadRequest());
            }

            return(Ok(userViewModel));
        }
        public async Task AddFavoriteBook(FavoriteBookDto input)
        {
            List <string> errorList = new List <string>();

            long userID = AbpSession.UserId.Value;

            var getData = _favoriteBookRepository.GetAll()
                          .Where(x => x.LibraryId == input.LibraryId && x.BookId == input.BookId && x.UserId == userID)
                          .FirstOrDefault();

            if (getData == null)
            {
                var favoriteBook = new FavoriteBook
                {
                    UserId        = userID,
                    BookLibraryId = input.BookLibraryId,
                    BookId        = input.BookId,
                    LibraryId     = input.LibraryId
                };

                FavoriteBookValidator validator        = new FavoriteBookValidator();
                ValidationResult      validationResult = validator.Validate(favoriteBook);

                if (!validationResult.IsValid)
                {
                    foreach (var failure in validationResult.Errors)
                    {
                        errorList.Add(string.Format("{0}", failure.ErrorMessage));
                    }
                    string errorString = string.Join(" ", errorList.ToArray());
                    throw new UserFriendlyException(errorString);
                }
                await _favoriteBookRepository.InsertAsync(favoriteBook);
            }
            else
            {
                throw new UserFriendlyException(string.Format("The book is already in the favorites list!"));
            }
        }
Ejemplo n.º 16
0
        public async Task AddBookToFavoritesAsync(FavoriteBook book)
        {
            await dbContext.Set <FavoriteBook>().AddAsync(book).ConfigureAwait(false);

            await dbContext.SaveChangesAsync().ConfigureAwait(false);
        }
Ejemplo n.º 17
0
 public void Delete([FromBody] FavoriteBook favoriteBook)
 {
     favoriteBookRepository.Remove(favoriteBook);
 }
Ejemplo n.º 18
0
 public void Add([FromBody] FavoriteBook favoriteBook)
 {
     favoriteBookRepository.Add(favoriteBook);
 }