Skip to content

Commit

Permalink
Add payment types
Browse files Browse the repository at this point in the history
  • Loading branch information
strifel committed Oct 4, 2024
1 parent 27b2c1d commit b8ddd7c
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 40 deletions.
40 changes: 26 additions & 14 deletions Components/Pages/GroupCart.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
@if (Group != null)
{
<h1>Left to add to Cart: @Group?.GroupName</h1>


<table class="table">
<thead>
<tr>
<th>
Food
</th>
<th>
Price
</th>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<th>
Price
</th>
}
<th>
Mark as Added
</th>
Expand All @@ -37,22 +40,31 @@
<td>
@order.Food
</td>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<td>
@order.GetPrice()
@if (order.PaymentStatus == PaymentStatus.Paid)
{
<span style="color: green"> (Paid)</span>
}
</td>
}
<td>
@order.GetPrice()
@if (order.PaymentStatus == PaymentStatus.Paid) {
<span style="color: green"> (Paid)</span>
}
</td>
<td>
<button class="primary" @onclick="() => SetAdded(order)">Added</button>
<button class="primary" @onclick="() => SetAdded(order)">Added</button>
</td>
</tr>
}
</tbody>
</table><br><br>
<p>You should have added @Order.GetPrice(CalculateAddedSum())to your cart.<br>
The total will be @Order.GetPrice(CalculateTotalSum())€.</p>
</table>
<br>
<br>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<p>You should have added @Order.GetPrice(CalculateAddedSum())to your cart.<br>
The total will be @Order.GetPrice(CalculateTotalSum())€.</p>
}
}

@code {

Expand Down
26 changes: 19 additions & 7 deletions Components/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,27 @@
</div>
<div class="col-sm-12 col-lg-5 offset-lg-3 mb-3">
<div class="form-floating">
<InputText
placeholder=" "
id="paypal_username"
class="form-control"
@bind-Value="Model!.PaypalUsername"
/>
<label for="paypal_username">Your PayPal Username (optional):</label>
<InputSelect id="payment_type" class="form-select form-control" @bind-Value="@Model.PaymentType">
<option value="@PaymentType.PAY" default>Enabled</option>
<option value="@PaymentType.NO_NEED_TO_PAY">Host pays everything</option>
<option value="@PaymentType.NO_PRICES">Disabled</option>
</InputSelect>
<label for="payment_type">Payments:</label>
</div>
</div>
@if (Model!.PaymentType == PaymentType.PAY)
{
<div class="col-sm-12 col-lg-5 offset-lg-3 mb-3">
<div class="form-floating">
<InputText
placeholder=" "
id="paypal_username"
class="form-control"
@bind-Value="Model!.PaypalUsername"/>
<label for="paypal_username">Your PayPal Username (optional):</label>
</div>
</div>
}
<div class="col-sm-12 col-lg-5 offset-lg-3 mb-3">
<div>
<ValidationMessage For="() => Model!"/>
Expand Down
60 changes: 42 additions & 18 deletions Components/Pages/MyOrder.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
<th>
Food
</th>
<th>
Price
</th>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<th>
Price
</th>
}
<th></th>
</tr>
</thead>
Expand All @@ -40,15 +43,21 @@
<td>
@order.Food
</td>
<td>
@order.GetPrice()
</td>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<td>
@order.GetPrice()
</td>
}
<td></td>
</tr>
}
<tr>
<td><InputText min-length="1" max-length="100" placeholder="Pizza Margherita" class="form-control" @bind-Value="OrderFood"/></td>
<td><InputNumber min="0" step="0.01" placeholder="10.00" class="form-control" @bind-Value="OrderPrice"/></td>
@if (Group.PaymentType != PaymentType.NO_PRICES)
{
<td><InputNumber min="0" step="0.01" placeholder="10.00" class="form-control" @bind-Value="OrderPrice"/></td>
}
<td><button class="btn btn-primary" type="submit">Add</button></td>
</tr>
</tbody>
Expand All @@ -69,6 +78,10 @@
<a class="btn btn-primary" style="margin-right: 2px" target="_blank" href="https://www.paypal.com/paypalme/@Group.PaypalUsername/@Order.GetPrice(GetPriceToPay())">Send money via Paypal</a>
}
<button class="btn btn-secondary" @onclick="Paid">Mark as paid</button>
} else if (Group.PaymentType == PaymentType.NO_NEED_TO_PAY)
{
<h2>Host pays!</h2>
<p>You are good to go!</p>
}
}
@if (Person == null && NoPerson)
Expand Down Expand Up @@ -204,7 +217,8 @@

private int GetPriceToPay()
{
if (Person == null) return 0;
if (Person == null || Group == null) return 0;
if (Group.PaymentType != PaymentType.PAY) return 0;
return Person.Orders.Sum(o => o.PaymentStatus == PaymentStatus.Unpaid ? o.Price : 0) ?? 0;
}

Expand Down Expand Up @@ -237,25 +251,35 @@
{
messageStore?.Add(() => editContext!, "You must enter a Food Choice between 1 and 100 chars.");
}

if (OrderPrice == null | OrderPrice < 0)
{
messageStore?.Add(() => editContext!, "Price must be greater than 0.");
}

if (OrderPrice is > 2000)
if (Group is null || Group.PaymentType != PaymentType.NO_PRICES)
{
// We produce an overflow if its bigger than 21474836€
// We also produce overflows if the sum of the total order is bigger than 21474836€
messageStore?.Add(() => editContext!, "Price must be smaller or equal to 2000€.");
if (OrderPrice == null | OrderPrice < 0)
{
messageStore?.Add(() => editContext!, "Price must be greater than 0.");
}

if (OrderPrice is > 2000)
{
// We produce an overflow if its bigger than 21474836€
// We also produce overflows if the sum of the total order is bigger than 21474836€
messageStore?.Add(() => editContext!, "Price must be smaller or equal to 2000€.");
}
}
}

private void AddToOrder()
{
Order NewOrder = new();
NewOrder.Food = OrderFood!;
NewOrder.Price = (int)(OrderPrice! * 100);
if (Group!.PaymentType == PaymentType.NO_PRICES)
{
NewOrder.Price = 0;
}
else
{
NewOrder.Price = (int)(OrderPrice! * 100);
}
NewOrder.Group = Group!;
NewOrder.Person = Person!;
if (Group != Person!.Group) return; // this should never happen
Expand Down
12 changes: 11 additions & 1 deletion Data/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace GroupOrder.Data;

using System.ComponentModel;

[Index(nameof(GroupSlug), IsUnique = true)]
public class Group
{
Expand All @@ -28,7 +30,15 @@ public class Group

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


[Required]
[DefaultValue(PaymentType.PAY)]
public PaymentType PaymentType { get; set; }

}

public enum PaymentType {
PAY,
NO_NEED_TO_PAY,
NO_PRICES
}
Loading

0 comments on commit b8ddd7c

Please sign in to comment.