From ede368b96336b196cecb024aef233554f533cbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 19 Apr 2023 17:25:42 +0200 Subject: [PATCH] Crud implemented by Contact --- .../Client/Pages/Contacts.razor | 135 ++++++++++++++---- .../Client/Pages/EditContact.razor | 91 ++++++++++++ .../Client/Pages/EditContact.razor.css | 3 + .../Client/Shared/NavBar.razor | 3 +- BlazorSyncfusionCrm/Client/_Imports.razor | 5 + .../Client/wwwroot/css/app.css | 5 + BlazorSyncfusionCrm/Client/wwwroot/index.html | 1 + .../Server/Controllers/ContactsController.cs | 87 +++++++++++ .../Server/Controllers/NotesController.cs | 33 +++++ BlazorSyncfusionCrm/Shared/Contact.cs | 2 + 10 files changed, 334 insertions(+), 31 deletions(-) create mode 100644 BlazorSyncfusionCrm/Client/Pages/EditContact.razor create mode 100644 BlazorSyncfusionCrm/Client/Pages/EditContact.razor.css create mode 100644 BlazorSyncfusionCrm/Server/Controllers/ContactsController.cs create mode 100644 BlazorSyncfusionCrm/Server/Controllers/NotesController.cs diff --git a/BlazorSyncfusionCrm/Client/Pages/Contacts.razor b/BlazorSyncfusionCrm/Client/Pages/Contacts.razor index 82aa64c..9650054 100644 --- a/BlazorSyncfusionCrm/Client/Pages/Contacts.razor +++ b/BlazorSyncfusionCrm/Client/Pages/Contacts.razor @@ -1,5 +1,6 @@ @page "/contacts" @inject NavigationManager NavigationManager +@inject HttpClient Http

Contacts

@@ -14,6 +15,15 @@ } + + + @@ -23,43 +33,108 @@ +

+ +

+ + + +
Are you sure?
+ +

Do you really want to delete @contactToDelete!.NickName

+
+
+ + + + + +
+ @code { + Contact? contactToDelete; public List GridData { get; set; } = new List { - new Contact - { - Id = 1, - FirstName = "Peter", - LastName = "Parker", - NickName = "Spider-Man", - Place = "New York City", - DateOfBirth = new DateTime(2001, 8, 1), - DateCreated = DateTime.Now - }, - new Contact - { - Id = 1, - FirstName = "Tony", - LastName = "Stark", - NickName = "Iron Man", - Place = "Malibu", - DateOfBirth = new DateTime(1970, 5, 29), - DateCreated = DateTime.Now - }, - new Contact - { - Id = 1, - FirstName = "Bruce", - LastName = "Wayne", - NickName = "Batman", - Place = "Gotham City", - DateOfBirth = new DateTime(1915, 4, 7), - DateCreated = DateTime.Now - } + //new Contact + //{ + // Id = 1, + // FirstName = "Peter", + // LastName = "Parker", + // NickName = "Spider-Man", + // Place = "New York City", + // DateOfBirth = new DateTime(2001, 8, 1), + // DateCreated = DateTime.Now + //}, + //new Contact + //{ + // Id = 1, + // FirstName = "Tony", + // LastName = "Stark", + // NickName = "Iron Man", + // Place = "Malibu", + // DateOfBirth = new DateTime(1970, 5, 29), + // DateCreated = DateTime.Now + //}, + //new Contact + //{ + // Id = 1, + // FirstName = "Bruce", + // LastName = "Wayne", + // NickName = "Batman", + // Place = "Gotham City", + // DateOfBirth = new DateTime(1915, 4, 7), + // DateCreated = DateTime.Now + //} }; + bool showDeleteDialog = false; + + protected override async Task OnInitializedAsync() + { + var result = await Http.GetFromJsonAsync>("api/contacts"); + if(result is not null) + { + GridData = result; + } + } + void EditContact(int Id) { NavigationManager.NavigateTo($"contacts/edit/{Id}"); } + + void CreateContact() + { + NavigationManager.NavigateTo($"contacts/new"); + } + + void DeleteContact(Contact contact) + { + contactToDelete = contact; + showDeleteDialog = true; + } + + void CancelDeleteContact() + { + showDeleteDialog = false; + } + + async Task ConfirmDeleteContact() + { + if(contactToDelete is null) + { + return; + } + + showDeleteDialog = false; + var result = await Http.DeleteAsync($"api/contacts/{contactToDelete.Id}"); + if (result.IsSuccessStatusCode) + { + var jsonResult = await result.Content.ReadFromJsonAsync>(); + if(jsonResult is not null) + { + GridData = jsonResult; + } + } + } } diff --git a/BlazorSyncfusionCrm/Client/Pages/EditContact.razor b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor new file mode 100644 index 0000000..9a1cf12 --- /dev/null +++ b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor @@ -0,0 +1,91 @@ +@page "/contacts/edit/{id:int}" +@page "/contacts/new" +@inject HttpClient Http +@inject NavigationManager NavigationManager + +@if (Id is not null) +{ +

Edit @contact.NickName

+} +else +{ +

Create a new contact

+} + +
+ + + + + + + + +
+ +
+
+

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

+@code { + [Parameter] + public int? Id { get; set; } + + public Contact contact { get; set; } = new Contact(); + + bool showSuccessMessage = false; + bool showErrorMessage = false; + + protected override async Task OnInitializedAsync() + { + if(Id is not null) + { + var result = await Http.GetAsync($"api/contacts/{Id}"); + if (result.IsSuccessStatusCode) + { + var jsonResult = await result.Content.ReadFromJsonAsync(); + if (jsonResult is not null) + contact = jsonResult; + } + else + { + NavigationManager.NavigateTo("/contacts/new"); + } + } + } + + async Task HandleSubmit() + { + HttpResponseMessage result; + if (Id is not null) + result = await Http.PutAsJsonAsync($"api/contacts/{contact.Id}", contact); + else + result = await Http.PostAsJsonAsync($"api/contacts/", contact); + + if (result.IsSuccessStatusCode) + { + var jsonResult = await result.Content.ReadFromJsonAsync(); + if(jsonResult is not null) + { + contact = jsonResult; + showSuccessMessage = true; + } + } + else + { + showErrorMessage = true; + } + } +} diff --git a/BlazorSyncfusionCrm/Client/Pages/EditContact.razor.css b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor.css new file mode 100644 index 0000000..725e47f --- /dev/null +++ b/BlazorSyncfusionCrm/Client/Pages/EditContact.razor.css @@ -0,0 +1,3 @@ +div { + width: 500px; +} diff --git a/BlazorSyncfusionCrm/Client/Shared/NavBar.razor b/BlazorSyncfusionCrm/Client/Shared/NavBar.razor index 5e40199..7f23f73 100644 --- a/BlazorSyncfusionCrm/Client/Shared/NavBar.razor +++ b/BlazorSyncfusionCrm/Client/Shared/NavBar.razor @@ -10,7 +10,8 @@ - + diff --git a/BlazorSyncfusionCrm/Client/_Imports.razor b/BlazorSyncfusionCrm/Client/_Imports.razor index a51f785..8be29fe 100644 --- a/BlazorSyncfusionCrm/Client/_Imports.razor +++ b/BlazorSyncfusionCrm/Client/_Imports.razor @@ -3,6 +3,7 @@ @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.WebAssembly.Http +@using Microsoft.AspNetCore.Components.Forms; @using Microsoft.JSInterop @using BlazorSyncfusionCrm.Client.Shared @using BlazorSyncfusionCrm.Client @@ -11,4 +12,8 @@ @using Syncfusion.Blazor.Navigations @using Syncfusion.Blazor.Buttons @using Syncfusion.Blazor.Grids +@using Syncfusion.Blazor.Inputs +@using Syncfusion.Blazor.Calendars +@using Syncfusion.Blazor.Notifications +@using Syncfusion.Blazor.Popups diff --git a/BlazorSyncfusionCrm/Client/wwwroot/css/app.css b/BlazorSyncfusionCrm/Client/wwwroot/css/app.css index f0d125d..9f4ce8f 100644 --- a/BlazorSyncfusionCrm/Client/wwwroot/css/app.css +++ b/BlazorSyncfusionCrm/Client/wwwroot/css/app.css @@ -8,6 +8,11 @@ main { margin: 12px; } +.validation-message { + font-size: 12px !important; + font-family: system-ui; +} + h1:focus { outline: none; } diff --git a/BlazorSyncfusionCrm/Client/wwwroot/index.html b/BlazorSyncfusionCrm/Client/wwwroot/index.html index 7881421..88c8266 100644 --- a/BlazorSyncfusionCrm/Client/wwwroot/index.html +++ b/BlazorSyncfusionCrm/Client/wwwroot/index.html @@ -7,6 +7,7 @@ + diff --git a/BlazorSyncfusionCrm/Server/Controllers/ContactsController.cs b/BlazorSyncfusionCrm/Server/Controllers/ContactsController.cs new file mode 100644 index 0000000..c182b5e --- /dev/null +++ b/BlazorSyncfusionCrm/Server/Controllers/ContactsController.cs @@ -0,0 +1,87 @@ +using BlazorSyncfusionCrm.Server.Data; +using BlazorSyncfusionCrm.Shared; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; + +namespace BlazorSyncfusionCrm.Server.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ContactsController : ControllerBase + { + private readonly DataContext _context; + + public ContactsController(DataContext context) + { + _context = context; + } + + [HttpGet] + public async Task>> GetAllContacts() + { + return await _context.Contacts + .Where(c => !c.IsDeleted) + .ToListAsync(); + } + + [HttpGet("{id}")] + public async Task> GetContactById(int id) + { + var result = await _context.Contacts.FindAsync(id); + if (result == null) + { + return NotFound("Contact not found."); + } + return result; + } + + [HttpPost] + public async Task> CreateContact(Contact contact) + { + _context.Contacts.Add(contact); + await _context.SaveChangesAsync(); + return Ok(contact); + } + + [HttpPut("{id}")] + public async Task> UpdateContact(int id, Contact contact) + { + var dbContact = await _context.Contacts.FindAsync(id); + if (dbContact == null) + { + return NotFound("Contact not found."); + } + + dbContact.FirstName = contact.FirstName; + dbContact.LastName = contact.LastName; + dbContact.NickName = contact.NickName; + dbContact.DateOfBirth = contact.DateOfBirth; + dbContact.Place = contact.Place; + dbContact.DateUpdated = DateTime.Now; + + await _context.SaveChangesAsync(); + + return Ok(contact); + + } + + [HttpDelete("{id}")] + public async Task>> DeleteContact(int id) + { + var dbContact = await _context.Contacts.FindAsync(id); + if (dbContact == null) + { + return NotFound("Contact not found."); + } + + dbContact.IsDeleted = true; + dbContact.DateDeleted = DateTime.Now; + + await _context.SaveChangesAsync(); + + return await GetAllContacts(); + + } + } +} diff --git a/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs b/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs new file mode 100644 index 0000000..919c2ab --- /dev/null +++ b/BlazorSyncfusionCrm/Server/Controllers/NotesController.cs @@ -0,0 +1,33 @@ +using BlazorSyncfusionCrm.Server.Data; +using BlazorSyncfusionCrm.Shared; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; + +namespace BlazorSyncfusionCrm.Server.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class NotesController : ControllerBase + { + private readonly DataContext _context; + + public NotesController(DataContext context) + { + _context = context; + } + + [HttpGet] + public async Task>> GetAllNotes() + { + return await _context.Notes + .Include(n => n.Contact) + .OrderByDescending(n => n.DateCreated) + .ToListAsync(); + + } + + + } +} diff --git a/BlazorSyncfusionCrm/Shared/Contact.cs b/BlazorSyncfusionCrm/Shared/Contact.cs index 38019e4..3012dcd 100644 --- a/BlazorSyncfusionCrm/Shared/Contact.cs +++ b/BlazorSyncfusionCrm/Shared/Contact.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Text.Json.Serialization; @@ -12,6 +13,7 @@ namespace BlazorSyncfusionCrm.Shared public int Id { get; set; } public string FirstName { get; set; } = string.Empty; public string LastName { get; set; } = string.Empty; + [Required] public string NickName { get; set; } = string.Empty; public string Place { get; set; } = string.Empty; public bool IsDeleted { get; set; }