From b8ddd7c461972f9dc81d23a581cf1bd31f2c7d6f Mon Sep 17 00:00:00 2001 From: strifel Date: Fri, 4 Oct 2024 16:34:21 +0200 Subject: [PATCH] Add payment types --- Components/Pages/GroupCart.razor | 40 +++-- Components/Pages/Home.razor | 26 ++- Components/Pages/MyOrder.razor | 60 +++++-- Data/Group.cs | 12 +- .../20241004105800_PaymentType.Designer.cs | 165 ++++++++++++++++++ Migrations/20241004105800_PaymentType.cs | 29 +++ Migrations/GroupContextModelSnapshot.cs | 3 + 7 files changed, 295 insertions(+), 40 deletions(-) create mode 100644 Migrations/20241004105800_PaymentType.Designer.cs create mode 100644 Migrations/20241004105800_PaymentType.cs diff --git a/Components/Pages/GroupCart.razor b/Components/Pages/GroupCart.razor index 2db2a12..c90430c 100644 --- a/Components/Pages/GroupCart.razor +++ b/Components/Pages/GroupCart.razor @@ -13,7 +13,7 @@ @if (Group != null) {

Left to add to Cart: @Group?.GroupName

- + @@ -21,9 +21,12 @@ - + @if (Group.PaymentType != PaymentType.NO_PRICES) + { + + } @@ -37,22 +40,31 @@ + @if (Group.PaymentType != PaymentType.NO_PRICES) + { + + } - } -
Food - Price - + Price + Mark as Added @order.Food + @order.GetPrice()€ + @if (order.PaymentStatus == PaymentStatus.Paid) + { + (Paid) + } + - @order.GetPrice()€ - @if (order.PaymentStatus == PaymentStatus.Paid) { - (Paid) - } - - +


-

You should have added @Order.GetPrice(CalculateAddedSum())€ to your cart.
- The total will be @Order.GetPrice(CalculateTotalSum())€.

+ +
+
+ @if (Group.PaymentType != PaymentType.NO_PRICES) + { +

You should have added @Order.GetPrice(CalculateAddedSum())€ to your cart.
+ The total will be @Order.GetPrice(CalculateTotalSum())€.

} +} @code { diff --git a/Components/Pages/Home.razor b/Components/Pages/Home.razor index 061a5ac..396662f 100644 --- a/Components/Pages/Home.razor +++ b/Components/Pages/Home.razor @@ -47,15 +47,27 @@
- - + + + + + +
+ @if (Model!.PaymentType == PaymentType.PAY) + { +
+
+ + +
+
+ }
diff --git a/Components/Pages/MyOrder.razor b/Components/Pages/MyOrder.razor index 48cc9bc..b979aa0 100644 --- a/Components/Pages/MyOrder.razor +++ b/Components/Pages/MyOrder.razor @@ -27,9 +27,12 @@ Food - - Price - + @if (Group.PaymentType != PaymentType.NO_PRICES) + { + + Price + + } @@ -40,15 +43,21 @@ @order.Food - - @order.GetPrice()€ - + @if (Group.PaymentType != PaymentType.NO_PRICES) + { + + @order.GetPrice()€ + + } } - + @if (Group.PaymentType != PaymentType.NO_PRICES) + { + + } @@ -69,6 +78,10 @@ Send money via Paypal } + } else if (Group.PaymentType == PaymentType.NO_NEED_TO_PAY) + { +

Host pays!

+

You are good to go!

} } @if (Person == null && NoPerson) @@ -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; } @@ -237,17 +251,20 @@ { 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€."); + } } } @@ -255,7 +272,14 @@ { 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 diff --git a/Data/Group.cs b/Data/Group.cs index 7d81dcb..8f82511 100644 --- a/Data/Group.cs +++ b/Data/Group.cs @@ -3,6 +3,8 @@ namespace GroupOrder.Data; +using System.ComponentModel; + [Index(nameof(GroupSlug), IsUnique = true)] public class Group { @@ -28,7 +30,15 @@ public class Group public ICollection Orders { get; } = new List(); public ICollection Persons { get; } = new List(); - + [Required] + [DefaultValue(PaymentType.PAY)] + public PaymentType PaymentType { get; set; } +} + +public enum PaymentType { + PAY, + NO_NEED_TO_PAY, + NO_PRICES } \ No newline at end of file diff --git a/Migrations/20241004105800_PaymentType.Designer.cs b/Migrations/20241004105800_PaymentType.Designer.cs new file mode 100644 index 0000000..4b0abb8 --- /dev/null +++ b/Migrations/20241004105800_PaymentType.Designer.cs @@ -0,0 +1,165 @@ +// +using System; +using GroupOrder.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace GroupOrder.Migrations +{ + [DbContext(typeof(GroupContext))] + [Migration("20241004105800_PaymentType")] + partial class PaymentType + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); + + modelBuilder.Entity("GroupOrder.Data.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AdminCode") + .IsRequired() + .HasMaxLength(15) + .HasColumnType("TEXT"); + + b.Property("Description") + .HasMaxLength(500) + .HasColumnType("TEXT"); + + b.Property("GroupName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.Property("GroupSlug") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.Property("PaymentType") + .HasColumnType("INTEGER"); + + b.Property("PaypalUsername") + .HasMaxLength(30) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("GroupSlug") + .IsUnique(); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("GroupOrder.Data.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedToCart") + .IsRequired() + .HasColumnType("INTEGER"); + + b.Property("Food") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.Property("GroupId") + .HasColumnType("INTEGER"); + + b.Property("PaymentStatus") + .HasColumnType("INTEGER"); + + b.Property("PersonId") + .HasColumnType("INTEGER"); + + b.Property("Price") + .IsRequired() + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("GroupId"); + + b.HasIndex("PersonId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("GroupOrder.Data.Person", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("GroupId") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("GroupId"); + + b.ToTable("Persons"); + }); + + modelBuilder.Entity("GroupOrder.Data.Order", b => + { + b.HasOne("GroupOrder.Data.Group", "Group") + .WithMany("Orders") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("GroupOrder.Data.Person", "Person") + .WithMany("Orders") + .HasForeignKey("PersonId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("Person"); + }); + + modelBuilder.Entity("GroupOrder.Data.Person", b => + { + b.HasOne("GroupOrder.Data.Group", "Group") + .WithMany("Persons") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("GroupOrder.Data.Group", b => + { + b.Navigation("Orders"); + + b.Navigation("Persons"); + }); + + modelBuilder.Entity("GroupOrder.Data.Person", b => + { + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20241004105800_PaymentType.cs b/Migrations/20241004105800_PaymentType.cs new file mode 100644 index 0000000..7582f1d --- /dev/null +++ b/Migrations/20241004105800_PaymentType.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace GroupOrder.Migrations +{ + /// + public partial class PaymentType : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "PaymentType", + table: "Groups", + type: "INTEGER", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "PaymentType", + table: "Groups"); + } + } +} diff --git a/Migrations/GroupContextModelSnapshot.cs b/Migrations/GroupContextModelSnapshot.cs index 8105579..35537fe 100644 --- a/Migrations/GroupContextModelSnapshot.cs +++ b/Migrations/GroupContextModelSnapshot.cs @@ -42,6 +42,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(100) .HasColumnType("TEXT"); + b.Property("PaymentType") + .HasColumnType("INTEGER"); + b.Property("PaypalUsername") .HasMaxLength(30) .HasColumnType("TEXT");