From 0b69e0b13a342f3a5c3f0898a4513937fe538e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 20 Apr 2023 22:00:58 +0200 Subject: [PATCH] Notes features are ready --- .../Client/Pages/EditContact.razor | 74 +++++++++++++++++-- BlazorSyncfusionCrm/Client/Pages/Notes.razor | 39 ++++++++++ BlazorSyncfusionCrm/Client/_Imports.razor | 1 + .../Server/Controllers/NotesController.cs | 41 +++++++++- 4 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 BlazorSyncfusionCrm/Client/Pages/Notes.razor diff --git a/BlazorSyncfusionCrm/Client/Pages/EditContact.razor b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor index 9a1cf12..59da650 100644 --- a/BlazorSyncfusionCrm/Client/Pages/EditContact.razor +++ b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor @@ -30,20 +30,53 @@ else
+

+ + Contact saved successfully. + + + Oops, something went wrong. + +

+ @if(Id is not null) + { +

Notes

+ + +
+

+ + @foreach(var note in notes) + { + + + + + + + + + +
+ } +

+ } + -

- - Contact saved successfully. - - - Oops, something went wrong. - -

+ + @code { [Parameter] public int? Id { get; set; } public Contact contact { get; set; } = new Contact(); + public List notes { get; set; } = new List(); + public Note newNote { get; set; } = new Note { Text = string.Empty }; bool showSuccessMessage = false; bool showErrorMessage = false; @@ -88,4 +121,29 @@ else showErrorMessage = true; } } + + async Task CreateNote() + { + if (Id is null) + return; + + newNote.ContactId = Id; + var result = await Http.PostAsJsonAsync($"api/notes", newNote); + if (result.IsSuccessStatusCode) + await LoadNotes(); + } + + async Task DeleteNote(int noteId) + { + var result = await Http.DeleteFromJsonAsync>($"api/notes/{noteId}"); + if (result is not null) + notes = result; + } + + async Task LoadNotes() + { + var result = await Http.GetFromJsonAsync>($"api/notes/{contact.Id}"); + if (result is not null) + notes = result; + } } diff --git a/BlazorSyncfusionCrm/Client/Pages/Notes.razor b/BlazorSyncfusionCrm/Client/Pages/Notes.razor new file mode 100644 index 0000000..f6aac0d --- /dev/null +++ b/BlazorSyncfusionCrm/Client/Pages/Notes.razor @@ -0,0 +1,39 @@ +@page "/notes" +@inject HttpClient Http +@inject NavigationManager NavigationManager + +

Notes

+ +@foreach (var note in NoteList) +{ + + + + + + @if(note.Contact is not null) + { + + } + + + +} + +@code { + public List NoteList { get; set; } = new List(); + + protected override async Task OnInitializedAsync() + { + var result = await Http.GetFromJsonAsync>("api/notes"); + if (result is not null) + NoteList = result; + } + + void NavigateToContact(int contactId) + { + NavigationManager.NavigateTo($"contacts/edit/{contactId}"); + } +} diff --git a/BlazorSyncfusionCrm/Client/_Imports.razor b/BlazorSyncfusionCrm/Client/_Imports.razor index 8be29fe..50ab631 100644 --- a/BlazorSyncfusionCrm/Client/_Imports.razor +++ b/BlazorSyncfusionCrm/Client/_Imports.razor @@ -16,4 +16,5 @@ @using Syncfusion.Blazor.Calendars @using Syncfusion.Blazor.Notifications @using Syncfusion.Blazor.Popups +@using Syncfusion.Blazor.Cards diff --git a/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs b/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs index 919c2ab..91c7a98 100644 --- a/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs +++ b/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs @@ -1,4 +1,5 @@ -using BlazorSyncfusionCrm.Server.Data; +using BlazorSyncfusionCrm.Client.Pages; +using BlazorSyncfusionCrm.Server.Data; using BlazorSyncfusionCrm.Shared; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -28,6 +29,44 @@ namespace BlazorSyncfusionCrm.Server.Controllers } + [HttpGet("{contactId}")] + public async Task>> GetNotesFromContact(int contactId) + { + return await _context.Notes + .Where(n => n.ContactId == contactId) + .OrderByDescending(n => n.DateCreated) + .ToListAsync(); + } + + [HttpPost] + public async Task>> CreateNote(Note note) + { + _context.Notes.Add(note); + await _context.SaveChangesAsync(); + return await _context.Notes + .Where(n => n.ContactId == note.ContactId) + .OrderByDescending(n => n.DateCreated) + .ToListAsync(); + + } + + [HttpDelete("{id}")] + public async Task>> DeleteNote(int id) + { + var dbNote = await _context.Notes.FindAsync(id); + if ( dbNote is null) + { + return NotFound("Note not found."); + } + _context.Notes.Remove(dbNote); + await _context.SaveChangesAsync(); + + return await _context.Notes + .Where(n => n.ContactId == dbNote.ContactId) + .OrderByDescending(n => n.DateCreated) + .ToListAsync(); + + } } }