From efa27a2122afdfcd7b7be52cb5e0bdcad01dc2f9 Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Sat, 2 Apr 2022 15:28:23 +0200 Subject: [PATCH] ProspectService updates --- .../Controls/InteractionRow.razor | 74 +++++++++++++++++++ .../Controls/InteractionRow.razor.css | 3 + .../Data/Service/ProspectService.cs | 24 +++++- FoodsharingSiegen.Server/Pages/Users.razor | 5 +- FoodsharingSiegen.Server/Shared/NavMenu.razor | 10 +-- FoodsharingSiegen.Server/wwwroot/css/site.css | 2 +- 6 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 FoodsharingSiegen.Server/Controls/InteractionRow.razor create mode 100644 FoodsharingSiegen.Server/Controls/InteractionRow.razor.css diff --git a/FoodsharingSiegen.Server/Controls/InteractionRow.razor b/FoodsharingSiegen.Server/Controls/InteractionRow.razor new file mode 100644 index 0000000..bb90239 --- /dev/null +++ b/FoodsharingSiegen.Server/Controls/InteractionRow.razor @@ -0,0 +1,74 @@ +@using FoodsharingSiegen.Contracts.Entity + +@code { + + [Parameter] + public Prospect Prospect { get; set; } + + [Parameter] + public InteractionType Type { get; set; } + + [Parameter] + public EventCallback AddClick { get; set; } + + [Parameter] + public EventCallback RemoveClick { get; set; } + + [Parameter] + public string Caption { get; set; } + + [Parameter] + public string ButtonText { get; set; } + + [Parameter] + public string IconClass { get; set; } + + [Parameter] + public bool Multiple { get; set; } + + [Parameter] + public int Minimum { get; set; } = 1; + + private List Interactions => Prospect?.Interactions?.Where(x => x.Type == Type).ToList() ?? new List(); + + private bool Done => Interactions.Count >= Minimum; +} + +@{ + var rowClass = ""; + if (Done) rowClass += "done"; +} + + + + + + @Caption: + + @if (Interactions.Count > 0) + { + foreach (var interaction in Interactions) + { +
+ @interaction.User.Name (@interaction.Date.ToShortDateString()) + @if (!string.IsNullOrWhiteSpace(interaction.Info)) + { + (@interaction.Info) + } + + @if (!Prospect.Complete || interaction.Type == InteractionType.Complete) + { +   + } + +
+ } + } + + @if (!Prospect.Complete && (Interactions.Count == 0 || Multiple)) + { + if (Multiple) ButtonText = "+"; + + } + + \ No newline at end of file diff --git a/FoodsharingSiegen.Server/Controls/InteractionRow.razor.css b/FoodsharingSiegen.Server/Controls/InteractionRow.razor.css new file mode 100644 index 0000000..8e4b193 --- /dev/null +++ b/FoodsharingSiegen.Server/Controls/InteractionRow.razor.css @@ -0,0 +1,3 @@ +tr.done th { + color: #64ae24; +} \ No newline at end of file diff --git a/FoodsharingSiegen.Server/Data/Service/ProspectService.cs b/FoodsharingSiegen.Server/Data/Service/ProspectService.cs index 140ceb3..e810c09 100644 --- a/FoodsharingSiegen.Server/Data/Service/ProspectService.cs +++ b/FoodsharingSiegen.Server/Data/Service/ProspectService.cs @@ -33,16 +33,17 @@ namespace FoodsharingSiegen.Server.Data.Service { var targetProspect = await Context.Prospects.Include(x => x.Interactions).FirstOrDefaultAsync(x => x.Id == interaction.ProspectId); if (targetProspect == null) return new OperationResult(new Exception("Invalid prospect id")); - - var dummyUser = Context.Users.First().Id; interaction.ProspectId = Guid.Empty; - interaction.UserId = dummyUser; targetProspect.Interactions.Add(interaction); await Context.SaveChangesAsync(); + // Detatch entities + Context.Entry(targetProspect).State = EntityState.Detached; + Context.Entry(interaction).State = EntityState.Detached; + return new OperationResult(interaction); } catch (Exception e) @@ -53,6 +54,21 @@ namespace FoodsharingSiegen.Server.Data.Service #endregion + public async Task RemoveInteraction(Guid interactionId) + { + try + { + Context.Interactions.Remove(new Interaction { Id = interactionId }); + await Context.SaveChangesAsync(); + + return new OperationResult(); + } + catch (Exception e) + { + return new OperationResult(e); + } + } + #region Public Method AddProspectAsync /// @@ -95,7 +111,7 @@ namespace FoodsharingSiegen.Server.Data.Service { try { - var prospects = await Context.Prospects.Include(x => x.Interactions).ThenInclude(x => x.User).OrderBy(x => x.Name).ToListAsync(); + var prospects = await Context.Prospects.AsNoTracking().Include(x => x.Interactions).ThenInclude(x => x.User).OrderBy(x => x.Name).ToListAsync(); return new OperationResult>(prospects); } catch (Exception e) diff --git a/FoodsharingSiegen.Server/Pages/Users.razor b/FoodsharingSiegen.Server/Pages/Users.razor index de62c9d..eb70c2e 100644 --- a/FoodsharingSiegen.Server/Pages/Users.razor +++ b/FoodsharingSiegen.Server/Pages/Users.razor @@ -1,4 +1,5 @@ -@page "/" +@page "/user" +@page "/users" @using FoodsharingSiegen.Contracts.Entity @code { @@ -18,7 +19,7 @@ } -Benutzer +Benutzerverwaltung

Benutzerverwaltung Admin

diff --git a/FoodsharingSiegen.Server/Shared/NavMenu.razor b/FoodsharingSiegen.Server/Shared/NavMenu.razor index f60ddee..862a466 100644 --- a/FoodsharingSiegen.Server/Shared/NavMenu.razor +++ b/FoodsharingSiegen.Server/Shared/NavMenu.razor @@ -9,13 +9,13 @@