public async Task <object> InsertData(LandmarkEntity landmarkEntity) { await using (CourierDbContext courierDbContext = new CourierDbContext()) { var dist = CalculateDistance(landmarkEntity.Latitude, landmarkEntity.Longitude); LandmarkDatum landmarkDatum = new LandmarkDatum() { LandmarkName = landmarkEntity.Name, Address = landmarkEntity.Address, Latitude = landmarkEntity.Latitude, Longitude = landmarkEntity.Longitude, ContactNumber = landmarkEntity.Phonenumber, PointOrder = 1, Distance = dist, CreatedDate = DateTime.Now }; var data = courierDbContext.LandmarkData.Select(x => new LandmarkDatum() { LandmarkName = x.LandmarkName, Address = x.Address, Latitude = x.Latitude, Longitude = x.Longitude, ContactNumber = x.ContactNumber, PointOrder = x.PointOrder, Distance = x.Distance, CreatedDate = x.CreatedDate }).ToList(); if (data.Count > 0) { await DeleteData(); } data.Add(landmarkDatum); var sortedData = data.OrderBy(x => x.Distance).ToList(); foreach (var(value, index) in sortedData.Select((v, i) => (v, i))) { value.PointOrder = index + 1; courierDbContext.LandmarkData.Add(value); await courierDbContext.SaveChangesAsync(); } return(new { Message = "Landmark added successfuly." }); } }
public async Task <object> AddLandmark(LandmarkEntity landmarkEntity) { var dist = CalculateDistance(landmarkEntity.Latitude, landmarkEntity.Longitude); if (dist == 0) { return new { Message = "Latitude and Longtitude is matches with the Headquaters. Please modify and try once again." } } ; if (dist < 1000) { return new { Message = "Distance is " + dist.ToString() + " but it should be greater than 1000 meters. please try again." } } ; var res = await InsertData(landmarkEntity); return(res); }
public async Task <IActionResult> AddLandmark([FromBody] LandmarkEntity landmarkEntity) { var reponse = await _courierService.AddLandmark(landmarkEntity); return(this.Ok(reponse)); }