Refactoring

This commit is contained in:
Andre Beging
2025-03-28 17:18:49 +01:00
parent 859b0c3712
commit e19268f3eb
4 changed files with 42 additions and 42 deletions

View File

@@ -48,13 +48,13 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if (saveR > 0) if (saveR > 0)
return new OperationResult<Audit>(audit); return new(audit);
return new OperationResult<Audit>(new Exception("Couldn't add audit")); return new(new Exception("Couldn't add audit"));
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<Audit>(e); return new(e);
} }
} }
@@ -84,13 +84,13 @@ namespace FoodsharingSiegen.Server.Data.Service
var mat = query?.ToList(); var mat = query?.ToList();
if (mat != null) return new OperationResult<List<Audit>>(mat); if (mat != null) return new(mat);
return new OperationResult<List<Audit>>(new Exception("Couldn't load audits")); return new(new Exception("Couldn't load audits"));
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<List<Audit>>(e); return new(e);
} }
} }

View File

@@ -38,7 +38,7 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var targetProspect = await Context.Prospects!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == interaction.ProspectID); var targetProspect = await Context.Prospects!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == interaction.ProspectID);
if (targetProspect == null) return new OperationResult<Interaction>(new Exception("Invalid prospect id")); if (targetProspect == null) return new(new Exception("Invalid prospect id"));
interaction.ProspectID = Guid.Empty; interaction.ProspectID = Guid.Empty;
interaction.Created = DateTime.UtcNow; interaction.Created = DateTime.UtcNow;
@@ -53,11 +53,11 @@ namespace FoodsharingSiegen.Server.Data.Service
Context.Entry(targetProspect).State = EntityState.Detached; Context.Entry(targetProspect).State = EntityState.Detached;
Context.Entry(interaction).State = EntityState.Detached; Context.Entry(interaction).State = EntityState.Detached;
return new OperationResult<Interaction>(interaction); return new(interaction);
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<Interaction>(e); return new(e);
} }
} }
@@ -74,7 +74,7 @@ namespace FoodsharingSiegen.Server.Data.Service
{ {
try try
{ {
if (prospect == null) return new OperationResult<Prospect>(new Exception("Cannot be empty")); if (prospect == null) return new(new Exception("Cannot be empty"));
prospect.Created = DateTime.UtcNow; prospect.Created = DateTime.UtcNow;
prospect.Id = Guid.Empty; prospect.Id = Guid.Empty;
@@ -85,14 +85,14 @@ namespace FoodsharingSiegen.Server.Data.Service
if (saveR > 0) if (saveR > 0)
{ {
await AuditService.Insert(AuditType.CreateProspect, prospect.Name, prospect.FsId.ToString()); await AuditService.Insert(AuditType.CreateProspect, prospect.Name, prospect.FsId.ToString());
return new OperationResult<Prospect>(prospect); return new(prospect);
} }
return new OperationResult<Prospect>(new Exception("Couldn't add prospect")); return new(new Exception("Couldn't add prospect"));
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<Prospect>(e); return new(e);
} }
} }
@@ -118,11 +118,11 @@ namespace FoodsharingSiegen.Server.Data.Service
var prospects = await prospectsQuery.ToListAsync(); var prospects = await prospectsQuery.ToListAsync();
return new OperationResult<List<Prospect>>(prospects); return new(prospects);
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<List<Prospect>>(e); return new(e);
} }
} }
@@ -139,16 +139,16 @@ namespace FoodsharingSiegen.Server.Data.Service
{ {
try try
{ {
Context.Interactions!.Remove(new Interaction { Id = interactionId }); Context.Interactions!.Remove(new() { Id = interactionId });
await Context.SaveChangesAsync(); await Context.SaveChangesAsync();
await AuditService.Insert(AuditType.RemoveInteraction, "?"); await AuditService.Insert(AuditType.RemoveInteraction, "?");
return new OperationResult(); return new();
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult(e); return new(e);
} }
} }
@@ -166,7 +166,7 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var entityProspect = await Context.Prospects!.FirstOrDefaultAsync(x => x.Id == prospect.Id); var entityProspect = await Context.Prospects!.FirstOrDefaultAsync(x => x.Id == prospect.Id);
if (entityProspect == null) return new OperationResult(new Exception("Prospect not found")); if (entityProspect == null) return new(new Exception("Prospect not found"));
entityProspect.Memo = prospect.Memo; entityProspect.Memo = prospect.Memo;
entityProspect.Name = prospect.Name; entityProspect.Name = prospect.Name;
@@ -175,14 +175,14 @@ namespace FoodsharingSiegen.Server.Data.Service
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if(saveR < 1) return new OperationResult(new Exception("Fehler beim speichern")); if(saveR < 1) return new(new Exception("Fehler beim speichern"));
await AuditService.Insert(AuditType.EditProspect, prospect.Name); await AuditService.Insert(AuditType.EditProspect, prospect.Name);
return new OperationResult(); return new();
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult(e); return new(e);
} }
} }

View File

@@ -44,7 +44,7 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
if (await Context.Users!.AnyAsync(x => x.Mail.ToLower().Equals(user.Mail.ToLower()))) if (await Context.Users!.AnyAsync(x => x.Mail.ToLower().Equals(user.Mail.ToLower())))
return new OperationResult<User>(new Exception("Diese E-Mail Adresse wird bereits verwendet")); return new(new Exception("Diese E-Mail Adresse wird bereits verwendet"));
user.Created = DateTime.UtcNow; user.Created = DateTime.UtcNow;
@@ -54,15 +54,15 @@ namespace FoodsharingSiegen.Server.Data.Service
await Context.Users!.AddAsync(user); await Context.Users!.AddAsync(user);
var saveResult = await Context.SaveChangesAsync(); var saveResult = await Context.SaveChangesAsync();
if (saveResult == 0) return new OperationResult<User>(new Exception("Fehler bei der Registrierung")); if (saveResult == 0) return new(new Exception("Fehler bei der Registrierung"));
await AuditService.Insert(AuditType.CreateUser, user.Mail); await AuditService.Insert(AuditType.CreateUser, user.Mail);
return new OperationResult<User>(user); return new(user);
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<User>(e); return new(e);
} }
} }
@@ -80,11 +80,11 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var users = await Context.Users!.AsNoTracking().ToListAsync(); var users = await Context.Users!.AsNoTracking().ToListAsync();
return new OperationResult<List<User>>(users); return new(users);
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult<List<User>>(e); return new(e);
} }
} }
@@ -102,7 +102,7 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var user = await Context.Users!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == userId); var user = await Context.Users!.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == userId);
if (user == null) return new OperationResult(new Exception("User not found")); if (user == null) return new(new Exception("User not found"));
// Interaktionen vom aktuellen Nutzer übernehmen // Interaktionen vom aktuellen Nutzer übernehmen
if(CurrentUser?.Id != null) if(CurrentUser?.Id != null)
@@ -118,13 +118,13 @@ namespace FoodsharingSiegen.Server.Data.Service
Context.Users?.Remove(user); Context.Users?.Remove(user);
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if (saveR < 1) return new OperationResult(new Exception("Fehler beim Löschen")); if (saveR < 1) return new(new Exception("Fehler beim Löschen"));
await AuditService.Insert(AuditType.RemoveUser, user.Mail); await AuditService.Insert(AuditType.RemoveUser, user.Mail);
return new OperationResult(); return new();
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult(e); return new(e);
} }
} }
@@ -142,22 +142,22 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id); var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id);
if (entityUser == null) return new OperationResult(new Exception("User not found")); if (entityUser == null) return new(new Exception("User not found"));
entityUser.Password = user.Password; entityUser.Password = user.Password;
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if (saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern")); if (saveR < 1) return new(new Exception("Fehler beim Speichern"));
var auditData = CurrentUser?.Id == user.Id ? "sich selbst" : user.Mail; var auditData = CurrentUser?.Id == user.Id ? "sich selbst" : user.Mail;
await AuditService.Insert(AuditType.SetUserPassword, auditData); await AuditService.Insert(AuditType.SetUserPassword, auditData);
return new OperationResult(); return new();
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult(e); return new(e);
} }
} }
@@ -175,7 +175,7 @@ namespace FoodsharingSiegen.Server.Data.Service
try try
{ {
var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id); var entityUser = await Context.Users!.FirstOrDefaultAsync(x => x.Id == user.Id);
if (entityUser == null) return new OperationResult(new Exception("User not found")); if (entityUser == null) return new(new Exception("User not found"));
if (entityUser.Mail != user.Mail || if (entityUser.Mail != user.Mail ||
entityUser.Verified != user.Verified || entityUser.Verified != user.Verified ||
@@ -192,17 +192,17 @@ namespace FoodsharingSiegen.Server.Data.Service
entityUser.Network = user.Network; entityUser.Network = user.Network;
if (!Context.HasChanges()) if (!Context.HasChanges())
return new OperationResult(new Exception("Nichts zum Speichern gefunden")); return new(new Exception("Nichts zum Speichern gefunden"));
var saveR = await Context.SaveChangesAsync(); var saveR = await Context.SaveChangesAsync();
if (saveR < 1) return new OperationResult(new Exception("Fehler beim Speichern")); if (saveR < 1) return new(new Exception("Fehler beim Speichern"));
await AuditService.Insert(AuditType.UpdateUser, user.Mail); await AuditService.Insert(AuditType.UpdateUser, user.Mail);
return new OperationResult(); return new();
} }
catch (Exception e) catch (Exception e)
{ {
return new OperationResult(e); return new(e);
} }
} }

View File

@@ -30,7 +30,7 @@
@* <Field> *@ @* <Field> *@
@* <FieldLabel>Wer?</FieldLabel> *@ @* <FieldLabel>Wer?</FieldLabel> *@
@* <Select TValue="Guid" @bind-SelectedValue="SelectedUser"> *@ @* <Select TValue="Guid" @bind-SelectedValue="SelectedUser"> *@
@* @foreach (var user in Users ?? new List<User>()) *@ @* @foreach (var user in Users ?? []) *@
@* { *@ @* { *@
@* <SelectItem Value="@user.Id">@user.Name</SelectItem> *@ @* <SelectItem Value="@user.Id">@user.Name</SelectItem> *@
@* } *@ @* } *@