示例#1
0
        private async void btnSubmit_Click(object sender, EventArgs e)
        {
            AirplaneTypeWrapper airplaneTypeWrapper = new AirplaneTypeWrapper();
            AirplaneInfoWrapper airplaneInfoWrapper = new AirplaneInfoWrapper();

            airplaneType.Name     = txtName.Text;
            airplaneType.IsActive = rbtnActive.Checked;
            airplaneType.AirplaneInfoes.FirstOrDefault(ai => ai.ClassID == 1).SeatCount = Convert.ToInt32(numFirstClassSeat.Value);
            airplaneType.AirplaneInfoes.FirstOrDefault(ai => ai.ClassID == 2).SeatCount = Convert.ToInt32(numBusinessClassSeat.Value);
            airplaneType.AirplaneInfoes.FirstOrDefault(ai => ai.ClassID == 3).SeatCount = Convert.ToInt32(numClubClassSeat.Value);

            // Tao bien luu ket qua tra ve
            bool isSuccess;

            // Kiem tra xem dang o che do nao
            if (mode == FormMode.CREATE)
            {
                // Neu dang o che do them moi (CREATE)
                // POST account len server
                isSuccess = await airplaneTypeWrapper.Post(airplaneType);

                foreach (AirplaneInfo ai in airplaneType.AirplaneInfoes)
                {
                    if (isSuccess)
                    {
                        isSuccess = await airplaneInfoWrapper.Post(ai);
                    }
                }
            }
            else
            {
                // Neu dang o che do chinh sua (EDIT)
                // PUT account len server
                isSuccess = await airplaneTypeWrapper.Put(airplaneType.ID, airplaneType);

                foreach (AirplaneInfo ai in airplaneType.AirplaneInfoes)
                {
                    if (isSuccess)
                    {
                        isSuccess = await airplaneInfoWrapper.Put(ai.AirplaneTypeID, ai);
                    }
                }
            }

            // Kiem tra ket qua tra ve
            if (isSuccess)
            {
                // Neu thanh cong
                MessageBox.Show("Operation completed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // Tat form CE
                this.Close();
            }
            else
            {
                // Neu that bai, hien thong bao loi
                MessageBox.Show("An error has occurred:\n" + airplaneTypeWrapper.GetErrorMessage(), "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Only use for generate sample data of table FlightSchedule
        /// This function will generate flight schedule for all route in database,
        /// each route will have <code>maxFlightSchedulePerRoute</code> flight schedules,
        /// each flight schedule of route will apart one day, starting from today.
        /// </summary>
        private async void AutoGenerateFlightSchedule()
        {
            DateTime startTime = DateTime.Now;

            Console.WriteLine("Start at {0}", startTime);

            FlightScheduleWrapper flightScheduleWrapper = new FlightScheduleWrapper();
            AirplaneWrapper       airplaneWrapper       = new AirplaneWrapper();
            AirplaneTypeWrapper   airplaneTypeWrapper   = new AirplaneTypeWrapper();
            AirplaneInfoWrapper   airplaneInfoWrapper   = new AirplaneInfoWrapper();
            RouteWrapper          routeWrapper          = new RouteWrapper();

            List <Airplane> lstAirplane = await airplaneWrapper.List();

            List <AirplaneType> lstAirplaneType = await airplaneTypeWrapper.List();

            List <AirplaneInfo> lstAirplaneInfo = await airplaneInfoWrapper.List();

            List <Route> lstRoute = await routeWrapper.List();

            int      flightScheduleOfCurrentRoute = 0;
            int      maxFlightSchedulePerRoute    = 15;
            DateTime startingDate         = DateTime.Now;
            int      currentAirplaneIndex = 0;
            int      count = 0;

            for (int routeIndex = 0, maxRouteIndex = lstRoute.Count - 1; routeIndex <= maxRouteIndex; routeIndex++)
            {
                Route currentRoute = lstRoute[routeIndex];

                do
                {
                    Airplane            currentAirplane     = lstAirplane[currentAirplaneIndex++];
                    AirplaneType        currentAirplaneType = lstAirplaneType.Find(at => at.ID == currentAirplane.TypeID);
                    List <AirplaneInfo> currentAirplaneInfo = lstAirplaneInfo.Where(ai => ai.AirplaneTypeID == currentAirplaneType.ID).ToList();
                    DateTime            departureDate       = startingDate.AddDays(flightScheduleOfCurrentRoute);

                    FlightSchedule flightSchedule = new FlightSchedule()
                    {
                        AirplaneCode      = currentAirplane.AirplaneCode,
                        RouteID           = currentRoute.ID,
                        DepartureDate     = departureDate,
                        FirstSeatAvail    = currentAirplaneInfo.Find(ai => ai.ClassID == 1).SeatCount,
                        BusinessSeatAvail = currentAirplaneInfo.Find(ai => ai.ClassID == 2).SeatCount,
                        ClubSeatAvail     = currentAirplaneInfo.Find(ai => ai.ClassID == 3).SeatCount,
                        IsActive          = true
                    };

                    if (await flightScheduleWrapper.Post(flightSchedule))
                    {
                        Console.WriteLine("Add flight schedule #{0} success!", ++count);
                    }
                    else
                    {
                        Console.WriteLine("Add flight schedule #{0} failed!", ++count);
                    }

                    if (currentAirplaneIndex >= lstAirplane.Count)
                    {
                        currentAirplaneIndex = 0;
                    }
                } while (++flightScheduleOfCurrentRoute < maxFlightSchedulePerRoute);

                flightScheduleOfCurrentRoute = 0;
            }

            DateTime endTime = DateTime.Now;

            Console.WriteLine("End at {0}", startTime);
            Console.WriteLine("Total time: {0}", (endTime - startTime).TotalSeconds);
        }
        public async void LoadDataGridView()
        {
            // Luu lai dong hien tai dang chon
            int currentRowIndex;

            // Neu hien tai khong co dong nao duoc chon thi mac dinh la dong so 0
            if (dgvAirplane.SelectedRows.Count == 0)
            {
                currentRowIndex = 0;
            }
            else
            {
                currentRowIndex = dgvAirplane.Rows.IndexOf(dgvAirplane.SelectedRows[0]);
            }

            // Luu lai dong hien tai dang o dau` bang? trong dataGridView
            int firstRowIndex = dgvAirplane.FirstDisplayedScrollingRowIndex;

            if (firstRowIndex == -1)
            {
                firstRowIndex = 0;
            }

            // Goi API lay du lieu ve
            AirplaneWrapper airplaneWrapper = new AirplaneWrapper();
            List <Airplane> lstAirplane     = await airplaneWrapper.List();

            // Khai bao API de lay du lieu cua Loai may bay (AirplaneType)
            // va Thong tin ve` so luong cua tung loai ghe (AirplaneInfo)
            AirplaneTypeWrapper airplaneTypeWrapper = new AirplaneTypeWrapper();
            AirplaneInfoWrapper airplaneInfoWrapper = new AirplaneInfoWrapper();

            // Goi API lay du lieu ve
            List <AirplaneType> lstAirplaneType = await airplaneTypeWrapper.List();

            List <AirplaneInfo> lstAirplaneInfo = await airplaneInfoWrapper.List();

            // Tao bang de chua du lieu tra ve tu API
            DataTable table = new DataTable();

            table.Columns.Add("AirplaneCode");       // Ma may bay
            table.Columns.Add("AirplaneTypeID");     // ID cua loai may bay
            table.Columns.Add("AirplaneTypeName");   // Ten cua loai may bay
            table.Columns.Add("FirstClassCount");    // So luong ghe hang nhat
            table.Columns.Add("BusinessClassCount"); // So luong ghe hang Thuong gia
            table.Columns.Add("ClubClassCount");     // So luong ghe thuong
            table.Columns.Add("IsActive");           // Trang thai cua may bay (con hoat dong khong)

            // Kiem tra xem ket qua goi API co thanh cong hay khong
            if (lstAirplane != null)
            {
                // Lap qua tung "dong` du lieu"
                foreach (Airplane airplane in lstAirplane)
                {
                    // Goi API de lay du lieu cua Loai may bay (AirplaneType)
                    AirplaneType airplaneType = lstAirplaneType.Where(at => at.ID == airplane.TypeID).Single();

                    // Goi API de lay So luong cua moi Loai ghe tren may bay nay
                    List <AirplaneInfo> airplaneInfo = lstAirplaneInfo.Where(ai => ai.AirplaneTypeID == airplaneType.ID).ToList();

                    // Tao mot dong` trong bang winForm
                    DataRow row = table.NewRow();

                    // Gan du lieu len dong moi tao
                    row["AirplaneCode"]       = airplane.AirplaneCode;
                    row["AirplaneTypeID"]     = airplane.TypeID;
                    row["AirplaneTypeName"]   = airplaneType.Name;
                    row["FirstClassCount"]    = airplaneInfo.Where(ai => ai.ClassID == 1).Single().SeatCount;
                    row["BusinessClassCount"] = airplaneInfo.Where(ai => ai.ClassID == 2).Single().SeatCount;
                    row["ClubClassCount"]     = airplaneInfo.Where(ai => ai.ClassID == 3).Single().SeatCount;
                    row["IsActive"]           = airplane.IsActive;

                    // Gan dong moi tao vao bang
                    table.Rows.Add(row);
                }

                // Sau khi lap qua het cac dong du lieu
                // Ta co bang WinForm hoan chinh
                // Gan bang WinForm len DataGridView
                dgvAirplane.DataSource = table;

                // An? cot AirplaneTypeID di, khong hien cot nay
                dgvAirplane.Columns["AirplaneTypeID"].Visible = false;

                // Chon lai dong ban dau duoc chon truoc khi reload
                if (dgvAirplane.Rows.Count > 0)
                {
                    dgvAirplane.Rows[currentRowIndex].Selected = true;
                }

                // Cuon. toi' dong` duoc. chon.
                if (dgvAirplane.Rows.Count > 0)
                {
                    dgvAirplane.FirstDisplayedScrollingRowIndex = firstRowIndex;
                }
            }
        }
        public async void LoadDataGridView()
        {
            // Luu lai dong hien tai dang chon
            int currentRowIndex;

            // Neu hien tai khong co dong nao duoc chon thi mac dinh la dong so 0
            if (dgvAirplaneType.SelectedRows.Count == 0)
            {
                currentRowIndex = 0;
            }
            else
            {
                currentRowIndex = dgvAirplaneType.Rows.IndexOf(dgvAirplaneType.SelectedRows[0]);
            }

            // Luu lai dong hien tai dang o dau` bang? trong dataGridView
            int firstRowIndex = dgvAirplaneType.FirstDisplayedScrollingRowIndex;

            if (firstRowIndex == -1)
            {
                firstRowIndex = 0;
            }

            // Goi API lay du lieu ve
            AirplaneTypeWrapper airplaneTypeWrapper = new AirplaneTypeWrapper();
            List <AirplaneType> lstAirplaneType     = await airplaneTypeWrapper.List();

            AirplaneInfoWrapper airplaneInfoWrapper = new AirplaneInfoWrapper();
            List <AirplaneInfo> lstAirplaneInfo     = await airplaneInfoWrapper.List();

            // Tao bang de chua du lieu tra ve tu API
            DataTable table = new DataTable();

            table.Columns.Add("ID");
            table.Columns.Add("Name");
            table.Columns.Add("FirstClassSeat");
            table.Columns.Add("BusinessClassSeat");
            table.Columns.Add("ClubClassSeat");
            table.Columns.Add("IsActive");

            // Kiem tra xem ket qua goi API co thanh cong hay khong
            if (lstAirplaneType != null)
            {
                // Lap qua tung "dong` du lieu"
                foreach (AirplaneType airplaneType in lstAirplaneType)
                {
                    // Tao mot dong` trong bang winForm
                    DataRow row = table.NewRow();

                    // Gan du lieu len dong moi tao
                    row["ID"]                = airplaneType.ID;;
                    row["Name"]              = airplaneType.Name;
                    row["FirstClassSeat"]    = lstAirplaneInfo.Find(ai => ai.AirplaneTypeID == airplaneType.ID && ai.ClassID == 1).SeatCount;
                    row["BusinessClassSeat"] = lstAirplaneInfo.Find(ai => ai.AirplaneTypeID == airplaneType.ID && ai.ClassID == 2).SeatCount;
                    row["ClubClassSeat"]     = lstAirplaneInfo.Find(ai => ai.AirplaneTypeID == airplaneType.ID && ai.ClassID == 3).SeatCount;
                    row["IsActive"]          = airplaneType.IsActive;

                    // Gan dong moi tao vao bang
                    table.Rows.Add(row);
                }

                // Sau khi lap qua het cac dong du lieu
                // Ta co bang WinForm hoan chinh
                // Gan bang WinForm len DataGridView
                dgvAirplaneType.DataSource = table;

                // Chon lai dong ban dau duoc chon truoc khi reload
                if (dgvAirplaneType.Rows.Count > 0)
                {
                    dgvAirplaneType.Rows[currentRowIndex].Selected = true;
                }

                // Cuon. toi' dong` duoc. chon.
                if (dgvAirplaneType.Rows.Count > 0)
                {
                    dgvAirplaneType.FirstDisplayedScrollingRowIndex = firstRowIndex;
                }
            }
        }