/// <summary>
        /// Update the input text boxes with information for a selected town
        /// </summary>
        /// <param name="Id">The ID of a selected town</param>
        private void UpdateTextBoxes(int Id)
        {
            TownBusiness townBusiness = new TownBusiness();
            Town         town         = townBusiness.GetTownById(Id);

            txtName.Text = town.Name;
        }
Пример #2
0
        public List <Town> UpdateTownData(List <City> cities)
        {
            string      errmsg = "";
            List <Town> towns  = new List <Town>();

            if (cities == null || cities.Count == 0)
            {
                return(null);
            }
            try
            {
                HttpHelper httpHelper = new HttpHelper();
                foreach (var city in cities)
                {
                    foreach (var item in city.Result)
                    {
                        string requestUrl = string.Format(Configs.ApiTownUrl, item.Id);
                        string result     = httpHelper.HttpResponsePostString(requestUrl);
                        if (string.IsNullOrEmpty(result))
                        {
                            Console.WriteLine($"请求{item.Name}(ID:{item.Id})下的区县失败!");
                            continue;
                        }
                        TownBusiness townBusiness = new TownBusiness();
                        Town         town         = townBusiness.SaveData(result, out errmsg);
                        if (town != null && string.IsNullOrEmpty(errmsg))
                        {
                            Console.WriteLine($"保存{item.Name}区县数据成功,共计{town.Result.Count}条记录。");
                            towns.Add(town);
                        }
                        else
                        {
                            if (town != null)
                            {
                                switch (town.Status)
                                {
                                case "203":
                                    Console.WriteLine($"{item.Name}下没有数据.");
                                    break;

                                default:
                                    Console.WriteLine($"获取{item.Name}数据失败,错误代码:{town.Status},错误信息:{town.Msg}");
                                    break;
                                }
                            }
                            else
                            {
                                Console.WriteLine($"保存{item.Name}城市数据失败!错误:" + errmsg);
                            }
                            continue;
                        }
                    }
                }
                return(towns);
            }
            catch (Exception ex)
            {
                throw new Exception($"更新区县数据出错,错误:{ex.Message}");
            }
        }
        public void GetTownByName()
        {
            var data = new List <Town>
            {
                new Town {
                    Name = "Town1"
                },
                new Town {
                    Name = "Town2"
                },
                new Town {
                    Name = "Town3"
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Town> >();

            mockSet.As <IQueryable <Town> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Town> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Town> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Town> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <CarDealershipContext>();

            mockContext.Setup(m => m.Towns).Returns(mockSet.Object);

            var service   = new TownBusiness(mockContext.Object);
            var TownFound = service.GetTownByName("Town1");

            Assert.AreEqual("Town1", TownFound.Name);
        }
        public void SortTownByNameDescending()
        {
            var data = new List <Town>
            {
                new Town {
                    Name = "TownC"
                },
                new Town {
                    Name = "TownA"
                },
                new Town {
                    Name = "TownB"
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Town> >();

            mockSet.As <IQueryable <Town> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Town> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Town> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Town> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <CarDealershipContext>();

            mockContext.Setup(m => m.Towns).Returns(mockSet.Object);

            var service    = new TownBusiness(mockContext.Object);
            var TownsFound = service.SortTownsByNameDescending();

            Assert.AreEqual(3, TownsFound.Count());
            Assert.AreEqual("TownC", TownsFound[0].Name);
            Assert.AreEqual("TownB", TownsFound[1].Name);
            Assert.AreEqual("TownA", TownsFound[2].Name);
        }
        /// <summary>
        /// Populate the data grid with information sorted by towns' name in descending order
        /// </summary>
        private void PopulateDataGridViewSortTownsByNameDescending()
        {
            dataGridView.Rows.Clear();
            TownBusiness townBusiness = new TownBusiness();
            var          townList     = townBusiness.SortTownsByNameDescending();

            DataPopulator(townList);
        }
        /// <summary>
        /// Populate the data grid with information about town with given town name
        /// </summary>
        private void PopulateDataGridViewGetTownByName()
        {
            dataGridView.Rows.Clear();
            TownBusiness townBusiness = new TownBusiness();
            var          town         = townBusiness.GetTownByName(txtGet.Text);

            DataPopulatorSingle(town);
        }
        //Main logic//
        //Get logic//

        /// <summary>
        /// Populate the data grid with information about all existing towns in the database
        /// </summary>
        private void PopulateDataGridViewDefault()
        {
            dataGridView.Rows.Clear();
            TownBusiness townBusiness = new TownBusiness();
            var          townList     = townBusiness.GetAllTowns();

            DataPopulator(townList);
        }
        /// <summary>
        /// Populate the data grid with information about town with given town Id
        /// </summary>
        private void PopulateDataGridViewGetTownById()
        {
            dataGridView.Rows.Clear();
            TownBusiness townBusiness = new TownBusiness();

            int.TryParse(txtGet.Text, out int townId);
            var town = townBusiness.GetTownById(townId);

            DataPopulatorSingle(town);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            Town         town         = GetEditedTown();
            TownBusiness townBusiness = new TownBusiness();

            townBusiness.Update(town);
            PopulateDataGridViewDefault();
            ResetSelect();
            ToggleSaveUpdate();
            txtName.Text = "";
        }
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (dataGridView.SelectedRows.Count > 0)
     {
         var          town         = dataGridView.SelectedRows[0].Cells;
         var          townId       = int.Parse(town[0].Value.ToString());
         TownBusiness townBusiness = new TownBusiness();
         townBusiness.Delete(townId);
         PopulateDataGridViewDefault();
         ResetSelect();
     }
 }
        //cbGet and cbSort//

        //Buttons + attached logic//
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Town town = new Town
            {
                Name = txtName.Text
            };

            TownBusiness townBusiness = new TownBusiness();

            townBusiness.Add(town);
            PopulateDataGridViewDefault();
            txtName.Text = "";
        }
        public void AddTown()
        {
            var mockSet = new Mock <DbSet <Town> >();

            var mockContext = new Mock <CarDealershipContext>();

            mockContext.Setup(m => m.Towns).Returns(mockSet.Object);

            var service = new TownBusiness(mockContext.Object);

            service.Add(new Town());

            mockSet.Verify(m => m.Add(It.IsAny <Town>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
Пример #13
0
        private void btnTowns_Click(object sender, EventArgs e)
        {
            HideSpecificInfo();
            lblInfoTown.Visible = true;
            btnNewTown.Visible  = true;

            dataGridView.Rows.Clear();

            dataGridView.ColumnCount = 2;

            dataGridView.Columns[0].Name = "ID";
            dataGridView.Columns[1].Name = "Име";

            dataGridView.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            dataGridView.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;
            dataGridView.CellBorderStyle   = DataGridViewCellBorderStyle.Single;
            dataGridView.RowHeadersVisible = false;
            dataGridView.SelectionMode     =
                DataGridViewSelectionMode.FullRowSelect;
            dataGridView.MultiSelect = false;

            TownBusiness townBusiness = new TownBusiness();
            var          townsList    = townBusiness.GetAllTowns();

            foreach (var town in townsList)
            {
                string[] row =
                {
                    town.Id.ToString(),
                    town.Name,
                };
                dataGridView.Rows.Add(row);
            }
        }