From 3e99016a2360314c2201946103b69771274a4da7 Mon Sep 17 00:00:00 2001 From: Matt Gordon Date: Sat, 21 May 2022 20:19:36 +0930 Subject: [PATCH] Small addition to BaseODataController, reusable JS code in MVC OData demo and updated some NuGet packages. --- .../Demo.Extenso.AspNetCore.Mvc.OData.csproj | 8 +- .../Views/Shared/_Layout.cshtml | 2 + .../wwwroot/js/app/people.js | 96 +++------------- .../wwwroot/js/odata-helpers.js | 105 ++++++++++++++++++ .../wwwroot/js/site.js | 2 +- .../Demo.Data.InfoSchema.csproj | 2 +- ...emo.Extenso.AspNetCore.Blazor.OData.csproj | 20 ++-- .../Startup.cs | 2 +- .../Extenso.AspNetCore.OData.csproj | 4 +- .../GenericODataController.cs | 5 + .../Extenso.Data.Entity.csproj | 8 +- Extenso.Data.MySql/Extenso.Data.MySql.csproj | 4 +- .../Extenso.Data.Npgsql.csproj | 4 +- 13 files changed, 156 insertions(+), 106 deletions(-) create mode 100644 Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/odata-helpers.js diff --git a/Demos/Demo.AspNetCore.Mvc.OData/Demo.Extenso.AspNetCore.Mvc.OData.csproj b/Demos/Demo.AspNetCore.Mvc.OData/Demo.Extenso.AspNetCore.Mvc.OData.csproj index 2c0834b..05cbbf5 100644 --- a/Demos/Demo.AspNetCore.Mvc.OData/Demo.Extenso.AspNetCore.Mvc.OData.csproj +++ b/Demos/Demo.AspNetCore.Mvc.OData/Demo.Extenso.AspNetCore.Mvc.OData.csproj @@ -8,12 +8,12 @@ - - - + + + runtime; build; native; contentfiles; analyzers - + diff --git a/Demos/Demo.AspNetCore.Mvc.OData/Views/Shared/_Layout.cshtml b/Demos/Demo.AspNetCore.Mvc.OData/Views/Shared/_Layout.cshtml index d6ad4b9..2dc0cb1 100644 --- a/Demos/Demo.AspNetCore.Mvc.OData/Views/Shared/_Layout.cshtml +++ b/Demos/Demo.AspNetCore.Mvc.OData/Views/Shared/_Layout.cshtml @@ -78,6 +78,8 @@ + + @RenderSection("Scripts", required: false) diff --git a/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/app/people.js b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/app/people.js index bec56f3..eea33de 100644 --- a/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/app/people.js +++ b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/app/people.js @@ -49,7 +49,7 @@ var ViewModel = function () { ] }, dataBound: function (e) { - var body = this.element.find("tbody")[0]; + let body = this.element.find("tbody")[0]; if (body) { ko.cleanNode(body); ko.applyBindings(ko.dataFor(body), body); @@ -107,54 +107,29 @@ var ViewModel = function () { $("#form-section-legend").html("Create"); }; - self.edit = function (id) { - $.ajax({ - url: self.apiUrl + "(" + id + ")", - type: "GET", - dataType: "json", - async: false - }) - .done(function (json) { - self.id(json.Id); - self.familyName(json.FamilyName); - self.givenNames(json.GivenNames); - self.dateOfBirth(json.DateOfBirth); - - switchSection($("#form-section")); - $("#form-section-legend").html("Edit"); - }) - .fail(function (jqXHR, textStatus, errorThrown) { - $.notify({ message: "Error when trying to retrieve record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); - console.log(textStatus + ': ' + errorThrown); - }); + self.edit = async function (id) { + const data = await getOData(`${self.apiUrl}(${id})`); + self.id(data.Id); + self.familyName(data.FamilyName); + self.givenNames(data.GivenNames); + self.dateOfBirth(data.DateOfBirth); + + switchSection($("#form-section")); + $("#form-section-legend").html("Edit"); }; - self.remove = function (id) { - if (confirm("Are you sure that you want to delete this record?")) { - $.ajax({ - url: self.apiUrl + "(" + id + ")", - type: "DELETE", - async: false - }) - .done(function (json) { - self.refreshGrid(); - $.notify({ message: "Successfully deleted record!", icon: 'fa fa-check' }, { type: 'success' }); - }) - .fail(function (jqXHR, textStatus, errorThrown) { - $.notify({ message: "Error when trying to delete record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); - console.log(textStatus + ': ' + errorThrown); - }); - } + self.remove = async function (id) { + await deleteOData(`${self.apiUrl}(${id})`); }; - self.save = function () { - var isNew = (self.id() == 0); + self.save = async function () { + const isNew = (self.id() == 0); if (!$("#form-section-form").valid()) { return false; } - var record = { + const record = { Id: self.id(), FamilyName: self.familyName(), GivenNames: self.givenNames(), @@ -162,51 +137,14 @@ var ViewModel = function () { }; if (isNew) { - $.ajax({ - url: self.apiUrl, - type: "POST", - contentType: "application/json; charset=utf-8", - data: JSON.stringify(record), - dataType: "json", - async: false - }) - .done(function (json) { - self.refreshGrid(); - switchSection($("#grid-section")); - $.notify({ message: "Successfully inserted record!", icon: 'fa fa-check' }, { type: 'success' }); - }) - .fail(function (jqXHR, textStatus, errorThrown) { - $.notify({ message: "Error when trying to insert record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); - console.log(textStatus + ': ' + errorThrown); - }); + await postOData(self.apiUrl, record); } else { - $.ajax({ - url: self.apiUrl + "(" + self.id() + ")", - type: "PUT", - contentType: "application/json; charset=utf-8", - data: JSON.stringify(record), - dataType: "json", - async: false - }) - .done(function (json) { - self.refreshGrid(); - switchSection($("#grid-section")); - $.notify({ message: "Successfully updated record!", icon: 'fa fa-check' }, { type: 'success' }); - }) - .fail(function (jqXHR, textStatus, errorThrown) { - $.notify({ message: "Error when trying to update record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); - console.log(textStatus + ': ' + errorThrown); - }); + await putOData(`${self.apiUrl}(${self.id()})`, record); } }; self.cancel = function () { switchSection($("#grid-section")); }; - - self.refreshGrid = function () { - $('#grid').data('kendoGrid').dataSource.read(); - $('#grid').data('kendoGrid').refresh(); - }; }; \ No newline at end of file diff --git a/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/odata-helpers.js b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/odata-helpers.js new file mode 100644 index 0000000..9ec5e19 --- /dev/null +++ b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/odata-helpers.js @@ -0,0 +1,105 @@ +async function getOData(url) { + return await fetch(url) + .then(response => response.json()) + .catch(error => { + $.notify({ message: "Error when trying to retrieve record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + console.error('Error: ', error); + }); +} + +async function deleteOData(url) { + if (confirm("Are you sure that you want to delete this record?")) { + await fetch(url, { method: 'DELETE' }) + .then(response => { + if (response.ok) { + refreshODataGrid(); + $.notify({ message: "Successfully deleted record!", icon: 'fa fa-check' }, { type: 'success' }); + } else { + $.notify({ message: "Error when trying to retrieve record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + } + }) + .catch(error => { + $.notify({ message: "Error when trying to retrieve record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + console.error('Error: ', error); + }); + } +} + +async function postOData(url, record) { + return await fetch(url, { + method: "POST", + headers: { + 'Content-type': 'application/json; charset=utf-8' + }, + body: JSON.stringify(record) + }) + .then(response => { + if (response.ok) { + refreshODataGrid(); + switchSection($("#grid-section")); + $.notify({ message: "Successfully inserted record!", icon: 'fa fa-check' }, { type: 'success' }); + } + else { + $.notify({ message: "Error when trying to insert record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + } + return response; + }) + .catch(error => { + $.notify({ message: "Error when trying to insert record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + console.error('Error: ', error); + }); +} + +async function putOData(url, record) { + return await fetch(url, { + method: "PUT", + headers: { + 'Content-type': 'application/json; charset=utf-8' + }, + body: JSON.stringify(record) + }) + .then(response => { + if (response.ok) { + refreshODataGrid(); + switchSection($("#grid-section")); + $.notify({ message: "Successfully updated record!", icon: 'fa fa-check' }, { type: 'success' }); + } + else { + $.notify({ message: "Error when trying to update record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + } + return response; + }) + .catch(error => { + $.notify({ message: "Error when trying to update record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + console.error('Error: ', error); + }); +} + +async function patchOData(url, patch) { + return await fetch(url, { + method: "PATCH", + headers: { + 'Content-type': 'application/json; charset=utf-8' + }, + body: JSON.stringify(patch) + }) + .then(response => { + if (response.ok) { + refreshODataGrid(); + $.notify({ message: "Successfully updated record!", icon: 'fa fa-check' }, { type: 'success' }); + } + else { + $.notify({ message: "Error when trying to update record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + } + return response; + }) + .catch(error => { + $.notify({ message: "Error when trying to update record!", icon: 'fa fa-exclamation-triangle' }, { type: 'danger' }); + console.error('Error: ', error); + }); +} + +function refreshODataGrid() { + $('#grid').data('kendoGrid').dataSource.read(); + $('#grid').data('kendoGrid').refresh(); +}; \ No newline at end of file diff --git a/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/site.js b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/site.js index 0f3411a..afce33d 100644 --- a/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/site.js +++ b/Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/site.js @@ -1 +1 @@ -// Write your JavaScript code. +// Write your JavaScript code. \ No newline at end of file diff --git a/Demos/Demo.Data.InfoSchema/Demo.Data.InfoSchema.csproj b/Demos/Demo.Data.InfoSchema/Demo.Data.InfoSchema.csproj index 27dfb68..569a927 100644 --- a/Demos/Demo.Data.InfoSchema/Demo.Data.InfoSchema.csproj +++ b/Demos/Demo.Data.InfoSchema/Demo.Data.InfoSchema.csproj @@ -18,7 +18,7 @@ - + all diff --git a/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Demo.Extenso.AspNetCore.Blazor.OData.csproj b/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Demo.Extenso.AspNetCore.Blazor.OData.csproj index 60cfc69..9ec84d7 100644 --- a/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Demo.Extenso.AspNetCore.Blazor.OData.csproj +++ b/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Demo.Extenso.AspNetCore.Blazor.OData.csproj @@ -8,19 +8,19 @@ - - - - - - - - - + + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Startup.cs b/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Startup.cs index d6a257a..b293542 100644 --- a/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Startup.cs +++ b/Demos/Demo.Extenso.AspNetCore.Blazor.OData/Startup.cs @@ -64,7 +64,7 @@ public void ConfigureServices(IServiceCollection services) services.AddBlazorise(options => { - options.ChangeTextOnKeyPress = true; // optional + //options.ChangeTextOnKeyPress = true; // optional }) .AddBootstrapProviders() .AddFontAwesomeIcons(); diff --git a/Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj b/Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj index 4f00276..030254c 100644 --- a/Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj +++ b/Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj @@ -6,7 +6,7 @@ en-US https://raw.githubusercontent.com/gordon-matt/Extenso/master/LICENSE.txt https://github.com/gordon-matt/Extenso - 6.0.2 + 6.0.3 @@ -19,7 +19,7 @@ - + diff --git a/Extenso.AspNetCore.OData/GenericODataController.cs b/Extenso.AspNetCore.OData/GenericODataController.cs index 7add038..83ac8e1 100644 --- a/Extenso.AspNetCore.OData/GenericODataController.cs +++ b/Extenso.AspNetCore.OData/GenericODataController.cs @@ -459,5 +459,10 @@ public override async Task Get([FromODataUri] TKey key) return Ok(result); } + + protected override TKey GetId(TEntity entity) + { + return entity.Id; + } } } \ No newline at end of file diff --git a/Extenso.Data.Entity/Extenso.Data.Entity.csproj b/Extenso.Data.Entity/Extenso.Data.Entity.csproj index b21dc78..2e32c4e 100644 --- a/Extenso.Data.Entity/Extenso.Data.Entity.csproj +++ b/Extenso.Data.Entity/Extenso.Data.Entity.csproj @@ -8,7 +8,7 @@ https://github.com/gordon-matt/Extenso en-US This library provides a generic repository interface and base class, as well as other data-related extension methods and helper classes. - 6.0.1 + 6.0.2 @@ -16,9 +16,9 @@ - - - + + + diff --git a/Extenso.Data.MySql/Extenso.Data.MySql.csproj b/Extenso.Data.MySql/Extenso.Data.MySql.csproj index 86e755e..2bb6a0c 100644 --- a/Extenso.Data.MySql/Extenso.Data.MySql.csproj +++ b/Extenso.Data.MySql/Extenso.Data.MySql.csproj @@ -7,7 +7,7 @@ https://github.com/gordon-matt/Extenso en-US Data-related extension methods and other helper classes for MySql - 6.0.1 + 6.0.2 @@ -15,7 +15,7 @@ - + diff --git a/Extenso.Data.Npgsql/Extenso.Data.Npgsql.csproj b/Extenso.Data.Npgsql/Extenso.Data.Npgsql.csproj index b5277fb..1b804c5 100644 --- a/Extenso.Data.Npgsql/Extenso.Data.Npgsql.csproj +++ b/Extenso.Data.Npgsql/Extenso.Data.Npgsql.csproj @@ -7,7 +7,7 @@ https://github.com/gordon-matt/Extenso en-US Data-related extension methods and other helper classes for Npgsql - 6.0.1 + 6.0.2 @@ -15,7 +15,7 @@ - +