예제 #1
0
        public void Setup()
        {
            var context = GetShellDb();

            // Add data to shelltemps and shelltemppositions
            for (var index = 0; index < shellTemps.Count; index++)
            {
                var shellTemp = shellTemps[index];
                context.ShellTemperatures.Add(shellTemp);

                ShellTemp dbTemp = context.ShellTemperatures.Find(shellTemp.Id);

                ShellTemperaturePosition shellTemperaturePosition = new ShellTemperaturePosition
                {
                    ShellTemp = dbTemp,
                    Position  = devPos[index]
                };
                context.ShellTemperaturePositions.Add(shellTemperaturePosition);
            }

            context.SaveChanges();

            positionsRepository = new PositionsRepository(context);
            context.Positions.Add(devPos[3]); // Extra
        }
        public bool Create(ShellTemperatureComment model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model), "The comment object was null");
            }

            // get data from database
            DeviceInfo     device         = Context.DevicesInfo.Find(model.ShellTemp.Device.Id);
            ShellTemp      temp           = Context.ShellTemperatures.Find(model.ShellTemp.Id);
            ReadingComment readingComment = Context.ReadingComments.Find(model.Comment.Id);

            if (temp == null || device == null || readingComment == null)
            {
                throw new NullReferenceException("The temperature, device or comment is null");
            }

            model.ShellTemp        = temp;
            model.ShellTemp.Device = device;
            model.Comment          = readingComment;
            Context.ShellTemperatureComments.Add(model);

            Context.SaveChanges();
            return(true);
        }
        public bool Update(ShellTemperatureComment model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model), "The shell temperature comment supplied is null");
            }

            // get data from database
            DeviceInfo     device         = Context.DevicesInfo.Find(model.ShellTemp.Device.Id);
            ShellTemp      temp           = Context.ShellTemperatures.Find(model.ShellTemp.Id);
            ReadingComment readingComment = Context.ReadingComments.Find(model.Comment.Id);

            if (temp == null || device == null || readingComment == null)
            {
                throw new NullReferenceException("Could not find the shell temperature");
            }

            // see if the temperature already has a comment
            ShellTemperatureComment exists = GetItem(model.Id);

            if (exists == null)
            {
                throw new NullReferenceException("Could not find the item to update");
            }

            exists.Comment          = readingComment;
            exists.ShellTemp        = temp;
            exists.ShellTemp.Device = device;

            Context.SaveChanges();
            return(true);
        }
예제 #4
0
        public void Create()
        {
            Random random = new Random();

            int        temp       = random.Next(18, 25);
            int        lat        = random.Next(0, 54);
            int        lon        = random.Next(0, 10);
            DeviceInfo deviceInfo = deviceInfos[random.Next(0, deviceInfos.Length)];

            ShellTemp shellTemp = new ShellTemp(Guid.NewGuid(), temp, DateTime.Now,
                                                lat, lon, deviceInfo);

            Positions position = positions[random.Next(0, positions.Count)];

            Context.ShellTemperatures.Add(shellTemp);

            ShellTemperaturePosition shellTemperaturePosition = new ShellTemperaturePosition
            {
                ShellTemp = shellTemp,
                Position  = position
            };

            bool created = shellTemperaturePositionRepository.Create(shellTemperaturePosition);

            Assert.IsTrue(created);
        }
예제 #5
0
        public bool Create(ShellTemperaturePosition model)
        {
            // Validation
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model), "The model supplied is null");
            }
            if (model.ShellTemp?.Device == null || model.Position == null)
            {
                throw new ArgumentNullException(nameof(model), "The model supplied is invalid as it has null references");
            }

            ShellTemp  dbShellTemp      = Context.ShellTemperatures.Find(model.ShellTemp.Id);
            DeviceInfo dbDeviceInfo     = Context.DevicesInfo.Find(model.ShellTemp.Device.Id);
            Positions  dbDevicePosition = Context.Positions.Find(model.Position.Id);

            if (dbDeviceInfo != null && dbShellTemp != null)
            {
                model.ShellTemp        = dbShellTemp;
                model.ShellTemp.Device = dbDeviceInfo;
                model.Position         = dbDevicePosition;

                Context.ShellTemperaturePositions.Add(model);
                Context.SaveChanges();
                return(true);
            }

            return(false);
        }
        public void Create_Test()
        {
            // Arrange
            Random random = new Random();

            for (int i = 0; i < 20; i++)
            {
                ShellTemp temp = new ShellTemp
                {
                    Id               = Guid.NewGuid(),
                    Latitude         = random.Next(0, 10000),
                    Longitude        = random.Next(0, 10000),
                    RecordedDateTime = DateTime.Now,
                    Temperature      = random.Next(0, 30),
                    Device           = new DeviceInfo
                    {
                        Id            = Guid.NewGuid(),
                        DeviceAddress = "Eleven",
                        DeviceName    = "Strokes"
                    }
                };

                // Act
                bool created = temperatureRepository.Create(temp);

                // Assert
                Assert.IsTrue(created);
            }
        }
 public void GetSingle()
 {
     foreach (var shellTemp in temps)
     {
         ShellTemp dbShellTemp = temperatureRepository.GetItem(shellTemp.Id);
         Assert.IsNotNull(dbShellTemp);
     }
 }
        public async Task <IActionResult> Get([FromRoute] Guid id)
        {
            ShellTemp shellTemp = await _shellTempRepository.GetItem(id);

            if (shellTemp == null)
            {
                return(BadRequest("Could not find the shell temperature item"));
            }

            return(Ok(shellTemp));
        }
        public async Task <IActionResult> Update(ShellTemp shellTemp)
        {
            bool updated = await _shellTempRepository.Update(shellTemp);

            if (!updated)
            {
                return(BadRequest("Could not update the shell temperature"));
            }

            return(Ok());
        }
예제 #10
0
        public void Setup()
        {
            Context = GetShellDb();
            shellTemperaturePositionRepository = new ShellTemperaturePositionRepository(Context);

            shellTemps.Clear();
            positions.Clear();
            shellTemperaturePositions.Clear();

            Random random = new Random();

            string[] pos = new[]
            {
                "Ironman", "Thor", "StarLord", "Rocket", "Gamora", "Thanos",
                "Quill", "Groot", "BlackWidow", "Hawkeye"
            };

            Context.DevicesInfo.AddRange(deviceInfos);

            for (int i = 0; i < 10; i++)
            {
                int        temp       = random.Next(18, 25);
                int        lat        = random.Next(0, 54);
                int        lon        = random.Next(0, 10);
                DeviceInfo deviceInfo = deviceInfos[random.Next(0, deviceInfos.Length)];

                ShellTemp shellTemp = new ShellTemp(Guid.NewGuid(), temp, DateTime.Now,
                                                    lat, lon, deviceInfo);

                shellTemps.Add(shellTemp);

                Positions position = new Positions(pos[i]);
                positions.Add(position);

                ShellTemperaturePosition shellTemperaturePosition = new ShellTemperaturePosition
                {
                    ShellTemp = shellTemp,
                    Position  = position
                };
                shellTemperaturePositions.Add(shellTemperaturePosition);

                Context.Positions.Add(position);
                Context.ShellTemperatures.Add(shellTemp);
                Context.ShellTemperaturePositions.Add(shellTemperaturePosition);
            }

            Context.SaveChanges();
        }
        public void Create()
        {
            // Arrange
            Random random = new Random();

            ShellTemp      shellTemp      = shellTemps[random.Next(0, shellTemps.Count)];
            ReadingComment readingComment = readingComments[random.Next(0, readingComments.Count)];

            ShellTemperatureComment newComment = new ShellTemperatureComment(readingComment, shellTemp);

            // ACt
            bool created = shellTemperatureCommentRepository.Create(newComment);

            // Assert
            Assert.IsTrue(created);
        }
        public void Delete_InUse()
        {
            DeviceInfo deviceInfo = devices.FirstOrDefault();

            Assert.IsNotNull(deviceInfo);

            // Add in a new shell temp to the db for testing
            ShellTemp shellTemp = new ShellTemp(Guid.NewGuid(), 22.2, DateTime.Now, 54, 1, deviceInfo);

            Context.ShellTemperatures.Add(shellTemp);
            Context.SaveChanges();

            bool deleted = deviceRepository.Delete(deviceInfo.Id);

            Assert.IsFalse(deleted);
        }
예제 #13
0
        public bool Delete(Guid id)
        {
            DeviceInfo dev = Context.DevicesInfo.FirstOrDefault(x => x.Id.Equals(id));

            if (dev == null)
            {
                return(false);
            }

            ShellTemp shellTemp = Context.ShellTemperatures.FirstOrDefault(x => x.Device.Id == id);

            if (shellTemp != null)
            {
                return(false); // can't delete as this device is linked to a temp
            }
            Context.DevicesInfo.Remove(dev);
            return(true);
        }
        public void Setup()
        {
            Context = GetShellDb();
            shellTemperatureCommentRepository = new ShellTemperatureCommentRepository(Context);

            Random random = new Random();

            shellTemps.Clear();
            readingComments.Clear();
            shellTemperatureComments.Clear();

            string[] comments = new[]
            {
                "IronMan", "Thor", "CaptainMarvel", "Thanos", "BlackWidow", "Wolverine", "JonSnow", "Deadpool",
                "Rocket", "Quill"
            };

            for (int i = 0; i < 10; i++)
            {
                int temp = random.Next(18, 25);
                int lat  = random.Next(0, 55);
                int lon  = random.Next(0, 10);

                ShellTemp shellTemp = new ShellTemp(Guid.NewGuid(), temp, DateTime.Now,
                                                    lat, lon, deviceInfo[random.Next(0, deviceInfo.Length)]);
                shellTemps.Add(shellTemp);

                ReadingComment readingComment = new ReadingComment(comments[i]);
                readingComments.Add(readingComment);

                ShellTemperatureComment shellTemperatureComment =
                    new ShellTemperatureComment(readingComment, shellTemp);
                shellTemperatureComments.Add(shellTemperatureComment);

                Context.ReadingComments.Add(readingComment);
                Context.ShellTemperatures.Add(shellTemp);
                Context.ShellTemperatureComments.Add(shellTemperatureComment);
            }

            Context.SaveChanges();
        }
        public void Create_NullDevice_Test()
        {
            // Arrange
            Random random = new Random();

            for (int i = 0; i < 20; i++)
            {
                ShellTemp temp = new ShellTemp
                {
                    Id               = Guid.NewGuid(),
                    Latitude         = random.Next(0, 10000),
                    Longitude        = random.Next(0, 10000),
                    RecordedDateTime = DateTime.Now,
                    Temperature      = random.Next(0, 30),
                    Device           = null
                };

                Assert.Throws <ArgumentNullException>(delegate
                {
                    temperatureRepository.Create(temp);
                });
            }
        }
        public async Task <IActionResult> Create(ShellTemperatureRecord record)
        {
            try
            {
                if (!record.RecordedDateTime.HasValue)
                {
                    record.RecordedDateTime = DateTime.Now;
                }

                ShellTemp shellTemp = new ShellTemp(record.Id, record.Temperature, (DateTime)record.RecordedDateTime, record.Latitude, record.Longitude, record.DeviceInfo);
                bool      result    = await _shellTempRepository.Create(shellTemp);

                if (!result)
                {
                    return(BadRequest());
                }

                return(Ok());
            }
            catch (ArgumentNullException ex)
            {
                return(BadRequest());
            }
        }