Skip to content

Commit a546467

Browse files
committed
Finished savepoint 0 and 1
1 parent 08ce819 commit a546467

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2660
-979
lines changed
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
using System.ComponentModel.DataAnnotations;
1+
namespace BlazingPizza;
22

3-
namespace BlazingPizza
3+
public class Address
44
{
5-
public class Address
6-
{
7-
public int Id { get; set; }
5+
public int Id { get; set; }
86

9-
public string Name { get; set; }
7+
[Required, MaxLength(100)]
8+
public string Name { get; set; }
109

11-
public string Line1 { get; set; }
10+
[Required, MaxLength(100)]
11+
public string Line1 { get; set; }
1212

13-
public string Line2 { get; set; }
13+
[MaxLength(100)]
14+
public string Line2 { get; set; }
1415

15-
public string City { get; set; }
16+
[Required, MaxLength(50)]
17+
public string City { get; set; }
1618

17-
public string Region { get; set; }
19+
[Required, MaxLength(20)]
20+
public string Region { get; set; }
1821

19-
public string PostalCode { get; set; }
20-
}
22+
[Required, MaxLength(20)]
23+
public string PostalCode { get; set; }
2124
}

save-points/00-get-started/BlazingPizza.Shared/BlazingPizza.Shared.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
<PropertyGroup>
44
<TargetFramework>$(TargetFrameworkVersion)</TargetFramework>
55
<RootNamespace>BlazingPizza</RootNamespace>
6+
<ImplicitUsings>true</ImplicitUsings>
67
</PropertyGroup>
78

9+
<ItemGroup>
10+
<Using Include="System.ComponentModel.DataAnnotations" />
11+
</ItemGroup>
12+
813
<ItemGroup>
914
<ProjectReference Include="..\BlazingPizza.ComponentsLibrary\BlazingPizza.ComponentsLibrary.csproj" />
1015
</ItemGroup>
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
namespace BlazingPizza
1+
namespace BlazingPizza;
2+
3+
public class LatLong
24
{
3-
public class LatLong
4-
{
5-
public LatLong()
6-
{
7-
}
5+
public LatLong()
6+
{
7+
}
88

9-
public LatLong(double latitude, double longitude) : this()
10-
{
11-
Latitude = latitude;
12-
Longitude = longitude;
13-
}
9+
public LatLong(double latitude, double longitude) : this()
10+
{
11+
Latitude = latitude;
12+
Longitude = longitude;
13+
}
1414

15-
public double Latitude { get; set; }
15+
public double Latitude { get; set; }
1616

17-
public double Longitude { get; set; }
17+
public double Longitude { get; set; }
1818

19-
public static LatLong Interpolate(LatLong start, LatLong end, double proportion)
20-
{
21-
// The Earth is flat, right? So no need for spherical interpolation.
22-
return new LatLong(
23-
start.Latitude + (end.Latitude - start.Latitude) * proportion,
24-
start.Longitude + (end.Longitude - start.Longitude) * proportion);
25-
}
26-
}
27-
}
19+
public static LatLong Interpolate(LatLong start, LatLong end, double proportion)
20+
{
21+
// The Earth is flat, right? So no need for spherical interpolation.
22+
return new LatLong(
23+
start.Latitude + (end.Latitude - start.Latitude) * proportion,
24+
start.Longitude + (end.Longitude - start.Longitude) * proportion);
25+
}
26+
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
using System.ComponentModel.DataAnnotations;
1+
namespace BlazingPizza;
22

3-
namespace BlazingPizza
3+
public class NotificationSubscription
44
{
5-
public class NotificationSubscription
6-
{
7-
public int NotificationSubscriptionId { get; set; }
5+
public int NotificationSubscriptionId { get; set; }
86

9-
public string UserId { get; set; }
7+
public string UserId { get; set; }
108

11-
public string Url { get; set; }
9+
public string Url { get; set; }
1210

13-
public string P256dh { get; set; }
11+
public string P256dh { get; set; }
1412

15-
public string Auth { get; set; }
16-
}
13+
public string Auth { get; set; }
1714
}
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Text.Json.Serialization;
42

5-
namespace BlazingPizza
3+
namespace BlazingPizza;
4+
5+
public class Order
66
{
7-
public class Order
8-
{
9-
public int OrderId { get; set; }
7+
public int OrderId { get; set; }
108

11-
public string UserId { get; set; }
9+
public string UserId { get; set; }
1210

13-
public DateTime CreatedTime { get; set; }
11+
public DateTime CreatedTime { get; set; }
1412

15-
public Address DeliveryAddress { get; set; } = new Address();
13+
public Address DeliveryAddress { get; set; } = new Address();
1614

17-
public LatLong DeliveryLocation { get; set; }
15+
public LatLong DeliveryLocation { get; set; }
1816

19-
public List<Pizza> Pizzas { get; set; } = new List<Pizza>();
17+
public List<Pizza> Pizzas { get; set; } = new List<Pizza>();
2018

21-
public decimal GetTotalPrice() => Pizzas.Sum(p => p.GetTotalPrice());
19+
public decimal GetTotalPrice() => Pizzas.Sum(p => p.GetTotalPrice());
2220

23-
public string GetFormattedTotalPrice() => GetTotalPrice().ToString("0.00");
24-
}
21+
public string GetFormattedTotalPrice() => GetTotalPrice().ToString("0.00");
2522
}
23+
24+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
25+
[JsonSerializable(typeof(Order))]
26+
[JsonSerializable(typeof(OrderWithStatus))]
27+
[JsonSerializable(typeof(List<OrderWithStatus>))]
28+
[JsonSerializable(typeof(Pizza))]
29+
[JsonSerializable(typeof(List<PizzaSpecial>))]
30+
[JsonSerializable(typeof(List<Topping>))]
31+
[JsonSerializable(typeof(Topping))]
32+
public partial class OrderContext : JsonSerializerContext {}
Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,78 @@
1-
using BlazingPizza.ComponentsLibrary.Map;
2-
using System;
3-
using System.Collections.Generic;
1+
using System.Text.Json.Serialization;
2+
using BlazingPizza.ComponentsLibrary.Map;
43

5-
namespace BlazingPizza
4+
5+
namespace BlazingPizza;
6+
7+
public class OrderWithStatus
68
{
7-
public class OrderWithStatus
8-
{
9-
public readonly static TimeSpan PreparationDuration = TimeSpan.FromSeconds(10);
10-
public readonly static TimeSpan DeliveryDuration = TimeSpan.FromMinutes(1); // Unrealistic, but more interesting to watch
9+
public readonly static TimeSpan PreparationDuration = TimeSpan.FromSeconds(10);
10+
public readonly static TimeSpan DeliveryDuration = TimeSpan.FromMinutes(1); // Unrealistic, but more interesting to watch
1111

12-
public Order Order { get; set; }
12+
public Order Order { get; set; }
1313

14-
public string StatusText { get; set; }
14+
public string StatusText { get; set; }
1515

16-
public bool IsDelivered => StatusText == "Delivered";
16+
public bool IsDelivered => StatusText == "Delivered";
1717

18-
public List<Marker> MapMarkers { get; set; }
18+
public List<Marker> MapMarkers { get; set; }
1919

20-
public static OrderWithStatus FromOrder(Order order)
21-
{
22-
// To simulate a real backend process, we fake status updates based on the amount
23-
// of time since the order was placed
24-
string statusText;
25-
List<Marker> mapMarkers;
26-
var dispatchTime = order.CreatedTime.Add(PreparationDuration);
20+
public static OrderWithStatus FromOrder(Order order)
21+
{
22+
// To simulate a real backend process, we fake status updates based on the amount
23+
// of time since the order was placed
24+
string statusText;
25+
List<Marker> mapMarkers;
26+
var dispatchTime = order.CreatedTime.Add(PreparationDuration);
2727

28-
if (DateTime.Now < dispatchTime)
29-
{
30-
statusText = "Preparing";
31-
mapMarkers = new List<Marker>
32-
{
33-
ToMapMarker("You", order.DeliveryLocation, showPopup: true)
34-
};
35-
}
36-
else if (DateTime.Now < dispatchTime + DeliveryDuration)
37-
{
38-
statusText = "Out for delivery";
28+
if (DateTime.Now < dispatchTime)
29+
{
30+
statusText = "Preparing";
31+
mapMarkers = new List<Marker>
32+
{
33+
ToMapMarker("You", order.DeliveryLocation, showPopup: true)
34+
};
35+
}
36+
else if (DateTime.Now < dispatchTime + DeliveryDuration)
37+
{
38+
statusText = "Out for delivery";
3939

40-
var startPosition = ComputeStartPosition(order);
41-
var proportionOfDeliveryCompleted = Math.Min(1, (DateTime.Now - dispatchTime).TotalMilliseconds / DeliveryDuration.TotalMilliseconds);
42-
var driverPosition = LatLong.Interpolate(startPosition, order.DeliveryLocation, proportionOfDeliveryCompleted);
43-
mapMarkers = new List<Marker>
44-
{
45-
ToMapMarker("You", order.DeliveryLocation),
46-
ToMapMarker("Driver", driverPosition, showPopup: true),
47-
};
48-
}
49-
else
50-
{
51-
statusText = "Delivered";
52-
mapMarkers = new List<Marker>
53-
{
54-
ToMapMarker("Delivery location", order.DeliveryLocation, showPopup: true),
55-
};
56-
}
40+
var startPosition = ComputeStartPosition(order);
41+
var proportionOfDeliveryCompleted = Math.Min(1, (DateTime.Now - dispatchTime).TotalMilliseconds / DeliveryDuration.TotalMilliseconds);
42+
var driverPosition = LatLong.Interpolate(startPosition, order.DeliveryLocation, proportionOfDeliveryCompleted);
43+
mapMarkers = new List<Marker>
44+
{
45+
ToMapMarker("You", order.DeliveryLocation),
46+
ToMapMarker("Driver", driverPosition, showPopup: true),
47+
};
48+
}
49+
else
50+
{
51+
statusText = "Delivered";
52+
mapMarkers = new List<Marker>
53+
{
54+
ToMapMarker("Delivery location", order.DeliveryLocation, showPopup: true),
55+
};
56+
}
5757

58-
return new OrderWithStatus
59-
{
60-
Order = order,
61-
StatusText = statusText,
62-
MapMarkers = mapMarkers,
63-
};
64-
}
58+
return new OrderWithStatus
59+
{
60+
Order = order,
61+
StatusText = statusText,
62+
MapMarkers = mapMarkers,
63+
};
64+
}
6565

66-
private static LatLong ComputeStartPosition(Order order)
67-
{
68-
// Random but deterministic based on order ID
69-
var rng = new Random(order.OrderId);
70-
var distance = 0.01 + rng.NextDouble() * 0.02;
71-
var angle = rng.NextDouble() * Math.PI * 2;
72-
var offset = (distance * Math.Cos(angle), distance * Math.Sin(angle));
73-
return new LatLong(order.DeliveryLocation.Latitude + offset.Item1, order.DeliveryLocation.Longitude + offset.Item2);
74-
}
66+
private static LatLong ComputeStartPosition(Order order)
67+
{
68+
// Random but deterministic based on order ID
69+
var rng = new Random(order.OrderId);
70+
var distance = 0.01 + rng.NextDouble() * 0.02;
71+
var angle = rng.NextDouble() * Math.PI * 2;
72+
var offset = (distance * Math.Cos(angle), distance * Math.Sin(angle));
73+
return new LatLong(order.DeliveryLocation.Latitude + offset.Item1, order.DeliveryLocation.Longitude + offset.Item2);
74+
}
7575

76-
static Marker ToMapMarker(string description, LatLong coords, bool showPopup = false)
77-
=> new Marker { Description = description, X = coords.Longitude, Y = coords.Latitude, ShowPopup = showPopup };
78-
}
79-
}
76+
static Marker ToMapMarker(string description, LatLong coords, bool showPopup = false)
77+
=> new Marker { Description = description, X = coords.Longitude, Y = coords.Latitude, ShowPopup = showPopup };
78+
}
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Text.Json.Serialization;
32

4-
namespace BlazingPizza
3+
namespace BlazingPizza;
4+
5+
/// <summary>
6+
/// /// Represents a customized pizza as part of an order
7+
/// </summary>
8+
public class Pizza
59
{
6-
/// <summary>
7-
/// Represents a customized pizza as part of an order
8-
/// </summary>
9-
public class Pizza
10-
{
11-
public const int DefaultSize = 12;
12-
public const int MinimumSize = 9;
13-
public const int MaximumSize = 17;
10+
public const int DefaultSize = 12;
11+
public const int MinimumSize = 9;
12+
public const int MaximumSize = 17;
1413

15-
public int Id { get; set; }
14+
public int Id { get; set; }
1615

17-
public int OrderId { get; set; }
16+
public int OrderId { get; set; }
1817

19-
public PizzaSpecial Special { get; set; }
18+
public PizzaSpecial Special { get; set; }
2019

21-
public int SpecialId { get; set; }
20+
public int SpecialId { get; set; }
2221

23-
public int Size { get; set; }
22+
public int Size { get; set; }
2423

25-
public List<PizzaTopping> Toppings { get; set; }
24+
public List<PizzaTopping> Toppings { get; set; }
2625

27-
public decimal GetBasePrice()
28-
{
29-
return ((decimal)Size / (decimal)DefaultSize) * Special.BasePrice;
30-
}
26+
public decimal GetBasePrice()
27+
{
28+
return ((decimal)Size / (decimal)DefaultSize) * Special.BasePrice;
29+
}
3130

32-
public decimal GetTotalPrice()
33-
{
34-
return GetBasePrice() + Toppings.Sum(t => t.Topping.Price);
35-
}
31+
public decimal GetTotalPrice()
32+
{
33+
return GetBasePrice() + Toppings.Sum(t => t.Topping.Price);
34+
}
3635

37-
public string GetFormattedTotalPrice()
38-
{
39-
return GetTotalPrice().ToString("0.00");
40-
}
41-
}
36+
public string GetFormattedTotalPrice()
37+
{
38+
return GetTotalPrice().ToString("0.00");
39+
}
4240
}
41+
42+
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
43+
[JsonSerializable(typeof(Pizza))]
44+
public partial class PizzaContext : JsonSerializerContext {}

0 commit comments

Comments
 (0)