コード例 #1
0
        public async Task FindAllPagedTest()
        {
            Assert.NotNull(dbc = new DBContext(settings.Database.ConnectionString, logger));
            ContactRepository contactRepos = new ContactRepository(settings, logger, dbc);
            CityRepository    cityRepos    = new CityRepository(settings, logger, dbc);
            StateRepository   stateRepos   = new StateRepository(settings, logger, dbc);

            IPager <Contact> contacts = await contactRepos.FindAll(new Pager <Contact>() { PageNbr = 2, PageSize = 5 });

            Assert.NotNull(contacts);
            Assert.True(contacts.RowCount > 0);
            Assert.NotNull(contacts.Entities);
            IPager <City> cities = await cityRepos.FindAll(new Pager <City>() { PageNbr = 2, PageSize = 5 });

            Assert.NotNull(cities.Entities);
            Assert.True(cities.RowCount > 0);
            Assert.NotNull(cities.Entities);
            IPager <State> states = await stateRepos.FindAll(new Pager <State>() { PageNbr = 2, PageSize = 5 });

            Assert.NotNull(states.Entities);
            Assert.True(states.RowCount > 0);
            Assert.NotNull(states.Entities);

            dbc.Close();
        }
コード例 #2
0
        public City ValidateCity(decimal lat, decimal longitude)
        {
            //look through cities and ensure one is close enough for check in
            var  cities       = CityRepository.FindAll();
            City selectedCity = null;

            foreach (var city in cities)
            {
                //use microsofts haversine formula (returns metres)
                //then assign current coordinates to work out the distance
                var cityCoordinate = new GeoCoordinate((double)city.LatPos,
                                                       (double)city.LongPos);
                var currentCoordinate = new GeoCoordinate(
                    (double)lat, (double)longitude);
                var distance = cityCoordinate.GetDistanceTo(currentCoordinate);
                if (distance < Constants.BookingMaxRangeFromCityCentre)
                {
                    //loop through all stored cities and if city is within
                    //the city limit assign the variable to return
                    selectedCity = city;

                    break;
                }
            }
            return(selectedCity);
        }
コード例 #3
0
        public HttpResponseMessage GetCitiesByProvinceID(int provinceId)
        {
            ResultMsg resultMsg = new ResultMsg();

            try
            {
                string provinceCode = string.Empty;
                using (IProvince repository = new ProvinceRepository())
                {
                    provinceCode = repository.Find(provinceId).ProvinceCode;
                }
                using (ICity repository = new CityRepository())
                {
                    var list = repository.FindAll(o => o.ProvinceCode == provinceCode);
                    resultMsg.code = 0;
                    resultMsg.data = list;
                }
            }
            catch (Exception ex)
            {
                resultMsg.code = (int)StatusCodeEnum.Error;
                resultMsg.msg  = ex.Message;
            }
            return(resultMsg.toJson());
        }
コード例 #4
0
        public IEnumerable <CityDTO> GetCity()
        {
            List <CityDTO> dtos = new List <CityDTO>();

            foreach (City c in cityRepository.FindAll())
            {
                CityDTO dto = new CityDTO();
                dto.CityId    = c.CityId;
                dto.Name      = c.Name;
                dto.Latitude  = c.Latitude;
                dto.Longitude = c.Longitude;
                dtos.Add(dto);
            }
            return(dtos);
        }
コード例 #5
0
        public async Task FindAllTest()
        {
            Assert.NotNull(dbc = new DBContext(settings.Database.ConnectionString, logger));
            ContactRepository contactRepos = new ContactRepository(settings, logger, dbc);
            CityRepository    cityRepos    = new CityRepository(settings, logger, dbc);
            StateRepository   stateRepos   = new StateRepository(settings, logger, dbc);

            ICollection <Contact> contacts = await contactRepos.FindAll();

            Assert.NotEmpty(contacts);
            ICollection <City> cities = await cityRepos.FindAll();

            Assert.NotEmpty(cities);
            ICollection <State> states = await stateRepos.FindAll();

            Assert.NotEmpty(states);

            dbc.Close();
        }
コード例 #6
0
ファイル: CarService.cs プロジェクト: cpt331/car-share-api
        public UpdateCarResponse UpdateCar(UpdateCarRequest request)
        {
            //this method allows cars to be updated after passing some basic checks

            //validate the a car category exists and fail if it doesnt
            var category = CarCategoryRepository.Find(request.CarCategory);

            if (category == null)
            {
                return new UpdateCarResponse
                       {
                           Message = $"Category {request.CarCategory} does not exist",
                           Success = false
                       }
            }
            ;

            //validate the a car exists and fail if it doesnt
            Car car;

            if (request.Id.HasValue)
            {
                car = CarRepository.Find(request.Id.Value);

                if (car == null)
                {
                    return new UpdateCarResponse
                           {
                               Message = $"Vehicle {request.Id} does not exist",
                               Success = false
                           }
                }
                ;
            }
            else
            {
                car = new Car();
            }

            //look through cities and ensure one is close enough for check in
            var  cities       = CityRepository.FindAll();
            City selectedCity = null;

            foreach (var city in cities)
            {
                //use microsofts haversine formula (returns metres)
                var cityCoordinate    = new GeoCoordinate((double)city.LatPos, (double)city.LongPos);
                var currentCoordinate = new GeoCoordinate((double)request.LatPos, (double)request.LongPos);
                var distance          = cityCoordinate.GetDistanceTo(currentCoordinate);
                if (distance < Constants.BookingMaxRangeFromCityCentre)
                {
                    selectedCity = city;

                    break;
                }
            }

            //validates that a selected city exists
            if (selectedCity == null)
            {
                return(new UpdateCarResponse
                {
                    Message = "No cities are within a " +
                              $"{Constants.BookingMaxRangeFromCityCentre}m radius",
                    Success = false
                });
            }

            //assigns car values based on the parsed request to change the car
            car.CarCategory  = request.CarCategory;
            car.Make         = request.Make;
            car.Model        = request.Model;
            car.Status       = request.Status;
            car.Suburb       = selectedCity.CityName;
            car.LatPos       = request.LatPos;
            car.LongPos      = request.LongPos;
            car.Transmission = request.Transmission;

            //switch to determine whether the car needs to be updated or added
            //to the car table

            if (request.Id.HasValue)
            {
                var updatedCar = CarRepository.Update(car);
            }
            else
            {
                var updatedCar = CarRepository.Add(car);
            }

            //message returned after car has been updated

            var response = new UpdateCarResponse
            {
                Success = true,
                Message = $"{request.Make} {request.Model} has been updated",
                Errors  = null
            };

            return(response);
        }
コード例 #7
0
 public async Task <List <CityDTO> > FindAll()
 {
     return((await CityRepository.FindAll())
            .Select(x => CityDTO.FromCity(x))
            .ToList());
 }
コード例 #8
0
        public void AddTest()
        {
            int    key        = 0;
            string naturalKey = String.Empty;

            using (DBContext db = new DBContext(settings.Database.ConnectionString, logger))
            {
                Assert.NotNull(db);
                ContactRepository contactRepos = new ContactRepository(settings, logger, db);
                CityRepository    cityRepos    = new CityRepository(settings, logger, db);
                StateRepository   stateRepos   = new StateRepository(settings, logger, db);

                #region Add Contact Test
                Contact contact = new Contact()
                {
                    FirstName = "New",
                    LastName  = "User",
                    Address1  = "Address1",
                    Address2  = "Address2",
                    CellPhone = "8005551212",
                    HomePhone = "8005551212",
                    WorkPhone = "8005551212",
                    Notes     = String.Empty,
                    ZipCode   = "99999",
                    EMail     = "*****@*****.**",
                    CityId    = 1
                };
                ICollection <Contact> contacts = contactRepos.FindAll();
                Assert.Null(contacts.Where(c => c.LastName == contact.LastName && c.FirstName == contact.FirstName).FirstOrDefault());
                key = (int)contactRepos.Add(contact);
                Assert.True(key > 0);
                Assert.NotNull(contactRepos.FindByPK(new PrimaryKey()
                {
                    Key = key
                }));
                #endregion

                #region Add City Test
                City city = new City()
                {
                    Name    = "New City",
                    StateId = "FL"
                };
                ICollection <City> cities = cityRepos.FindAll();
                Assert.Null(cities.Where(c => c.Name == city.Name).FirstOrDefault());
                key = (int)cityRepos.Add(city);
                Assert.True(key > 0);
                Assert.NotNull(cityRepos.FindByPK(new PrimaryKey()
                {
                    Key = key
                }));
                #endregion

                #region Add State Test
                State newState = new State()
                {
                    Id   = "ZZ",
                    Name = "New State"
                };
                State state = stateRepos.FindByPK(new PrimaryKey()
                {
                    Key = newState.Id, IsIdentity = false
                });
                Assert.Null(state);
                naturalKey = (string)stateRepos.Add(newState);
                Assert.True(naturalKey == (string)newState.PK.Key);
                Assert.NotNull(stateRepos.FindByPK(new PrimaryKey()
                {
                    Key = newState.Id, IsIdentity = false
                }));
                #endregion
            }
        }
コード例 #9
0
        public async Task AddTest()
        {
            int key = 0;

            using (DBContext db = new DBContext(settings.Database.ConnectionString, logger))
            {
                Assert.NotNull(db);
                ContactRepository        contactRepos        = new ContactRepository(settings, logger, db);
                CityRepository           cityRepos           = new CityRepository(settings, logger, db);
                StateRepository          stateRepos          = new StateRepository(settings, logger, db);
                ProjectContactRepository projectContactRepos = new ProjectContactRepository(settings, logger, db);

                #region Add Contact Test
                Contact contact = new Contact()
                {
                    FirstName = "New",
                    LastName  = "User",
                    Address1  = "Address1",
                    Address2  = "Address2",
                    CellPhone = "8005551212",
                    HomePhone = "8005551212",
                    WorkPhone = "8005551212",
                    Notes     = String.Empty,
                    ZipCode   = "99999",
                    EMail     = "*****@*****.**",
                    CityId    = 1
                };
                ICollection <Contact> contacts = await contactRepos.FindAll();

                Assert.Null(contacts.Where(c => c.LastName == contact.LastName && c.FirstName == contact.FirstName).FirstOrDefault());
                key = (int)await contactRepos.Add(contact);

                Assert.True(key > 0);
                Assert.NotNull(await contactRepos.FindByPK(new PrimaryKey()
                {
                    Key = key
                }));
                #endregion

                #region Add City Test
                City city = new City()
                {
                    Name    = "New City",
                    StateId = "FL"
                };


                ICollection <City> cities = await cityRepos.FindAll();

                Assert.Null(cities.Where(c => c.Name == city.Name).FirstOrDefault());
                key = (int)await cityRepos.Add(city);

                Assert.True(key > 0);
                Assert.NotNull(await cityRepos.FindByPK(new PrimaryKey()
                {
                    Key = key
                }));
                #endregion

                #region Add State Test
                State newState = new State()
                {
                    Id   = "ZZ",
                    Name = "New State"
                };

                State state = await stateRepos.FindByPK(new PrimaryKey()
                {
                    Key = newState.Id
                });

                Assert.Null(state);
                key = (int)await stateRepos.Add(newState);

                Assert.True(key > 0);
                Assert.NotNull(await stateRepos.FindByPK(new PrimaryKey()
                {
                    Key = newState.Id
                }));
                #endregion

                #region Add ProjectContact Test
                ProjectContact projectContact = new ProjectContact()
                {
                    ProjectId = 2,
                    ContactId = 23
                };
                ICollection <ProjectContact> projectContacts = await projectContactRepos.FindAll();

                Assert.Null(projectContacts.Where(c => c.ProjectId == projectContact.ProjectId && c.ContactId == projectContact.ContactId).FirstOrDefault());
                key = (int)await projectContactRepos.Add(projectContact);           // returns number of rows added

                Assert.True(key > 0);
                Assert.NotNull(await projectContactRepos.FindByPK(new PrimaryKey()
                {
                    CompositeKey = new object[] { projectContact.ProjectId, projectContact.ContactId }, IsComposite = true
                }));
                #endregion
            }
        }