Now even map technic is implemented

This commit is contained in:
2023-04-23 12:52:46 +02:00
parent 0b69e0b13a
commit c9ee3e7f14
8 changed files with 386 additions and 7 deletions

View File

@ -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;
}
}
}
}