Skip to content

Commit

Permalink
Move to person based database layout
Browse files Browse the repository at this point in the history
Switch from saving the name of an order
to saving the person that added the order

Old orders are migrated and create one person
per order.
  • Loading branch information
strifel committed Jul 30, 2024
1 parent 4b0fe06 commit 0536b6a
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 14 deletions.
19 changes: 16 additions & 3 deletions Components/Pages/GroupAdd.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="form-group">
<label>
Name:
<InputText class="form-control" @bind-Value="Order!.Name"/>
<InputText class="form-control" @bind-Value="Name"/>
</label>
</div>
<div class="form-group">
Expand Down Expand Up @@ -51,6 +51,7 @@

private Order? Order { get; set; }
private Decimal? Price { get; set; }
private String? Name { get; set; }

private bool Saved { get; set; } = false;

Expand All @@ -69,6 +70,7 @@
{
Order = new();
Price = null;
Name = null;
editContext = new(Order);
editContext.OnValidationRequested += HandleValidationRequested;
messageStore = new(editContext);
Expand All @@ -91,9 +93,20 @@
return;
}

Person? person = await context.Persons.FirstOrDefaultAsync(p => p.Group == group && p.Name == Name);

if (person is null)
{
person = new Person();
person.Group = group;
person.Name = Name;
context.Add(person);
}

Order!.PaymentStatus = PaymentStatus.Unpaid;
Order!.AddedToCart = false;
Order!.Group = group;
Order!.Person = person;
context.Add(Order!);
context.SaveChanges();
Saved = true;
Expand All @@ -104,9 +117,9 @@
{
messageStore?.Clear();

if (Order!.Name == null || Order!.Name!.Length == 0 || Order!.Name!.Length > 100)
if (Name == null || Name!.Length == 0 || Name!.Length > 100)
{
messageStore?.Add(() => Order, "You must enter a Name between 1 and 100 chars.");
messageStore?.Add(() => Order!, "You must enter a Name between 1 and 100 chars.");
}

if (Order!.Food == null || Order!.Food!.Length == 0 || Order!.Food!.Length > 100)
Expand Down
3 changes: 2 additions & 1 deletion Components/Pages/GroupFinanze.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{
<tr>
<td>
@order.Name
@order.Person.Name
</td>
<td>
@order.GetPrice()
Expand Down Expand Up @@ -105,6 +105,7 @@
{
Group = await _context!.Groups
.Include(group => group.Orders)
.ThenInclude(order => order.Person)
.SingleOrDefaultAsync(
c => c.GroupSlug == GroupSlug && c.AdminCode == AdminCode);

Expand Down
3 changes: 2 additions & 1 deletion Components/Pages/GroupOverview.razor
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
<tr>
<td>
@order.Name
@order.Person.Name
</td>
<td>
@order.Food
Expand Down Expand Up @@ -139,6 +139,7 @@
{
Group = await context.Groups
.Include(group => group.Orders)
.ThenInclude(order => order.Person)
.SingleOrDefaultAsync(
c => c.GroupSlug == GroupSlug);

Expand Down
2 changes: 2 additions & 0 deletions Data/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Group
public string? PaypalUsername { get; set; }

public ICollection<Order> Orders { get; } = new List<Order>();
public ICollection<Person> Persons { get; } = new List<Person>();



}
1 change: 1 addition & 0 deletions Data/GroupContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class GroupContext : DbContext
{
public DbSet<Group> Groups { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Person> Persons { get; set; }
private string DbPath { get; }

public GroupContext()
Expand Down
7 changes: 3 additions & 4 deletions Data/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ public class Order

[Required]
public Group Group { get; set; } = null!;

[Required]
[StringLength(100, ErrorMessage = "Name can not exceed 100 characters.")]
public string? Name { get; set; }

public Person Person { get; set; } = null!;

[Required]
[StringLength(100, ErrorMessage = "Food cannot exceed 100 characters.")]
public string? Food { get; set; }
Expand Down
22 changes: 22 additions & 0 deletions Data/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace GroupOrder.Data;

public class Person
{
// The id is used to identify which person a given user can edit
// therefore we will sign a cookie with the id
public int Id { get; set; }

// One person can only ever exist in exactly one Group
[Required]
public Group Group { get; set; } = null!;

[Required]
[StringLength(100, ErrorMessage = "Name can not exceed 100 characters.")]
public string? Name { get; set; }

// One Person can only ever have Orders from the correct group
public ICollection<Order> Orders { get; } = new List<Order>();

}
167 changes: 167 additions & 0 deletions Migrations/20240729233758_person.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0536b6a

Please sign in to comment.