Skip to content

Commit 6183a49

Browse files
committed
Holy fuck postgresql
1 parent 0e22565 commit 6183a49

73 files changed

Lines changed: 1180 additions & 11043 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ obj/
33
/packages/
44
riderModule.iml
55
/_ReSharper.Caches/
6-
.idea/
6+
.idea/
7+
postgres_data/
8+
dotnet_data/

EatSomewhere.sln.DotSettings.user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEnumerable_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F8e0f76263c434f48923effc052b11def78a00_003F14_003Fd5bdda30_003FEnumerable_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
3+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpListener_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F9a52bbb6016b431f99d9b6e2d1d363a745a00_003F9f_003F7a293fa6_003FHttpListener_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
34
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThreadPoolWorkQueue_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd15de8ed93064806a163b97c7155b882d19c00_003F9e_003F26077aa7_003FThreadPoolWorkQueue_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

EatSomewhere/Config.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ public class Config
77
{
88

99
public static Config? Instance;
10-
public string? dbConnectionString { get; set; }
10+
11+
public string? dbConnectionString { get; set; } =
12+
"Server=127.0.0.1;Port=5432;Database=eatsomewhere;Username=eatsomewhere;Password=eatsomewhere;";
1113
public int port { get; set; } = 8383;
1214
private static readonly string _configPath = "config.json";
1315
public static void LoadConfig()

EatSomewhere/Data/Bill.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public class Bill
1919
[JsonIgnore]
2020
public FoodEntry FoodEntry { get; set; }
2121
public int Amount { get; set; }
22+
public int Persons { get; set; } = 1;
2223
}

EatSomewhere/Data/BillManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public class BillManager
88
public static List<Bill> GetTotalBills(User user, string assemblyId, bool first = true)
99
{
1010
using var d = new AppDbContext();
11-
d.Attach(user);
1211
List<Bill> total = d.Bills
13-
.Where(x => x.User == user && x.FoodEntry.Assembly.Id == assemblyId)
12+
.Where(x => x.User.Id == user.Id && x.FoodEntry.Assembly.Id == assemblyId)
1413
.GroupBy(x => x.Recipient)
1514
.Select(x => new Bill
1615
{
@@ -24,7 +23,7 @@ public static List<Bill> GetTotalBills(User user, string assemblyId, bool first
2423
foreach (var bill in total)
2524
{
2625
Bill? theirBill = d.Bills
27-
.Where(x => x.User == bill.Recipient && x.Recipient == user && x.FoodEntry.Assembly.Id == assemblyId)
26+
.Where(x => x.User == bill.Recipient && x.Recipient.Id == user.Id && x.FoodEntry.Assembly.Id == assemblyId)
2827
.GroupBy(x => x.Recipient)
2928
.Select(x => new Bill
3029
{

EatSomewhere/Data/Food.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations.Schema;
2+
using System.Text.Json.Serialization;
23
using EatSomewhere.Users;
34

45
namespace EatSomewhere.Data;
@@ -24,4 +25,6 @@ public int EstimatedCost
2425
}
2526
public string Recipe { get; set; }
2627
public bool Archived { get; set; } = false;
28+
[JsonIgnore]
29+
public IEnumerable<FoodEntry>? FoodEntries { get; set; }
2730
}

EatSomewhere/Data/FoodEntry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FoodEntry
1818

1919
public int PersonCount => Participants.Sum(x => x.Persons);
2020
public List<FoodParticipant> Participants { get; set; }
21-
public User PayedBy { get; set; }
21+
public User? PayedBy { get; set; }
2222
public Assembly Assembly { get; set; }
2323
public User CreatedBy { get; set; }
2424
public List<Bill> Bills { get; set; }
@@ -29,6 +29,7 @@ public List<Bill> CalculateBills()
2929
{
3030
User = x.User ?? null,
3131
Amount = CostPerPerson * x.Persons,
32+
Persons = x.Persons,
3233
Recipient = PayedBy,
3334
}).ToList();
3435
}

EatSomewhere/Database/AppDbContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AppDbContext : DbContext
1818
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1919
{
2020
Config.LoadConfig();
21-
optionsBuilder.UseSqlite(Config.Instance.dbConnectionString == null ? "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "database.db" : Config.Instance.dbConnectionString);
21+
optionsBuilder.UseNpgsql(Config.Instance?.dbConnectionString == null ? new Config().dbConnectionString : Config.Instance.dbConnectionString);
2222
}
2323

2424
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -49,10 +49,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4949

5050
modelBuilder.Entity<FoodEntry>().HasKey(x => x.Id);
5151
modelBuilder.Entity<FoodEntry>().Navigation(x => x.Food).AutoInclude();
52+
modelBuilder.Entity<FoodEntry>().HasOne(x => x.Food).WithMany(x => x.FoodEntries);
5253
modelBuilder.Entity<FoodEntry>().Navigation(x => x.Participants).AutoInclude();
5354
modelBuilder.Entity<FoodEntry>().HasMany(x => x.Participants).WithOne(x => x.FoodEntry);
54-
modelBuilder.Entity<FoodEntry>().Navigation(x => x.PayedBy).AutoInclude();
55+
modelBuilder.Entity<FoodEntry>().HasOne(x => x.CreatedBy).WithMany();
5556
modelBuilder.Entity<FoodEntry>().Navigation(x => x.CreatedBy).AutoInclude();
57+
modelBuilder.Entity<FoodEntry>().HasOne(x => x.PayedBy).WithMany(x => x.PayedFoodEntries);
58+
modelBuilder.Entity<FoodEntry>().Navigation(x => x.PayedBy).AutoInclude();
5659
modelBuilder.Entity<FoodEntry>().HasOne(x => x.Assembly).WithMany(x => x.FoodEntries);
5760

5861
modelBuilder.Entity<FoodParticipant>().HasKey(x => x.Id);

EatSomewhere/EatSomewhere.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
</PackageReference>
1919
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.1" />
20+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
2021
</ItemGroup>
2122

2223
</Project>

EatSomewhere/Manager/FoodManager.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static ApiResponse<FoodEntry> CreateFoodEntry(User user, FoodEntry food)
188188
};
189189
}
190190
food.Food = foundFood;
191-
User? foundPayedBy = a.Users.FirstOrDefault(x => x.Id == food.PayedBy.Id);
191+
User? foundPayedBy = a.Users.FirstOrDefault(x => x.Id == food.PayedBy?.Id);
192192
if (foundPayedBy == null)
193193
{
194194
return new ApiResponse<FoodEntry>
@@ -308,6 +308,7 @@ public static ApiResponse<Ingredient> CreateIngredient(User user, Ingredient ing
308308
public static ApiResponse DeleteAssembly(User user, string Id)
309309
{
310310
using var d = new AppDbContext();
311+
d.Attach(user);
311312
var assembly = d.Assemblies.FirstOrDefault(a => a.Id == Id);
312313
if (assembly == null)
313314
{
@@ -345,7 +346,8 @@ static bool CanAdministrateAssembly(User user, Assembly assembly)
345346
public static ApiResponse DeleteIngredient(User user, string id)
346347
{
347348
using var d = new AppDbContext();
348-
var ingredient = d.Ingredients.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
349+
d.Attach(user);
350+
var ingredient = d.Ingredients.Where(a => a.Id == id).Include(x => x.Assembly).ThenInclude(x => x.Admins).FirstOrDefault();
349351
if (ingredient == null)
350352
{
351353
return new ApiResponse
@@ -376,7 +378,8 @@ public static ApiResponse DeleteIngredient(User user, string id)
376378
public static ApiResponse DeleteFoodEntry(User user, string id)
377379
{
378380
using var d = new AppDbContext();
379-
var foodEntry = d.FoodEntries.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
381+
d.Attach(user);
382+
var foodEntry = d.FoodEntries.Where(a => a.Id == id).Include(x => x.Assembly).ThenInclude(x => x.Admins).FirstOrDefault();
380383
if (foodEntry == null)
381384
{
382385
return new ApiResponse
@@ -564,7 +567,8 @@ public static ApiResponse<String> RemoveUserFromAssembly(User user, string assem
564567
public static ApiResponse DeleteFood(User user, string id)
565568
{
566569
using var d = new AppDbContext();
567-
var food = d.Foods.Where(a => a.Id == id).Include(x => x.Assembly).FirstOrDefault();
570+
d.Attach(user);
571+
var food = d.Foods.Where(a => a.Id == id).Include(x => x.Assembly).ThenInclude(x => x.Admins).FirstOrDefault();
568572
if (food == null)
569573
{
570574
return new ApiResponse
@@ -574,7 +578,7 @@ public static ApiResponse DeleteFood(User user, string id)
574578
};
575579
}
576580

577-
if (!CanAdministrateAssembly(user, food.Assembly))
581+
if (!CanAdministrateAssembly(user, food.Assembly) && food.CreatedBy.Id != user.Id)
578582
{
579583
return new ApiResponse
580584
{
@@ -589,7 +593,7 @@ public static ApiResponse DeleteFood(User user, string id)
589593

590594
return new ApiResponse
591595
{
592-
Success = true
596+
Success = true,
593597
};
594598
}
595599

0 commit comments

Comments
 (0)