Skip to content

Commit

Permalink
chore: Update namespaces and interfaces for CentralPennIncidentsFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
myty committed Jun 21, 2024
1 parent fb8b266 commit cd5ee2e
Show file tree
Hide file tree
Showing 23 changed files with 519 additions and 611 deletions.
109 changes: 50 additions & 59 deletions apps/func/src/CentralPennIncidentsFunc/API/Incidents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,68 @@
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace CentralPennIncidentsFunc.API
namespace CentralPennIncidentsFunc.API;

public class Incidents(
IFeedService feedService,
ILocationService locationService,
ILogger<Incidents> log,
IMapper mapper
)
{
public class Incidents
{
private readonly IFeedService _feedService;
private readonly ILocationService _locationService;
private readonly ILogger<Incidents> _log;
private readonly IMapper _mapper;
private readonly IFeedService _feedService = feedService;
private readonly ILocationService _locationService = locationService;
private readonly ILogger<Incidents> _log = log;
private readonly IMapper _mapper = mapper;

public Incidents(
IFeedService feedService,
ILocationService locationService,
ILogger<Incidents> log,
IMapper mapper
)
{
_feedService = feedService;
_locationService = locationService;
_log = log;
_mapper = mapper;
}
[Function("incidents")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req
)
{
_log.LogInformation(
"api/incidents - HTTP trigger function processed a request. {Request}",
req
);

[Function("incidents")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req
)
try
{
_log.LogInformation(
"api/incidents - HTTP trigger function processed a request. {Request}",
req
);
var incidents = await _feedService.GetIncidentsAsync();

try
{
var incidents = await _feedService.GetIncidentsAsync();
var incidentDtos = await Task.WhenAll(
incidents.Select(async i =>
{
var dto = _mapper.Map<Dtos.Incident>(i);

var incidentDtos = await Task.WhenAll(
incidents.Select(async i =>
if (string.IsNullOrWhiteSpace(i.Location))
{
var dto = _mapper.Map<Dtos.Incident>(i);

if (string.IsNullOrWhiteSpace(i.Location))
{
return dto;
}
return dto;
}

var locationEntity = await _locationService.GetLocationAsync(
i.Location,
i.Area
);
var locationEntity = await _locationService.GetLocationAsync(
i.Location,
i.Area
);

dto.GeocodeLocation =
(locationEntity?.Lat_VC.HasValue ?? false)
? _mapper.Map<Dtos.Location>(locationEntity)
: null;
dto.GeocodeLocation =
(locationEntity?.Lat_VC.HasValue ?? false)
? _mapper.Map<Dtos.Location>(locationEntity)
: null;

return dto;
})
);
return dto;
})
);

var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "application/json; charset=utf-8");
await response.WriteStringAsync(JsonSerializer.Serialize(incidentDtos));
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "application/json; charset=utf-8");
await response.WriteStringAsync(JsonSerializer.Serialize(incidentDtos));

return response;
}
catch (Exception ex)
{
_log.LogError(ex, "Execution Error: {Exception}", ex);
throw;
}
return response;
}
catch (Exception ex)
{
_log.LogError(ex, "Execution Error: {Exception}", ex);
throw;
}
}
}
39 changes: 19 additions & 20 deletions apps/func/src/CentralPennIncidentsFunc/Dtos/Incident.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using System;
using System.Text.Json.Serialization;

namespace CentralPennIncidentsFunc.Dtos
namespace CentralPennIncidentsFunc.Dtos;

public class Incident
{
public class Incident
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }

[JsonPropertyName("incident_dt")]
public DateTime IncidentDate { get; set; }
[JsonPropertyName("incident_dt")]
public DateTime IncidentDate { get; set; }

[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }

[JsonPropertyName("subType")]
public string SubType { get; set; }
[JsonPropertyName("subType")]
public string SubType { get; set; }

[JsonPropertyName("location")]
public string Location { get; set; }
[JsonPropertyName("location")]
public string Location { get; set; }

[JsonPropertyName("area")]
public string Area { get; set; }
[JsonPropertyName("area")]
public string Area { get; set; }

[JsonPropertyName("units_assigned")]
public string[] UnitsAssigned { get; set; }
[JsonPropertyName("units_assigned")]
public string[] UnitsAssigned { get; set; }

[JsonPropertyName("geocode_location")]
public Location GeocodeLocation { get; set; }
}
[JsonPropertyName("geocode_location")]
public Location GeocodeLocation { get; set; }
}
15 changes: 7 additions & 8 deletions apps/func/src/CentralPennIncidentsFunc/Dtos/Location.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Text.Json.Serialization;

namespace CentralPennIncidentsFunc.Dtos
namespace CentralPennIncidentsFunc.Dtos;

public class Location
{
public class Location
{
[JsonPropertyName("lat")]
public double Lat { get; set; }
[JsonPropertyName("lat")]
public double Lat { get; set; }

[JsonPropertyName("lng")]
public double Lng { get; set; }
}
[JsonPropertyName("lng")]
public double Lng { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using AutoMapper;

namespace CentralPennIncidentsFunc.Dtos.Mappings
namespace CentralPennIncidentsFunc.Dtos.Mappings;

public class IncidentProfile : Profile
{
public class IncidentProfile : Profile
public IncidentProfile()
{
public IncidentProfile()
{
CreateMap<Models.Incident, Incident>()
.ForMember(dst => dst.Id, opt => opt.MapFrom(src => src.GlobalId.Uid));
}
CreateMap<Models.Incident, Incident>()
.ForMember(dst => dst.Id, opt => opt.MapFrom(src => src.GlobalId.Uid));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using AutoMapper;

namespace CentralPennIncidentsFunc.Dtos.Mappings
namespace CentralPennIncidentsFunc.Dtos.Mappings;

public class LocationProfile : Profile
{
public class LocationProfile : Profile
public LocationProfile()
{
public LocationProfile()
{
CreateMap<Models.LocationEntity, Location>()
.ForMember(d => d.Lat, opt => opt.MapFrom(src => src.Lat_VC))
.ForMember(d => d.Lng, opt => opt.MapFrom(src => src.Lng_VC));
}
CreateMap<Models.LocationEntity, Location>()
.ForMember(d => d.Lat, opt => opt.MapFrom(src => src.Lat_VC))
.ForMember(d => d.Lng, opt => opt.MapFrom(src => src.Lng_VC));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -7,14 +6,9 @@

namespace CentralPennIncidentsFunc.IncidentProviders;

public abstract class BaseIncidentProvider : IIncidentProvider
public abstract class BaseIncidentProvider(IEnvironmentProvider env) : IIncidentProvider
{
private readonly IEnvironmentProvider _env;

public BaseIncidentProvider(IEnvironmentProvider env)
{
_env = env;
}
private readonly IEnvironmentProvider _env = env;

public string Source => _env.GetEnvironmentVariable($"IncidentSources__{Key}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@

namespace CentralPennIncidentsFunc.IncidentProviders;

public class LancasterIncidentProvider : BaseIncidentProvider
public class LancasterIncidentProvider(
IEnvironmentProvider env,
IDataCache<string, IEnumerable<Incident>> feedCache,
IHttpClientFactory httpClientFactory
) : BaseIncidentProvider(env)
{
private readonly IDataCache<string, IEnumerable<Incident>> _feedCache;
private readonly HttpClient _client;

public LancasterIncidentProvider(
IEnvironmentProvider env,
IDataCache<string, IEnumerable<Incident>> feedCache,
IHttpClientFactory httpClientFactory
)
: base(env)
{
_feedCache = feedCache;
_client = httpClientFactory.CreateClient();
}
private readonly IDataCache<string, IEnumerable<Incident>> _feedCache = feedCache;
private readonly HttpClient _client = httpClientFactory.CreateClient();

public override string Key => "Lancaster";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@

namespace CentralPennIncidentsFunc.IncidentProviders;

public class YorklIncidentProvider : BaseIncidentProvider
public class YorklIncidentProvider(
IEnvironmentProvider env,
IDataCache<string, IEnumerable<Incident>> feedCache,
IHttpClientFactory httpClientFactory
) : BaseIncidentProvider(env)
{
private readonly IDataCache<string, IEnumerable<Incident>> _feedCache;
private readonly HttpClient _client;

public YorklIncidentProvider(
IEnvironmentProvider env,
IDataCache<string, IEnumerable<Incident>> feedCache,
IHttpClientFactory httpClientFactory
)
: base(env)
{
_feedCache = feedCache;
_client = httpClientFactory.CreateClient();
}
private readonly IDataCache<string, IEnumerable<Incident>> _feedCache = feedCache;
private readonly HttpClient _client = httpClientFactory.CreateClient();

public override string Key => "York";

Expand Down
12 changes: 4 additions & 8 deletions apps/func/src/CentralPennIncidentsFunc/Interfaces/IDataCache.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Collections.Generic;
using CentralPennIncidentsFunc.Models;
namespace CentralPennIncidentsFunc.Interfaces;

namespace CentralPennIncidentsFunc.Interfaces
public interface IDataCache<TKey, TValue>
{
public interface IDataCache<TKey, TValue>
{
bool TryGetValue(TKey key, out TValue value);
void SaveValue(TKey key, TValue value);
}
bool TryGetValue(TKey key, out TValue value);
void SaveValue(TKey key, TValue value);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

namespace CentralPennIncidentsFunc.Interfaces
namespace CentralPennIncidentsFunc.Interfaces;

public interface IEnvironmentProvider
{
public interface IEnvironmentProvider
{
string GetEnvironmentVariable(
string variable,
EnvironmentVariableTarget target = EnvironmentVariableTarget.Process
);
}
string GetEnvironmentVariable(
string variable,
EnvironmentVariableTarget target = EnvironmentVariableTarget.Process
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using CentralPennIncidentsFunc.Models;
using Microsoft.Azure.Cosmos.Table;

namespace CentralPennIncidentsFunc.Interfaces
namespace CentralPennIncidentsFunc.Interfaces;

public interface ILocationRepository
{
public interface ILocationRepository
{
Task SaveAsync(ITableEntity locationEntity);
Task<LocationEntity> FindByAddressAsync(string location, string area);
}
Task SaveAsync(ITableEntity locationEntity);
Task<LocationEntity> FindByAddressAsync(string location, string area);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Threading.Tasks;
using CentralPennIncidentsFunc.Models;

namespace CentralPennIncidentsFunc.Interfaces
namespace CentralPennIncidentsFunc.Interfaces;

public interface ILocationService
{
public interface ILocationService
{
Task<LocationEntity> GetLocationAsync(string location, string area);
}
Task<LocationEntity> GetLocationAsync(string location, string area);
}
Loading

0 comments on commit cd5ee2e

Please sign in to comment.