Beispiel #1
0
        public IActionResult Delete(int id)
        {
            var ent = _service.Get(id);

            _service.Delete(ent);
            return(new NoContentResult());
        }
        public void Delete_ThrowsEntityNotFoundException()
        {
            var repositoryMock = new Mock <ICrudRepository>();

            repositoryMock.Setup(_ => _.GetById <TestEntity>(It.IsAny <Guid>())).Throws <EntityNotFoundException>();
            var service = new CrudService <TestEntity>(repositoryMock.Object);

            Assert.Throws <EntityNotFoundException>(() => service.Delete(Guid.Empty));
        }
Beispiel #3
0
        public async Task Delete()
        {
            // Arrange
            var obj = new UserEntity();

            // Act
            var service = new CrudService <UserEntity>(_logger.Object, _crudRepository.Object);
            var result  = await Record.ExceptionAsync(() => service.Delete(obj));

            // Assert
            Assert.Null(result);
        }
        public void Delete_ReturnsDeletedElement()
        {
            var repositoryMock = new Mock <ICrudRepository>();

            repositoryMock.Setup(_ => _.GetById <TestEntity>(_entity.Id)).Returns(_entity);
            var service = new CrudService <TestEntity>(repositoryMock.Object);

            var result = service.Delete(_entity.Id);

            Assert.Equal(_entity.Id, result.Id);
            repositoryMock.Verify(_ => _.GetById <TestEntity>(It.IsAny <Guid>()), Times.Once());
            repositoryMock.Verify(_ => _.Delete <TestEntity>(It.IsAny <Guid>()), Times.Once);
            repositoryMock.Verify(_ => _.SaveChanges(), Times.Once);
        }
Beispiel #5
0
        public void TestCrudService()
        {
            int    expectedId = 2;
            string readId     = "Adan";
            string updateId   = "Sonny";

            MockStorage ms = new MockStorage();
            CrudService cs = new CrudService(ms);

            cs.Create("Adan"); //Adding to NameList
            cs.Create("Jae");
            cs.Create("Tiana");
            cs.Delete("test"); //Deleting from NameList


            Assert.AreEqual(expectedId, ms.NameList.Count);                                   // compare 2 to the Namelist count
            Assert.AreEqual(readId, ms.Select());                                             //comparing Adan to value in Select function
            Assert.AreEqual(expectedId, ms.Update(readId, updateId));                         // updating Adan to Sonny
            Assert.AreEqual(updateId, ms.NameList.Where(n => n == "Sonny").FirstOrDefault()); //Checking to if Sonny exist in NameList
        }
Beispiel #6
0
        public void TestCrudService()
        {
            int         expectedId     = 3;
            string      expectedString = "Tiana";
            string      newName        = "John";
            int         count          = 2;
            MockStorage ms             = new MockStorage();
            CrudService cs             = new CrudService(ms);

            cs.Create("Tiana");
            cs.Create("Adan");
            cs.Create("Britto");


            Assert.AreEqual(expectedString, ms.Read());

            Assert.AreEqual(expectedId, ms.Update(expectedString, newName));
            Assert.AreEqual(newName, ms.NameList.Where(n => n == "John").FirstOrDefault());

            cs.Delete("Britto");
            Assert.AreEqual(count, ms.NameList.Count);
        }
        public virtual async Task <IActionResult> Delete(string id, TEdit vm)
        {
            await crudService.Delete(GetSelectSingle(id));

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #8
0
        public static void showData(List <OpenData> nodes, string f)
        {
            CrudService _crudService = new CrudService();

            Console.WriteLine("共 " + nodes.Count + " 筆資料");
            if (f == "0")
            {
                nodes.ForEach(group =>
                {
                    var country   = group.縣市;
                    var site      = group.地區;
                    var AQI       = group.空氣品質指標;
                    var pollutant = group.空氣汙染指標物;
                    var status    = group.狀態;
                    if (pollutant == "")
                    {
                        pollutant = "無";
                    }
                    Console.WriteLine("縣市名:" + country + "\t地區:" + site + "\t空氣品質指標:" + AQI);
                    Console.WriteLine("空氣汙染指標物:" + pollutant);
                    Console.WriteLine("狀態:" + status);
                });
            }
            else if (f == "1")
            {
                nodes.GroupBy(node => node.空氣汙染指標物).ToList()
                .ForEach(group =>
                {
                    var key = group.Key;
                    if (key == "")
                    {
                        key = "無";
                    }
                    var groupDatas = group.ToList();
                    var msg        = $"空氣汙染指標物:{key},共有{groupDatas.Count()}筆資料";
                    Console.WriteLine(msg);
                });
            }
            else if (f == "2")
            {
                nodes.GroupBy(node => node.狀態).ToList()
                .ForEach(group =>
                {
                    var key        = group.Key;
                    var groupDatas = group.ToList();
                    var msg        = $"狀態:{key},共有{groupDatas.Count()}筆資料";
                    Console.WriteLine(msg);
                });
            }
            else if (f == "3")
            {
                _crudService.ImportToDb(nodes);
                Console.WriteLine("資料已新增至LocalDB");
            }
            else if (f == "4")
            {
                Console.Write("請輸入欲查詢之縣市:");
                var in_country = Console.ReadLine();
                var result     = _crudService.FindDataFromDB(in_country);
                Console.WriteLine("共 " + result.Count + " 筆資料");
                result.ForEach(group =>
                {
                    var country   = group.縣市;
                    var AQI       = group.空氣品質指標;
                    var pollutant = group.空氣汙染指標物;
                    var status    = group.狀態;
                    if (pollutant == "")
                    {
                        pollutant = "無";
                    }
                    Console.WriteLine("縣市名:" + country + "\t空氣品質指標:" + AQI);
                    Console.WriteLine("空氣汙染指標物:" + pollutant);
                    Console.WriteLine("狀態:" + status);
                });
            }
            else if (f == "d")
            {
                Console.Write("請輸入欲刪除資料之ID:");
                var id = Console.ReadLine();
                _crudService.Delete(id);
                Console.WriteLine("資料已刪除!");
            }
            else
            {
                Console.WriteLine("測試用字串");
            }
        }