Ejemplo n.º 1
0
        public void Add(DisplayAdLibrisForBookModel book)
        {
            Series series = new Series();

            if (book.SeriesName != string.Empty && book.SeriesName != null)
            {
                series = GetSeriesByName(book.SeriesName);
            }
            else
            {
                series = null;
            }

            List <Reader> readers = new List <Reader>();

            if (book.ReaderNames != string.Empty && book.ReaderNames != null)
            {
                readers = GetReadersByName(book.ReaderNames).ToList();
            }
            else
            {
                readers = null;
            }

            Book newDBBook = new Book
            {
                Title           = book.Title,
                Description     = book.Description,
                ISBN            = book.ISBN,
                Series          = series,
                PublishingDate  = Convert.ToDateTime(book.PublishingDate),
                ImagePath       = book.ImagePath,
                Readers         = readers,
                Publisher       = GetPublisherByName(book.PublisherName),
                Authors         = GetAuthorsByNames(book.AuthorNames),
                Format          = GetFormatByName(book.Format),
                Genres          = GetGenresByName(book.GenreNames),
                Language        = GetLanguageByName(book.Language),
                PageNumber      = book.PageNumber,
                Price           = book.Price,
                Weight          = book.Weight,
                QuantityInStock = int.Parse(book.QuantityInStock)
            };

            context.Books.Add(newDBBook);
            context.SaveChanges();
        }
Ejemplo n.º 2
0
        public bool ProcessOrder(AddressModel address, ContactModel contact, ShoppingCartModel cart, int paymentId, int deliveryId, string comment)
        {
            var dbAddress      = GetAddressByStreet(address);
            var dbContact      = GetContactBySocialSecurityNumber(dbAddress, contact);
            var dbPaymentType  = GetPaymentTypeById(paymentId);
            var dbDeliveryType = GetDeliveryTypeById(deliveryId);

            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    var order = new Order
                    {
                        Address      = dbContact.Address,
                        Contact      = dbContact,
                        OrderNumber  = System.Guid.NewGuid().ToString(),
                        DeliveryType = dbDeliveryType,
                        PaymentType  = dbPaymentType,
                        TotalPrice   = cart.GetCartTotal(),
                        OrderDate    = DateTime.Now,
                        Comment      = comment,
                    };
                    context.Orders.Add(order);
                    context.SaveChanges();

                    foreach (var item in cart.Items)
                    {
                        if (item.Quantity <= item.Book.QuantityInStock)
                        {
                            var orderDetail = new OrderDetail
                            {
                                OrderId         = order.Id,
                                Book            = GetBookById(item.Book.Id),
                                QuantityOrdered = item.Quantity,
                            };
                            context.OrderDetails.Add(orderDetail);
                            var bookBought = context.Books.Find(item.Book.Id);
                            bookBought.QuantityInStock -= item.Quantity;
                        }
                        else
                        {
                            transaction.Rollback();
                            return(false);
                        }
                    }
                    context.SaveChanges();
                    transaction.Commit();
                    return(true);
                }

                catch (Exception E)
                {
                }
            }
            return(true);
        }