Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
uzbekdev1 committed Sep 1, 2020
1 parent c5509d4 commit 9e1c935
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 171 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/dotnetcore.yml

This file was deleted.

12 changes: 6 additions & 6 deletions src/CT.Api/Controllers/AirportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ namespace CT.Api.Controllers
[ApiController]
public class AirportController : ControllerBase
{
private readonly RestService _service;
private readonly AirportService _service;

public AirportController(RestService service)
public AirportController(AirportService service)
{
_service = service;
}
}

[HttpPost]
public async Task<IActionResult> CaculateDistance([FromBody]DistanceInputModel model)
public async Task<IActionResult> CaculateDistance([FromBody] DistanceInputModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var sourceAir = await _service.GetAirport(model.SourceCode) ?? throw new AppException("Source entry is null");
var targetAir = await _service.GetAirport(model.TargetCode) ?? throw new AppException("Target entry is null");
var sourceAir = await _service.GetAirport(model.SourceCode) ?? throw new Exception("Source entry is null");
var targetAir = await _service.GetAirport(model.TargetCode) ?? throw new Exception("Target entry is null");
var betweenDist = sourceAir.location.GetDistance(targetAir.location);

return Ok(betweenDist);
Expand Down
18 changes: 11 additions & 7 deletions src/CT.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ namespace CT.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}

public IConfiguration Configuration { get; }

public IHostingEnvironment Environment { get; set; }

public virtual void ConfigureServices(IServiceCollection services)
{
//config
Expand All @@ -33,7 +36,7 @@ public virtual void ConfigureServices(IServiceCollection services)
services.AddSingleton(config);

//services
services.AddHttpClient<RestService>(a=>
services.AddHttpClient<AirportService>(a =>
{
a.BaseAddress = new Uri(config.SourceUrl);
});
Expand All @@ -54,25 +57,26 @@ public virtual void ConfigureServices(IServiceCollection services)

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app)
{
if (env.IsDevelopment())
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

app.UseCors("All");

app.UseMvc();

//swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "CT API V1");
c.RoutePrefix = "";
});

app.UseMvc();

}
}
}
5 changes: 4 additions & 1 deletion src/CT.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"System": "Information",
"Microsoft": "Information"
}
}
},
"Config": {
"SourceUrl": "https://places-dev.cteleport.com"
}
}
5 changes: 4 additions & 1 deletion src/CT.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"Default": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Config": {
"SourceUrl": "https://places.cteleport.com"
}
}
2 changes: 1 addition & 1 deletion src/CT.Common/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CT.Common
public class AppConfig
{

public string SourceUrl { get; set; } = "https://places-dev.cteleport.com";
public string SourceUrl { get; set; }

}
}
26 changes: 0 additions & 26 deletions src/CT.Common/AppException.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/CT.Common/Extensions/GeoExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CT.Common.Dto;
using System;
using System;
using System.Collections.Generic;
using System.Text;
using CT.Common.Models;

namespace CT.Common.Extensions
{
Expand All @@ -15,10 +15,10 @@ private static double ToRadians(this double val)
public static double GetDistance(this GeoLocation source, GeoLocation target)
{
if (source == null)
throw new AppException("Argument source is null");
throw new Exception("Argument source is null");

if (target == null)
throw new AppException("Argument target is null");
throw new Exception("Argument target is null");

//Calculate radius of earth
var R = 3960;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CT.Common.Dto
{

public class GeoLocation
{

public double lon { get; set; }

public double lat { get; set; }

public GeoLocation()
{

}

public GeoLocation(double lon, double lat)
{
this.lat = lat;
this.lon = lon;
}

}

}
namespace CT.Common.Models
{

public class GeoLocation
{

public double lon { get; set; }

public double lat { get; set; }

public GeoLocation()
{

}

public GeoLocation(double lon, double lat)
{
this.lat = lat;
this.lon = lon;
}

}

}
74 changes: 36 additions & 38 deletions src/CT.Service/RestService.cs → src/CT.Service/AirportService.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
using CT.Common;
using CT.Service.Dto;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace CT.Service
{

public class RestService
{

private readonly HttpClient _client;

public RestService(HttpClient client)
{
_client = client;
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

/// <summary>
/// Airports are identified by 3-letter IATA code.
/// </summary>
/// <param name="code">IATA code.</param>
/// <returns></returns>
public virtual async Task<AirportItem> GetAirport(string code)
{
var response = await _client.GetAsync($"/airports/{code}");

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsAsync<AirportItem>();
}

}

}
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using CT.Service.Dtos;

namespace CT.Service
{

public class AirportService
{

private readonly HttpClient _client;

public AirportService(HttpClient client)
{
_client = client;
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

/// <summary>
/// Airports are identified by 3-letter IATA code.
/// </summary>
/// <param name="code">IATA code.</param>
/// <returns></returns>
public virtual async Task<AirportDto> GetAirport(string code)
{
var response = await _client.GetAsync($"/airports/{code}");

response.EnsureSuccessStatusCode();

return await response.Content.ReadAsAsync<AirportDto>();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
using CT.Common.Dto;

namespace CT.Service.Dto
{
public class AirportItem
{

public string country { get; set; }

public string city_iata { get; set; }

public string iata { get; set; }

public string city { get; set; }

public string timezone_region_name { get; set; }

public string country_iata { get; set; }

public int rating { get; set; }

public string name { get; set; }

public GeoLocation location { get; set; }

public string type { get; set; }

public int hubs { get; set; }
}

}
using CT.Common.Models;

namespace CT.Service.Dtos
{
public class AirportDto
{

public string country { get; set; }

public string city_iata { get; set; }

public string iata { get; set; }

public string city { get; set; }

public string timezone_region_name { get; set; }

public string country_iata { get; set; }

public int rating { get; set; }

public string name { get; set; }

public GeoLocation location { get; set; }

public string type { get; set; }

public int hubs { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CT.Integration.Test
{

[TestClass]
public class RestTest
public class AirpotTests
{

[TestMethod]
Expand Down
Loading

0 comments on commit 9e1c935

Please sign in to comment.