コード例 #1
0
        public Response updateBook(BookData book)
        {
            Values values = new Values();

            values.add(BookTable.KEY_ID, book.id);
            values.add(BookTable.KEY_TITLE, book.title);
            values.add(BookTable.KEY_QUANTITY, book.quantity);
            values.add(BookTable.KEY_PRICE, book.price);

            Values values_where = new Values();

            values_where.add(BookTable.KEY_ID, book.id);

            BookTable.Instance.update(values, values_where);

            return(new Response("success", "book updated"));
        }
コード例 #2
0
        public Response addBook(BookData book)
        {
            Values values = new Values();

            values.add(BookTable.KEY_TITLE, book.title);

            List <Values> books = BookTable.Instance.get(null, values);

            if (books.Count > 0)
            {
                return(new Response("error", "That book is already registered!"));
            }

            values.clear();

            values.add(BookTable.KEY_TITLE, book.title);
            values.add(BookTable.KEY_QUANTITY, book.quantity);
            values.add(BookTable.KEY_PRICE, book.price);

            BookTable.Instance.insert(values);

            return(new Response("success", "Book added!"));
        }
コード例 #3
0
        public Response orderBook(BookData book, long quantity, string clientName, string clientEmail, string clientPassword, string address)
        {
            Values        values       = new Values();
            List <String> keys         = new List <string>();
            Values        where_values = new Values();
            string        cName;

            keys.Add(UserTable.KEY_ID);
            keys.Add(UserTable.KEY_NAME);
            where_values.add(UserTable.KEY_EMAIL, clientEmail);

            List <Values> result = UserTable.Instance.get(keys, where_values);

            if (result.Count == 0)
            {
                if (clientPassword == "" || clientName == "")
                {
                    return(new Response("error", "no client with that email and no info provided to create a new one"));
                }
                else
                {
                    values.add(UserTable.KEY_NAME, clientName);
                    values.add(UserTable.KEY_EMAIL, clientEmail);
                    values.add(UserTable.KEY_PASSWORD, clientPassword);

                    UserTable.Instance.insert(values);
                }
            }

            result = UserTable.Instance.get(keys, where_values);

            cName = (string)result[0].getValue(UserTable.KEY_NAME);

            values.clear();

            values.add(OrderTable.KEY_BOOK_ID, book.id);
            values.add(OrderTable.KEY_CLIENT_ID, result[0].getValue(UserTable.KEY_ID));
            values.add(OrderTable.KEY_QUANTITY, quantity);
            values.add(OrderTable.KEY_STATE, OrderTable.WAITING);
            values.add(OrderTable.KEY_DATE, Functions.getCurrentDate());
            values.add(OrderTable.KEY_STATE_DATE, "Waiting Expedition");
            values.add(OrderTable.KEY_PRICE, quantity * book.price);
            values.add(OrderTable.KEY_ADDRESS, address);

            OrderTable.Instance.insert(values);

            where_values.clear();
            where_values.add(OrderTable.KEY_BOOK_ID, book.id);
            keys.Clear();
            keys.Add(OrderTable.KEY_QUANTITY);

            result = OrderTable.Instance.get(keys, where_values);
            long bookDemand = 0;

            foreach (Values v in result)
            {
                bookDemand += (long)v.getValue(OrderTable.KEY_QUANTITY);
            }

            keys.Clear();
            where_values.clear();
            keys.Add(BookTable.KEY_QUANTITY);
            where_values.add(BookTable.KEY_ID, book.id);

            List <Values> res = BookTable.Instance.get(keys, where_values);

            bookDemand -= (long)res[0].getValue(BookTable.KEY_QUANTITY);

            where_values.clear();
            where_values.add(RequestTable.KEY_BOOK_ID, book.id);
            where_values.add(RequestTable.KEY_STATE, RequestTable.WAITING);
            keys.Clear();
            keys.Add(RequestTable.KEY_QUANTITY);

            result = RequestTable.Instance.get(keys, where_values);
            long requestQuantity = 0;

            foreach (Values v in result)
            {
                requestQuantity += (long)v.getValue(RequestTable.KEY_QUANTITY);
            }

            where_values.clear();
            where_values.add(RequestTable.KEY_BOOK_ID, book.id);
            where_values.add(RequestTable.KEY_STATE, RequestTable.SHIPPED);
            keys.Clear();
            keys.Add(RequestTable.KEY_QUANTITY);

            result = RequestTable.Instance.get(keys, where_values);

            foreach (Values v in result)
            {
                requestQuantity += (long)v.getValue(RequestTable.KEY_QUANTITY);
            }

            if (requestQuantity < bookDemand)
            {
                values.clear();
                values.add(RequestTable.KEY_ORDER_ID, OrderTable.Instance.all.Count);
                values.add(RequestTable.KEY_BOOK_ID, book.id);
                values.add(RequestTable.KEY_TITLE, book.title);
                values.add(RequestTable.KEY_QUANTITY, 10 * quantity);
                values.add(RequestTable.KEY_STATE, RequestTable.WAITING);
                values.add(RequestTable.KEY_DATE, Functions.getCurrentDate());
                values.add(RequestTable.KEY_STATE_DESC, "Waiting Expedition");

                RequestTable.Instance.insert(values);

                NetMsmqBinding binding = new NetMsmqBinding();
                binding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
                binding.Security.Transport.MsmqProtectionLevel    = System.Net.Security.ProtectionLevel.None;

                ServiceRequestClient proxy = new ServiceRequestClient(binding, new EndpointAddress("net.msmq://localhost/private/OrderQueue"));

                Request req = new Request();
                req.order_id = RequestTable.Instance.all.Count;
                req.book_id  = book.id;
                req.title    = book.title;
                req.quantity = 10 * quantity;
                req.state    = OrderTable.WAITING;
                req.date     = Functions.getCurrentDate();

                proxy.requestBook(req);
            }

            return(new Response("success", "book ordered"));
        }