Now even map technic is implemented
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using BlazorSyncfusionCrm.Server.Data;
|
||||
using BlazorSyncfusionCrm.Shared;
|
||||
using GoogleMaps.LocationServices;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -39,6 +40,8 @@ namespace BlazorSyncfusionCrm.Server.Controllers
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Contact>> CreateContact(Contact contact)
|
||||
{
|
||||
SetLatLong(contact);
|
||||
|
||||
_context.Contacts.Add(contact);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok(contact);
|
||||
@ -59,7 +62,8 @@ namespace BlazorSyncfusionCrm.Server.Controllers
|
||||
dbContact.DateOfBirth = contact.DateOfBirth;
|
||||
dbContact.Place = contact.Place;
|
||||
dbContact.DateUpdated = DateTime.Now;
|
||||
|
||||
SetLatLong(dbContact);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return Ok(contact);
|
||||
@ -83,5 +87,29 @@ namespace BlazorSyncfusionCrm.Server.Controllers
|
||||
return await GetAllContacts();
|
||||
|
||||
}
|
||||
|
||||
[HttpGet("map")]
|
||||
public async Task<ActionResult<List<Contact>>> GetMapContacts()
|
||||
{
|
||||
return await _context.Contacts
|
||||
.Where(c => !c.IsDeleted && c.Longitude != null && c.Latitude != null)
|
||||
.ToListAsync();
|
||||
}
|
||||
MapPoint GetLatLong(Contact contact)
|
||||
{
|
||||
var gls = new GoogleLocationService("AIzaSyAGh2TGRfQu1UURz2PdJd4DWFFcOpT-jYA");
|
||||
var latLong = gls.GetLatLongFromAddress(contact.Place);
|
||||
return latLong;
|
||||
}
|
||||
|
||||
void SetLatLong(Contact contact)
|
||||
{
|
||||
var latLong = GetLatLong(contact);
|
||||
if(latLong != null)
|
||||
{
|
||||
contact.Latitude = latLong.Latitude;
|
||||
contact.Longitude = latLong.Longitude;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user