Skip to content

Commit

Permalink
Refactoring (records)
Browse files Browse the repository at this point in the history
  • Loading branch information
rstropek committed Sep 20, 2022
1 parent 9a31546 commit 2a02d8d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 62 deletions.
9 changes: 2 additions & 7 deletions StarshipTraveler.Api/Controllers/ConnectionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public class ConnectionsController : ControllerBase
{
[HttpGet]
public IEnumerable<Connection> Connections() =>
SampleData.Connections.Union(SampleData.Connections.Select(c => new Connection
{
From = c.To,
To = c.From,
Distance = c.Distance,
Price = c.Price
})).ToArray();
SampleData.Connections.Union(SampleData.Connections.Select(
c => new Connection(c.To, c.From, c.Distance, c.Price))).ToArray();
}
20 changes: 10 additions & 10 deletions StarshipTraveler.Api/Data/SampleData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public static class SampleData

public static Connection[] Connections { get; } = new[]
{
new Connection { From = "Decapod 10", To = "Earth", Distance = 1000, Price = 100m },
new Connection { From = "Cybertron", To = "Earth", Distance = 100, Price = 375.75m },
new Connection { From = "Krypton", To = "Earth", Distance = 12300, Price = 7499.99m },
new Connection { From = "Tatooine", To = "Vulcan", Distance = 5200, Price = 89.90m },
new Connection { From = "Earth", To = "Pandora", Distance = 3750, Price = 301.01m },
new Connection { From = "Earth", To = "Magrathea", Distance = 4242, Price = 4242.42m },
new Connection { From = "Arrakis", To = "Decapod 10", Distance = 7300, Price = 1349.90m },
new Connection { From = "Arrakis", To = "Pandora", Distance = 7300, Price = 1349.90m },
new Connection { From = "Pandora", To = "Magrathea", Distance = 4242, Price = 4242.42m },
new Connection { From = "Tatooine", To = "Krypton", Distance = 1234, Price = 1345.67m },
new Connection("Decapod 10", "Earth", 1000, 100m),
new Connection("Cybertron", "Earth", 100, 375.75m),
new Connection("Krypton", "Earth", 12300, 7499.99m),
new Connection("Tatooine", "Vulcan", 5200, 89.90m),
new Connection("Earth", "Pandora", 3750, 301.01m),
new Connection("Earth", "Magrathea", 4242, 4242.42m),
new Connection("Arrakis", "Decapod 10", 7300, 1349.90m),
new Connection("Arrakis", "Pandora", 7300, 1349.90m),
new Connection("Pandora", "Magrathea", 4242, 4242.42m),
new Connection("Tatooine", "Krypton", 1234, 1345.67m),
};
}
4 changes: 2 additions & 2 deletions StarshipTraveler.Client/Data/StarshipApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace StartshipTraveler.Client.Data;
public class StarshipApi : StarshipApiBase
{
public StarshipApi(HttpClient client, IWebAssemblyHostEnvironment hostEnvironment)
: base(client)
{
// Note Blazor environment. For more details see
// https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments?view=aspnetcore-5.0
var apiUrl = hostEnvironment.IsDevelopment() ? "http://localhost:5000/api/" : "https://starshipapi.azurewebsites.net/api/";

client.BaseAddress = new Uri(apiUrl);
Client = client;
Client.BaseAddress = new Uri(apiUrl);
}
}
33 changes: 4 additions & 29 deletions StarshipTraveler.Model/Model.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ComponentModel.DataAnnotations;

namespace StarshipTraveler.Model;

public class Base
{
public string? Name { get; set; }
public string? Image { get; set; }

public Base() { }

public Base(string name, string image)
{
Name = name;
Image = image;
}
}
public record Base(string Name, string Image);

public class GraphBase : Base
public record GraphBase : Base
{
public GraphBase() { }

public GraphBase(uint graphID, string name, string image) : base(name, image)
{
GraphID = graphID;
Expand Down Expand Up @@ -62,13 +46,4 @@ public enum TimeRelation
Upcoming
}

public class Connection : IEquatable<Connection>
{
public string? From { get; set; }
public string? To { get; set; }
public int Distance { get; set; }
public decimal Price { get; set; }

public bool Equals(Connection other) =>
From == other.From && To == other.To && Distance == other.Distance && Price == other.Price;
}
public record Connection(string From, string To, int Distance, decimal Price);
12 changes: 5 additions & 7 deletions StarshipTraveler.Model/StarshipApi.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using StarshipTraveler.Model;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace StartshipTraveler.Model;

public abstract class StarshipApiBase : IStarshipApi
{
private HttpClient? client;
private readonly HttpClient client;

protected HttpClient Client
public StarshipApiBase(HttpClient client)
{
set { client = value; }
this.client = client;
}

protected HttpClient Client => client;

public async Task<Base> GetBaseAsync(string baseId)
{
if (client == null)
Expand Down
7 changes: 4 additions & 3 deletions StarshipTraveler.Model/StarshipTraveler.Model.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 2 additions & 4 deletions StarshipTraveler.ServerSide/Data/StarshipApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ namespace StarshipTraveler.ServerSide.Data;
public class StarshipApi : StarshipApiBase
{
public StarshipApi(IHttpClientFactory factory, IHostEnvironment hostEnvironment)
: base(factory.CreateClient())
{
var apiUrl = hostEnvironment.IsDevelopment() ? "http://localhost:5000/api/" : "https://starshipapi.azurewebsites.net/api/";

var client = factory.CreateClient();
client.BaseAddress = new Uri(apiUrl);
Client = client;
Client.BaseAddress = new Uri(apiUrl);
}
}

0 comments on commit 2a02d8d

Please sign in to comment.