Skip to content

Commit

Permalink
Merge pull request #18 from shahedc/XmlJsonSerialzation
Browse files Browse the repository at this point in the history
Xml json serialization
  • Loading branch information
shahedc authored Jun 21, 2020
2 parents cc86c78 + 905e38a commit a12d913
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/NetLearner.Api/Controllers/LearningResourcesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NetLearner.Api.Infrastructure;
using NetLearner.SharedLib.Models;

namespace NetLearner.Api.Controllers
{
// NOTE: order of precedence
// 1. public JsonResult Get()
// 2. [Produces("application/json")]
// 3. Accept: application/json

// [Produces("application/xml")]
// [Produces("application/json")]
[ApiController]
[Route("api/[controller]")]
public class LearningResourcesController : ControllerBase
{
private readonly ISampleRepository _sampleRepository;

public LearningResourcesController(ISampleRepository sampleRepository)
{
_sampleRepository = sampleRepository;
}

// GET: api/LearningResources
[HttpGet]
public JsonResult Get()
{
return new JsonResult(_sampleRepository.LearningResources());
}

// GET: api/LearningResources/search?fragment=ir
[HttpGet("Search")]
public IActionResult Search(string fragment)
{
var result = _sampleRepository.GetByPartialName(fragment);
if (!result.Any())
{
return NotFound(fragment);
}
return Ok(result);
}
}
}
14 changes: 14 additions & 0 deletions src/NetLearner.Api/Infrastructure/ISampleRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using NetLearner.SharedLib.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetLearner.Api.Infrastructure
{
public interface ISampleRepository
{
List<LearningResource> LearningResources();
List<LearningResource> GetByPartialName(string nameSubstring);
}
}
52 changes: 52 additions & 0 deletions src/NetLearner.Api/Infrastructure/SampleRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using NetLearner.SharedLib.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetLearner.Api.Infrastructure
{
public class SampleRepository : ISampleRepository
{

public List<LearningResource> LearningResources()
{
var resourceLists = new List<ResourceList>();
var resourceList = new ResourceList
{
Id = 1,
Name = "RL1",
LearningResources = new List<LearningResource>()
};
resourceLists.Add(resourceList);
return new List<LearningResource>
{
new LearningResource
{
Name = "ASP .NET Core Docs",
Url = "https://docs.microsoft.com/aspnet/core",
ResourceListId = 1,
ResourceList = resourceList
},
new LearningResource
{
Name = "Wake Up And Code!",
Url = "https://WakeUpAndCode.com",
ResourceListId = 1,
ResourceList = resourceList,
ContentFeedUrl = "https://WakeUpAndCode.com/rss"
},
}
.OrderBy(ci => ci.Name).ToList();
}

public List<LearningResource> GetByPartialName(string nameSubstring)
{
return LearningResources()
.Where(ci => ci.Name
.IndexOf(nameSubstring, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
.ToList();

}
}
}
12 changes: 12 additions & 0 deletions src/NetLearner.Api/NetLearner.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\NetLearner.SharedLib\NetLearner.SharedLib.csproj" />
</ItemGroup>


</Project>
26 changes: 26 additions & 0 deletions src/NetLearner.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace NetLearner.Api
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
30 changes: 30 additions & 0 deletions src/NetLearner.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64982",
"sslPort": 44350
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/LearningResources",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"NetLearner.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
53 changes: 53 additions & 0 deletions src/NetLearner.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NetLearner.Api.Infrastructure;

namespace NetLearner.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<ISampleRepository, SampleRepository>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
9 changes: 9 additions & 0 deletions src/NetLearner.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions src/NetLearner.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
8 changes: 7 additions & 1 deletion src/NetLearnerApp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetLearner.Mvc.Tests", "Net
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetLearner.Pages.Tests", "NetLearner.Pages.Tests\NetLearner.Pages.Tests.csproj", "{86A29A92-73D9-405E-95C0-886E0C20896B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetLearner.Blazor.Tests", "NetLearner.Blazor.Tests\NetLearner.Blazor.Tests.csproj", "{0EA78035-4680-4971-BCE3-A18DA814E28C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetLearner.Blazor.Tests", "NetLearner.Blazor.Tests\NetLearner.Blazor.Tests.csproj", "{0EA78035-4680-4971-BCE3-A18DA814E28C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetLearner.Api", "NetLearner.Api\NetLearner.Api.csproj", "{30394ACF-192B-4F67-8E59-EC3079C85483}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -77,6 +79,10 @@ Global
{0EA78035-4680-4971-BCE3-A18DA814E28C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EA78035-4680-4971-BCE3-A18DA814E28C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EA78035-4680-4971-BCE3-A18DA814E28C}.Release|Any CPU.Build.0 = Release|Any CPU
{30394ACF-192B-4F67-8E59-EC3079C85483}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30394ACF-192B-4F67-8E59-EC3079C85483}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30394ACF-192B-4F67-8E59-EC3079C85483}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30394ACF-192B-4F67-8E59-EC3079C85483}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a12d913

Please sign in to comment.