public JsonResult GetBookPageData([FromBody] int?bookId)
        {
            if (bookId != null)
            {
                try
                {
                    Book book = _context.Books.Where(b => b.Id == bookId).Include(b => b.Picture).FirstOrDefault();


                    List <Comment> bookComments = _context.Comments.Where(c => c.BookId == book.Id).Include(c => c.User).OrderByDescending(c => c.CreatedOn).ToList();

                    List <CommentModel> bookComms = new List <CommentModel>();

                    if (bookComments != null && bookComments.Count > 0)
                    {
                        bookComments.ForEach(c =>
                        {
                            bookComms.Add(new CommentModel
                            {
                                Id            = c.Id,
                                BookId        = c.BookId,
                                CommentBody   = c.CommentBody,
                                UserId        = c.UserId,
                                UserFirstName = c.User.FirstName,
                                UserLastName  = c.User.LastName,
                                CreatedOn     = String.Format("{0:f}", c.CreatedOn)
                            });
                        });
                    }

                    List <FullBookInfoHistory> bookHistory = GetAllBookHistory(book.Id);

                    DetailedBookInfo dd = new DetailedBookInfo
                    {
                        Book         = book,
                        HistoryList  = bookHistory,
                        CommentsList = bookComms
                    };

                    dd.photoInBinary = book.Picture != null?PictureHelper.ConvertToString(book.Picture.ImageData) : null;


                    return(Json(dd));
                }
                catch (Exception ex)
                {
                    return(Json("Select book"));
                }
            }
            else
            {
                return(Json("Select book"));
            }
        }
        public async Task <JsonResult> GetAllUserBooks()
        {
            try
            {
                var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value;

                var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == int.Parse(userId));

                if (user != null)
                {
                    List <Book> userBooks = await _context.Books.Where(
                        b => b.CurrentUserId == user.Id)
                                            .Include(b => b.Picture)
                                            .OrderByDescending(b => b.CreatedOn)
                                            .ToListAsync();

                    List <BookOnShelf> booksOnShelf = new List <BookOnShelf>();

                    foreach (var book in userBooks)
                    {
                        booksOnShelf.Add(new BookOnShelf()
                        {
                            Id            = book.Id,
                            AmazonId      = book.AmazonId,
                            Title         = book.Title,
                            Author        = book.Author,
                            ConributorId  = book.ContributorId,
                            CurrentUserId = book.CurrentUserId,
                            Description   = book.Description,
                            CreatedOn     = book.CreatedOn,
                            PrintedOn     = book.PrintedOn,
                            GenreId       = book.GenreId,
                            ISBN          = book.ISBN,
                            IsUsable      = book.IsUsable,
                            Price         = book.Price,
                            PhotoInBinary = book.Picture != null ? PictureHelper.ConvertToString(book.Picture.ImageData) : null
                        });
                    }

                    return(Json(booksOnShelf));
                }
                else
                {
                    throw new Exception("User was not found");
                }
            }
            catch (Exception e)
            {
                return(Json("Error: " + e.Message));
            }
        }
        public JsonResult GetUserInfo()
        {
            try
            {
                var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value;

                var user = _context.Users.Where(u => u.Id == int.Parse(userId))
                           .Include(u => u.Picture)
                           .Include(u => u.RatingStatus)
                           .FirstOrDefault();

                if (user != null)
                {
                    UserInfo uInfo = new UserInfo
                    {
                        Id               = user.Id,
                        FirstName        = user.FirstName,
                        LastName         = user.LastName,
                        Email            = user.Email,
                        PhoneNumber      = user.PhoneNumber,
                        RoleName         = user.Role != null ? user.Role.Name : "Guest",
                        AvailableFrom    = user.AvailableFrom,
                        AvailableTill    = user.AvailableTill,
                        BirthDate        = user.BirthDate,
                        RegisteredOn     = user.RegisteredOn,
                        RatingStatusName = user.RatingStatus != null ? user.RatingStatus.Name : null,
                        PhotoinBinary    = user.Picture != null?PictureHelper.ConvertToString(user.Picture.ImageData) : null
                    };

                    return(Json(uInfo));
                }
                return(Json("Error"));
            }
            catch (Exception ex)
            {
                return(Json("Error"));
            }
        }
Beispiel #4
0
        public JsonResult Order([FromBody] int bookId)
        {
            try
            {
                var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value;
                var user   = _context.Users.FirstOrDefault(u => u.Id == int.Parse(userId));

                var book = _context.Books.FirstOrDefault(b => b.Id == bookId);

                var currentBookOwner = _context.Users.FirstOrDefault(u => u.Id == book.CurrentUserId);

                if (book != null && user != null)
                {
                    Deal deal = new Deal
                    {
                        DonorId      = book.CurrentUserId,
                        AcceptorId   = user.Id,
                        BookId       = book.Id,
                        DealStatusId = (int?)DealHelper.Status.OPENED,
                        CreatedOn    = DateTime.UtcNow,
                        ExpiredOn    = DateTime.UtcNow.AddDays(_context.DealStatuses.FirstOrDefault(d => d.Id == (int?)DealHelper.Status.OPENED).ExpirationTime),
                        ModifiedOn   = DateTime.UtcNow,
                        EndedOn      = null
                    };

                    _context.Deals.Add(deal);
                    _context.SaveChanges();

                    BookForOrder bookForOrder = new BookForOrder
                    {
                        Id                                                           = book.Id,
                        Title                                                        = book.Title,
                        Author                                                       = book.Author,
                        Description                                                  = book.Description,
                        BookTypeId                                                   = book.BookTypeId,
                        GenreId                                                      = book.GenreId,
                        CreatedOn                                                    = book.CreatedOn,
                        PrintedOn                                                    = book.PrintedOn,
                        photoInBinnary                                               = book.Picture != null?PictureHelper.ConvertToString(book.Picture.ImageData) : null,
                                                                    Price            = book.Price,
                                                                    OwnerId          = book.CurrentUserId,
                                                                    OwnerFirstName   = book.CurrentUser.FirstName,
                                                                    OwnerLastName    = book.CurrentUser.LastName,
                                                                    OwnerPhoneNumber = book.CurrentUser.PhoneNumber,
                                                                    OwnerEmail       = book.CurrentUser.Email
                    };

                    if (!String.IsNullOrEmpty(currentBookOwner.Email))
                    {
                        GmailSender.SmtpClientLibrary.SendOrderRequestMessages(currentBookOwner.Email,
                                                                               responceDictionary["AskDonor"], "Book Donation", "mail", "pass");
                    }
                    if (!String.IsNullOrEmpty(user.Email))
                    {
                        GmailSender.SmtpClientLibrary.SendOrderRequestMessages(user.Email,
                                                                               responceDictionary["AskAcceptor"], "Book Donation", "mail", "pass");
                    }

                    return(Json(bookForOrder));
                }
                else
                {
                    return(Json("Error: " + "Not found"));
                }
            }
            catch (Exception e)
            {
                return(Json("Error: " + e.Message));
            }
        }