Skip to content

Commit

Permalink
Small addition to BaseODataController, reusable JS code in MVC OData …
Browse files Browse the repository at this point in the history
…demo and updated some NuGet packages.
  • Loading branch information
gordon-matt committed May 21, 2022
1 parent 10df0f3 commit 3e99016
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="6.3.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2" PrivateAssets="all">
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.5" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Demos/Demo.AspNetCore.Mvc.OData/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@

<script src="~/kendo-sdk/js/jszip.min.js"></script>
<script src="~/kendo-sdk/js/kendo.all.min.js"></script>

<script src="~/js/odata-helpers.js"></script>

@RenderSection("Scripts", required: false)
</body>
Expand Down
96 changes: 17 additions & 79 deletions Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/app/people.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -107,106 +107,44 @@ 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(),
DateOfBirth: self.dateOfBirth()
};

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();
};
};
105 changes: 105 additions & 0 deletions Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/odata-helpers.js
Original file line number Diff line number Diff line change
@@ -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();
};
2 changes: 1 addition & 1 deletion Demos/Demo.AspNetCore.Mvc.OData/wwwroot/js/site.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// Write your JavaScript code.
// Write your JavaScript code.
2 changes: 1 addition & 1 deletion Demos/Demo.Data.InfoSchema/Demo.Data.InfoSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.3.310801">
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.3.326103">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
<ItemGroup>
<PackageReference Include="Autofac" Version="6.3.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="Blazorise.Bootstrap" Version="0.9.5.5" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="0.9.5.5" />
<PackageReference Include="EPPlus" Version="5.8.6" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.2">
<PackageReference Include="Blazorise.Bootstrap" Version="1.0.4" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.0.4" />
<PackageReference Include="EPPlus" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Radzen.Blazor" Version="3.15.5" />
<PackageReference Include="Radzen.Blazor" Version="3.18.10" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Demos/Demo.Extenso.AspNetCore.Blazor.OData/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddBlazorise(options =>
{
options.ChangeTextOnKeyPress = true; // optional
//options.ChangeTextOnKeyPress = true; // optional
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
Expand Down
4 changes: 2 additions & 2 deletions Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<PackageLicenseUrl>https://raw.githubusercontent.com/gordon-matt/Extenso/master/LICENSE.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/gordon-matt/Extenso</PackageProjectUrl>
<Version>6.0.2</Version>
<Version>6.0.3</Version>
<Description></Description>
</PropertyGroup>

Expand All @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions Extenso.AspNetCore.OData/GenericODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,10 @@ public override async Task<IActionResult> Get([FromODataUri] TKey key)

return Ok(result);
}

protected override TKey GetId(TEntity entity)
{
return entity.Id;
}
}
}
8 changes: 4 additions & 4 deletions Extenso.Data.Entity/Extenso.Data.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
<PackageProjectUrl>https://github.com/gordon-matt/Extenso</PackageProjectUrl>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>This library provides a generic repository interface and base class, as well as other data-related extension methods and helper classes.</Description>
<Version>6.0.1</Version>
<Version>6.0.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard2.1\Extenso.Data.Entity.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.2" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.20" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Extenso.Data.MySql/Extenso.Data.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<PackageProjectUrl>https://github.com/gordon-matt/Extenso</PackageProjectUrl>
<NeutralLanguage>en-US</NeutralLanguage>
<Description>Data-related extension methods and other helper classes for MySql</Description>
<Version>6.0.1</Version>
<Version>6.0.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard2.1\Extenso.Data.MySql.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.28" />
<PackageReference Include="MySql.Data" Version="8.0.29" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 3e99016

Please sign in to comment.