public async Task <IActionResult> Index(AddToWishListViewModel input)
        {
            string login = this.HttpContext.User.Claims.First(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier).Value;
            Guid   num   = dbContext.Users.Where(user => user.Login == login).Single().Id;

            if (!await this.addToWishListService.AddBook(input, this.HttpContext.User.GetId()))
            {
                input.Message = "Book exists on your wish list";
                return(this.View(input));
            }
            else
            {
                input.Message = "Book added successfully!";
                input.Success = true;
                return(this.View(input));
            }
        }
Example #2
0
        public async Task <bool> AddBook(AddToWishListViewModel book, Guid?user)
        {
            WishList userWishList = this.applicationDbContext.WishLists.FirstOrDefault(b => b.UserId == user); // whishlist from current user

            if (userWishList == null)
            {
                userWishList = new WishList()
                {
                    Id     = Guid.NewGuid(),
                    UserId = user.Value
                };
                this.applicationDbContext.Add(userWishList);
            }

            book.Name = book.Name.Trim();
            Book bok = this.applicationDbContext.Books.FirstOrDefault(b => b.Name == book.Name);

            if (bok != null)                                                                                                            //book already exists
            {
                if (await this.applicationDbContext.WishListBooks.AnyAsync(b => b.WishListId == userWishList.Id && b.BookId == bok.Id)) // exists in wishlist? throw error
                {
                    return(false);
                }
                else // if no, add to wishlist
                {
                    userWishList.WishListBooks.Add(new WishListBook()
                    {
                        Id     = Guid.NewGuid(),
                        BookId = bok.Id
                    });
                }
            }
            else // book doesnt exists, add to Book and wishlist
            {
                Book           bok2         = null;
                List <Subject> subjectslist = null;
                var            bookAPI      = await SearchBook(book.Name, book.Author);

                if (bookAPI != null)
                {
                    bok2         = this.applicationDbContext.Books.FirstOrDefault(b => b.Name == bookAPI.Title);
                    subjectslist =
                        this.applicationDbContext.Subjects.Where(s => bookAPI.Subjects.Contains(s.Name)).ToList();
                }

                if (bok2 == null) //book doesnt exists in database
                {
                    book.Created = "01.01." + book.Created;
                    DateTime d = DateTime.Parse(book.Created);
                    bok = new Book()
                    {
                        Id       = Guid.NewGuid(),
                        Name     = bookAPI?.Title ?? book.Name,
                        Author   = bookAPI?.Authors.FirstOrDefault().Name ?? book.Author,
                        Created  = bookAPI?.FirstPublishDate ?? d,
                        CoverUrl = bookAPI?.Covers.FirstOrDefault().ToString() ?? null,
                        Isbn     = bookAPI?.Key ?? null,
                        Subjects = subjectslist ?? null
                    };
                    this.applicationDbContext.Books.Add(bok);

                    userWishList.WishListBooks.Add(new WishListBook()
                    {
                        Id     = Guid.NewGuid(),
                        BookId = bok.Id
                    });
                }
                else //book exists in database
                {
                    if (await this.applicationDbContext.WishListBooks.AnyAsync(b => b.WishListId == userWishList.Id && b.BookId == bok2.Id)) // exists in wishlist? throw error
                    {
                        return(false);
                    }
                    else // if no, add to wishlist
                    {
                        userWishList.WishListBooks.Add(new WishListBook()
                        {
                            Id     = Guid.NewGuid(),
                            BookId = bok2.Id
                        });
                    }
                }
            }
            await this.applicationDbContext.SaveChangesAsync();

            return(true);
        }