Esempio n. 1
0
        public async Task <IActionResult> Detail(int shelfId, string slug = "")
        {
            if (shelfId == 0)
            {
                return(RedirectToAction("Error", "Home"));
            }

            var userId = User.GetUserId();

            var shelf = _unitOfWork.Shelves.GetShelf(shelfId);

            if (shelf == null ||
                shelf.IsDeleted ||
                !shelf.IsPublic && shelf.CreatedById != userId ||
                (!string.IsNullOrEmpty(slug) && shelf.Slug != slug))
            {
                return(RedirectToAction("Error", "Home"));
            }

            var viewModel = new ShelfViewModel
            {
                ShowActions  = User.Identity.IsAuthenticated,
                IsShelfOwner = userId == shelf.CreatedById,
                Shelf        = shelf,
                Items        = new List <ItemViewModel>()
            };

            foreach (var item in shelf.Items)
            {
                var itemViewModel = new ItemViewModel()
                {
                    Item = item
                };

                if (!string.IsNullOrEmpty(item.CoverId))
                {
                    itemViewModel.CoverImageUrl = new Cloudinary().GetResource(item.CoverId)?.SecureUrl;
                }
                else
                {
                    switch (item.Type)
                    {
                    case ItemType.Book:
                        itemViewModel.CoverImageUrl = _unitOfWork.ItemBookDetails.GetBookDetailByItemId(item.Id)?.ImageLink;
                        break;

                    case ItemType.Game:
                        itemViewModel.CoverImageUrl = (await _rawgGamesClient.ReadAsync(item.RawgId.ToString()))?.Background_image.ToString();
                        break;
                    }
                }

                viewModel.Items.Add(itemViewModel);
            }

            return(View(viewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(BookFormViewModel viewModel, CancellationToken cancellationToken)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

            if (string.IsNullOrWhiteSpace(viewModel.Title) &&
                string.IsNullOrWhiteSpace(viewModel.GBookId) &&
                viewModel.ShelfId == 0)
            {
                return(BadRequest());
            }

            var userId = User.GetUserId();

            var item = new Item
            {
                CreationDate = DateTime.UtcNow,
                Title        = viewModel.Title,
                CreatedById  = userId
            };

            if (!string.IsNullOrWhiteSpace(viewModel.GBookId))
            {
                item.Type = ItemType.Book;
                var bookDetailFromDb = _unitOfWork.BookDetails.GetBookDetail(viewModel.GBookId);

                if (bookDetailFromDb != null)
                {
                    if (viewModel.ShelfId > 0)
                    {
                        var itemFromDb = _unitOfWork.ItemBookDetails.GetItemByBookDetailId(bookDetailFromDb.Id, userId);

                        if (itemFromDb != null)
                        {
                            if (itemFromDb.ShelfId == viewModel.ShelfId)
                            {
                                if (itemFromDb.IsDeleted)
                                {
                                    itemFromDb.Reactivate();

                                    _unitOfWork.Complete();

                                    return(Ok(itemFromDb.Id));
                                }

                                return(BadRequest("The book is already in the specified shelf."));
                            }
                        }
                    }

                    _unitOfWork.ItemBookDetails.Save(new ItemBookDetail(item, bookDetailFromDb));
                }
                else
                {
                    var volume = await _bookFinder.GetAsync(viewModel.GBookId);

                    if (volume != null)
                    {
                        item.Title = volume.VolumeInfo.Title;

                        var bookDetail = new BookDetail
                        {
                            Subtitle = volume.VolumeInfo.Subtitle != null && volume.VolumeInfo.Subtitle.Length > 255
                                ? volume.VolumeInfo.Subtitle.Substring(0, 255)
                                : volume.VolumeInfo.Subtitle,
                            Description =
                                volume.VolumeInfo.Description != null && volume.VolumeInfo.Description.Length > 1000
                                    ? volume.VolumeInfo.Description.Substring(0, 1000)
                                    : volume.VolumeInfo.Description,
                            GoogleBookId  = volume.Id,
                            PublishedYear =
                                volume.VolumeInfo.PublishedDate != null && volume.VolumeInfo.PublishedDate.Length >= 4
                                    ? Convert.ToInt16(volume.VolumeInfo.PublishedDate.Substring(0, 4))
                                    : (short?)null,
                            Publisher = volume.VolumeInfo.Publisher,
                            ImageLink = volume.VolumeInfo.ImageLinks != null
                                ? volume.VolumeInfo.ImageLinks.Thumbnail
                                : ""
                        };

                        _unitOfWork.ItemBookDetails.Save(new ItemBookDetail(item, bookDetail));

                        if (volume.VolumeInfo.Authors != null && volume.VolumeInfo.Authors.Any())
                        {
                            foreach (var authorName in volume.VolumeInfo.Authors)
                            {
                                var authorFromDb = _unitOfWork.Authors.GetAuthor(authorName);

                                Author author;

                                if (authorFromDb == null)
                                {
                                    author = new Author()
                                    {
                                        Name         = authorName,
                                        CreationDate = DateTime.UtcNow
                                    };
                                }
                                else
                                {
                                    author = authorFromDb;
                                }

                                _unitOfWork.BookAuthors.Save(new BookAuthor()
                                {
                                    BookDetail = bookDetail,
                                    Author     = author
                                });
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(viewModel.GameId))
            {
                if (viewModel.ShelfId > 0 && int.TryParse(viewModel.GameId, out var gameId))
                {
                    var itemFromDb = _unitOfWork.Items.GetItemByRawgId(gameId, viewModel.ShelfId);

                    if (itemFromDb != null)
                    {
                        if (itemFromDb.IsDeleted)
                        {
                            itemFromDb.Reactivate();

                            _unitOfWork.Complete();

                            return(Ok(itemFromDb.Id));
                        }

                        return(BadRequest("The game is already in the specified shelf."));
                    }
                }

                var game = await _gamesClient.ReadAsync(viewModel.GameId, cancellationToken);

                if (game != null)
                {
                    item.RawgId = game.Id;
                    item.Title  = game.Name;
                    item.Slug   = game.Slug;
                    item.Type   = ItemType.Game;
                }
            }

            if (viewModel.ShelfId > 0)
            {
                item.ShelfId = viewModel.ShelfId;

                var shelf = _unitOfWork.Shelves.GetShelf(item.ShelfId);

                shelf.UpdateDate = DateTime.UtcNow;
            }
            else
            {
                var shelf = new Shelf(userId, string.Format("{0}'s shelf", User.GetUserName()));

                var followers = _unitOfWork.Followings.GetFollowers(userId);

                shelf.Publish(followers);

                _unitOfWork.Shelves.Save(shelf);

                item.Shelf = shelf;
            }

            _unitOfWork.Items.Save(item);
            _unitOfWork.Complete();

            return(Ok(item.Id));
        }
Esempio n. 3
0
        public async Task <IActionResult> Detail(int shelfId, int itemId, string slug = "")
        {
            if (shelfId <= 0 || itemId <= 0)
            {
                return(RedirectToAction("Error", "Home"));
            }

            var userId = User.GetUserId();

            var shelf = _unitOfWork.Shelves.GetShelf(shelfId);

            if (shelf == null ||
                shelf.IsDeleted ||
                !shelf.IsPublic && shelf.CreatedById != userId)
            {
                return(RedirectToAction("Error", "Home"));
            }

            var item = _unitOfWork.Items.GetItem(itemId);

            if (item == null ||
                item.IsDeleted ||
                (!string.IsNullOrEmpty(slug) && item.Slug != slug))
            {
                return(RedirectToAction("Error", "Home"));
            }

            var viewModel = new ItemViewModel()
            {
                Item         = item,
                IsShelfOwner = item.Shelf.CreatedById == User.GetUserId()
            };

            switch (item.Type)
            {
            case ItemType.Book:
                viewModel.BookDetail    = _unitOfWork.ItemBookDetails.GetBookDetailByItemId(item.Id);
                viewModel.CoverImageUrl = viewModel.BookDetail?.ImageLink;
                break;

            case ItemType.Game:
                viewModel.GameDetail = await _rawgGamesClient.ReadAsync(item.RawgId.ToString());

                viewModel.CoverImageUrl = viewModel.GameDetail.Background_image.ToString();
                break;
            }

            if (!string.IsNullOrEmpty(item.CoverId))
            {
                var cloudinary = new Cloudinary();

                var coverImage = cloudinary.GetResource(item.CoverId);

                if (coverImage != null)
                {
                    viewModel.CoverImageUrl = coverImage.SecureUrl;
                }
            }

            return(View(viewModel));
        }