예제 #1
0
        public async Task <IActionResult> GetLocation(int id)
        {
            Location location = await _context.Locations.Include(x => x.Photo).FirstOrDefaultAsync(x => x.Id == id);

            if (location == null)
            {
                return(BadRequest());
            }

            var locationForDisplayDto = location.Adapt <LocationForDisplayDto>();

            return(Ok(locationForDisplayDto));
        }
예제 #2
0
        public async Task UpdateSheriff()
        {
            var sheriffObject = await CreateSheriffUsingDbContext();

            var newLocation = new Location {
                Name = "5", Id = 50005, AgencyId = "645646464646464"
            };
            await Db.Location.AddAsync(newLocation);

            var newLocation2 = new Location {
                Name = "6", Id = 50006, AgencyId = "6456456464"
            };
            await Db.Location.AddAsync(newLocation2);

            await Db.SaveChangesAsync();

            Detach();

            var updateSheriff = sheriffObject.Adapt <SheriffWithIdirDto>();

            updateSheriff.FirstName   = "Al";
            updateSheriff.LastName    = "Hoyne";
            updateSheriff.BadgeNumber = "55";
            updateSheriff.Email       = "*****@*****.**";
            updateSheriff.Gender      = Gender.Other;

            //This object is only used for fetching.
            //updateSheriff.HomeLocation = new LocationDto { Name = "Als place2", Id = 5};
            updateSheriff.HomeLocationId = newLocation.Id;

            Detach();

            var controllerResult = await _controller.UpdateSheriff(updateSheriff);

            var response        = HttpResponseTest.CheckForValid200HttpResponseAndReturnValue(controllerResult);
            var sheriffResponse = response.Adapt <Sheriff>();

            Assert.Equal(updateSheriff.FirstName, sheriffResponse.FirstName);
            Assert.Equal(updateSheriff.LastName, sheriffResponse.LastName);
            Assert.Equal(updateSheriff.BadgeNumber, sheriffResponse.BadgeNumber);
            Assert.Equal(updateSheriff.Email, sheriffResponse.Email);
            Assert.Equal(updateSheriff.Gender, sheriffResponse.Gender);

            //Shouldn't be able to update any of the navigation properties here.
            Assert.Empty(sheriffResponse.AwayLocation);
            Assert.Empty(sheriffResponse.Training);
            Assert.Empty(sheriffResponse.Leave);
            //Set to invalid location, should be null.
            //WE didn't set the HomeLocationId here.
            Assert.NotNull(sheriffResponse.HomeLocation);

            Detach();

            updateSheriff.HomeLocationId = newLocation2.Id;
            updateSheriff.HomeLocation   = newLocation2.Adapt <LocationDto>();
            controllerResult             = await _controller.UpdateSheriff(updateSheriff);

            response = HttpResponseTest.CheckForValid200HttpResponseAndReturnValue(controllerResult);

            Detach();

            var controllerResult2 = await _controller.GetSheriffForTeam(sheriffResponse.Id);

            var response2 = HttpResponseTest.CheckForValid200HttpResponseAndReturnValue(controllerResult);

            Assert.NotNull(response2.HomeLocation);
            Assert.Equal(newLocation2.Id, response.HomeLocation.Id);
        }