<%@ WebService Language="JScript" Class="InstantOrder" %>

import System;
import System.Web.Services;
import IBuySpy;

public class InstantOrder extends WebService {

    //*******************************************************
    //
    // InstantOrder.OrderItem() Method 
    //
    // The OrderItem method enables a remote client to programmatically
    // place an order using a webservice. 
    //
    //*******************************************************      

 
    WebMethodAttribute(Description="The OrderItem method enables a remote client to programmatically place an order using a WebService.", EnableSession=false)
    
	public function OrderItem(userName: String, password: String, productID: int, quantity: int) : OrderDetails {
        
        // Login client using provided username and password
        var accountSystem : IBuySpy.CustomersDB = new IBuySpy.CustomersDB();
        var customerId : String  = accountSystem.Login(userName, password);
        
        if (customerId == null) {
            throw new Exception("Error: Invalid Login!");
        }

        // Wrap in try/catch block to catch errors in the event that someone types in
        // an invalid value for quantity
        var qty : int  = System.Math.Abs(quantity);
        if (qty == quantity && qty < 1000) {
        
            // Add Item to Shopping Cart
            var myShoppingCart : IBuySpy.ShoppingCartDB = new IBuySpy.ShoppingCartDB();
            myShoppingCart.AddItem(customerId, productID, qty);

            // Place Order
            var orderSystem : IBuySpy.OrdersDB = new IBuySpy.OrdersDB();
            var orderID : int  = orderSystem.PlaceOrder(customerId, customerId);
            
            // Return OrderDetails
            return orderSystem.GetOrderDetails(orderID, customerId);
        }
        else {
            // invalid input 
            return null;
        }
    }
     
    //*******************************************************
    //
    // InstantOrder.CheckStatus() Method 
    //
    // The CheckStatus method enables a remote client to programmatically
    // query the current status of an order in the IBuySpy System. 
    //
    //*******************************************************  
     
    WebMethodAttribute(Description="The CheckStatus method enables a remote client to programmatically query the current status of an order in the IBuySpy System.", EnableSession=false)

    public function CheckStatus(userName:  String, password:  String, orderID:  int ) : OrderDetails {
     
        // Login client using provided username and password
        var accountSystem : IBuySpy.CustomersDB = new IBuySpy.CustomersDB();
        var customerId : String = accountSystem.Login(userName, password);
        
        if (customerId == null) {
            throw new Exception("Error: Invalid Login!");
        }
        
        // Return OrderDetails Status for Specified Order
        var orderSystem : IBuySpy.OrdersDB  = new IBuySpy.OrdersDB();
        return orderSystem.GetOrderDetails(orderID, customerId);
    }
}