Exemplo n.º 1
0
        public void SatisfyOrders(string bookTitle, int quantity, Guid orderGuid)
        {
            Request request  = new Request(bookTitle, quantity, orderGuid);
            Book    book     = GetBookByTitle(request.bookTitle);
            int     tmpStock = book.stock + request.quantity;

            Order.State state = new Order.State()
            {
                type         = Order.State.Type.DISPATCHED_AT,
                dispatchDate = DateTime.Now
            };
            List <Order> orders      = getOrdersByBook(book, tmpStock);
            Order        targetOrder = null;

            using (SqlConnection c = new SqlConnection(database)) {
                c.Open();
                targetOrder = getOrder(c, request.orderGuid);
                SqlTransaction transaction = c.BeginTransaction("SatisfyOrder");
                try {
                    RemoveRequestFromDB(c, transaction, request);

                    if (targetOrder.state.type != Order.State.Type.DISPATCHED_AT)
                    {
                        UpdateOrderState(c, transaction, request.orderGuid, state);
                        tmpStock         -= targetOrder.quantity;
                        targetOrder.state = state;
                    }

                    orders.ForEach(o => {
                        if (o.quantity <= tmpStock)
                        {
                            UpdateOrderState(c, transaction, o.guid, state);
                            o.state   = state;
                            tmpStock -= o.quantity;
                        }
                    });

                    UpdateBookStock(c, transaction, book.id, tmpStock);
                    transaction.Commit();
                } catch (Exception e) {
                    Console.WriteLine("DB Exception: " + e.Message);
                    transaction.Rollback();
                } finally {
                    c.Close();
                }
            }

            NotifyDeleteRequest(request);

            book.stock = tmpStock;
            orders.Add(targetOrder);
            orders.ForEach(order => {
                order.book = book;
                NotifyClients(OrderType.UPDATE_STATE, order);
                if (order.state.type == Order.State.Type.DISPATCHED_AT)
                {
                    Email.SendEmail(OrderType.UPDATE_STATE, order);
                }
            });
        }
Exemplo n.º 2
0
 public Order(Guid guid, Client client, Book book, int quantity, Order.State state)
 {
     this.guid     = guid;
     this.book     = book;
     this.quantity = quantity;
     this.client   = client;
     this.state    = state;
     totalPrice    = quantity * book.price;
 }
Exemplo n.º 3
0
 public Order(Client client, Book book, int quantity, Order.State state)
 {
     guid          = Guid.NewGuid();
     this.book     = book;
     this.quantity = quantity;
     this.client   = client;
     this.state    = state;
     totalPrice    = quantity * book.price;
 }
Exemplo n.º 4
0
        /*private Order SetState(string id, string stateType) {
         *  using (SqlConnection c = new SqlConnection(database)) {
         *      try {
         *          c.Open();
         *          Order.State state = nextState(stateType);
         *          UpdateOrderState(c, id, state);
         *          Order order = getOrder(c, Guid.Parse(id));
         *          return order;
         *      } catch (SqlException e) {
         *          Console.WriteLine("DB Exception: " + e);
         *      }
         *      return null;
         *  }
         * }*/

        protected void UpdateOrderState(SqlConnection c, SqlTransaction t, Guid guid, Order.State state)
        {
            string sql = "UPDATE Orders SET State = @s, DispatchDate = @d" +
                         " WHERE Guid LIKE @id";
            SqlCommand cmd = new SqlCommand(sql, c, t);

            cmd.Parameters.AddWithValue("@s", state.type.ToString());
            cmd.Parameters.AddWithValue("@d", state.dispatchDate);
            cmd.Parameters.AddWithValue("@id", guid.ToString());
            cmd.ExecuteNonQuery();
        }