Exemplo n.º 1
0
        //[Authorize(Roles = "Admin")]
        public IHttpActionResult PostBook(Book book)
        {
            book.Rate = 0;
            var LoggedInUserId = UserUtilities.GetCurrentUserId(User);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.Books.Add(book);
            if (book.Want.Equals("have"))
            {
                db.UserHaveBooks.Add(new UserHaveBook()
                {
                    UserId = LoggedInUserId, BookId = book.Book_Id, BookConditionId = BookConditionEnum.New
                });
            }
            else if (book.Want.Equals("want"))
            {
                var user = db.Users.Where(user2 => user2.UserId == LoggedInUserId).FirstOrDefault();
                //book.UserWantBooks.Add(user);
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception e)
            {
                return(Ok(e.Message));
            }

            return(CreatedAtRoute("DefaultApi", new { id = book.Book_Id }, book));
        }
        public IHttpActionResult GetRecievedRequests(int pageNumber, int pageSize)
        {
            var currentUSerId = UserUtilities.GetCurrentUserId(User);

            pageNumber = pageNumber == 0 ? 1 : 1;
            var requests = db.Requests
                           //.Include("Book")
                           .Where(req => req.RecieverId == currentUSerId)
                           .OrderByDescending(req => req.DateOfMessage)
                           .Skip((pageNumber - 1) * pageSize)
                           .Take(pageSize)

                           .Select(req => new { req.Id,
                                                req.DateOfMessage,
                                                req.SenderId,
                                                SenderUser = new {
                                                    req.SenderUser.UserId,
                                                    req.SenderUser.FirstName,
                                                    req.SenderUser.LastName,
                                                    req.SenderUser.PhotoUrl,
                                                },
                                                RequestedBook = new {
                                                    req.SendedBook.Book_Id,
                                                    req.SendedBook.Title,
                                                    req.SendedBook.Photo_Url,
                                                },
                                                RequestStatus = req.RequestStaus.Name })
                           .ToList();

            return(Ok(requests));
        }
        public IHttpActionResult CreateRequest6(int id)
        {
            var resultRequest = db.Requests.Where(ww => ww.Id == id).FirstOrDefault();

            if (resultRequest == null)
            {
                return(NotFound());
            }
            else
            {
                resultRequest.SendSwapUsertId = UserUtilities.GetCurrentUserId(User);

                resultRequest.RequestStatusId = RequestStatusEnum.RequestSwap;

                db.Entry(resultRequest).State = EntityState.Modified;
                db.SaveChanges();

                return(Ok(new
                {
                    resultRequest.Id,
                    resultRequest.DateOfMessage,
                    resultRequest.SenderId,
                    resultRequest.SendSwapUsertId,
                    RequestedUser = new
                    {
                        resultRequest.RecieverUser.UserId,
                        resultRequest.RecieverUser.FirstName,
                        resultRequest.RecieverUser.LastName,
                        resultRequest.RecieverUser.PhotoUrl
                    },
                    SenderUser = new
                    {
                        resultRequest.SenderUser.UserId,
                        resultRequest.SenderUser.FirstName,
                        resultRequest.SenderUser.LastName,
                        resultRequest.SenderUser.PhotoUrl
                    },
                    RequestedBook = new
                    {
                        resultRequest.RequestedBook.Book_Id,
                        resultRequest.RequestedBook.Title,
                        resultRequest.RequestedBook.Photo_Url,
                    },
                    SendedBook = new
                    {
                        resultRequest.SendedBook.Book_Id,
                        resultRequest.SendedBook.Title,
                        resultRequest.SendedBook.Photo_Url,
                    },
                    RequestStatus = resultRequest.RequestStatusId
                }));;
            }
        }
        //[Authorize(Roles = "Admin")]
        public IHttpActionResult PostBook(Book book)
        {
            book.Rate = 0;
            var LoggedInUserId = UserUtilities.GetCurrentUserId(User);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.Books.Add(book);
            db.SaveChanges();
            foreach (string item in book.Categor.Split(','))
            {
                var genre = db.Genres.Where(gen => gen.Genre_Name.Equals(item)).FirstOrDefault();
                if (genre == null)
                {
                    genre = new Genre()
                    {
                        Genre_Name = item
                    };
                    db.Genres.Add(genre);
                    db.SaveChanges();
                    db.GenreBooks.Add(new GenreBook()
                    {
                        BookId = book.Book_Id, GenreId = genre.Genre_Id
                    });
                }
                else
                {
                    db.GenreBooks.Add(new GenreBook()
                    {
                        BookId = book.Book_Id, GenreId = genre.Genre_Id
                    });
                    db.SaveChanges();
                }
            }

            if (book.Want.Equals("have"))
            {
                var en = BookConditionEnum.Good;
                if (book.BookCondition == 0)
                {
                    en = BookConditionEnum.New;
                }
                else if (book.BookCondition == 1)
                {
                    en = BookConditionEnum.Good;
                }
                else if (book.BookCondition == 2)
                {
                    en = BookConditionEnum.Fair;
                }
                else if (book.BookCondition == 3)
                {
                    en = BookConditionEnum.Old;
                }
                ;
                //var x = db.BookConditions.Where(ww=> ww.Id == book.BookCondition);

                db.UserHaveBooks.Add(new UserHaveBook()
                {
                    UserId = LoggedInUserId, BookId = book.Book_Id, BookConditionId = en, DateOfAdded = DateTime.Now
                });
            }
            else if (book.Want.Equals("want"))
            {
                var user = db.Users.Where(user2 => user2.UserId == LoggedInUserId).FirstOrDefault();
                //book.UserWantBooks.Add(user);
                db.UserWantBooks.Add(new UserWantBook()
                {
                    BookId = book.Book_Id, UserId = LoggedInUserId
                });
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception e)
            {
                return(Ok(e.Message));
            }

            return(CreatedAtRoute("DefaultApi", new { id = book.Book_Id }, book));
        }