Skip to content

Commit

Permalink
Merge pull request #38 from FitIW/feature/Lecture_12
Browse files Browse the repository at this point in the history
Feature/lecture 12
  • Loading branch information
jasho authored Dec 14, 2020
2 parents ca2bc16 + 69d0ae6 commit 06f417a
Show file tree
Hide file tree
Showing 227 changed files with 26,907 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Lectures/Lecture_12/ApiClient.Mobile.nswag

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions Lectures/Lecture_12/ApiClient.Web.nswag

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Lectures/Lecture_12/IW5_12_Xamarin-old.pptx
Git LFS file not shown
3 changes: 3 additions & 0 deletions Lectures/Lecture_12/IW5_12_Xamarin.pptx
Git LFS file not shown
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System;

namespace CookBook.Api.Controllers.v1
{
[ApiController]
[Route("api/[controller]")]
[ApiVersion("1.0")]
public class IngredientController : ControllerBase
{
[HttpGet]
[OpenApiOperation("Ingredient" + nameof(GetAll))]
public IActionResult GetAll()
{
var ingredients = new[]
{
new { Id = new Guid("df935095-8709-4040-a2bb-b6f97cb416dc"), Name = "Vejce", Description = "Popis vajec" },
new { Id = new Guid("23b3902d-7d4f-4213-9cf0-112348f56238"), Name = "Name 2", Description = "Description 2" }
};
return Ok(ingredients);
}

[HttpGet("{id}")]
public IActionResult GetById(Guid id)
{
if (id == new Guid("df935095-8709-4040-a2bb-b6f97cb416dc"))
{
return Ok(new
{
Id = new Guid("df935095-8709-4040-a2bb-b6f97cb416dc"),
Name = "Vejce"
});
}
else
{
return NotFound();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using CookBook.DAL.Entities;
using CookBook.DAL.Repositories;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System;
using System.Collections.Generic;

namespace CookBook.Api.Controllers.v2
{
[ApiController]
[Route("api/[controller]")]
[ApiVersion("2.0")]
public class IngredientController : ControllerBase
{
private readonly IngredientRepository ingredientRepository;

public IngredientController(IngredientRepository ingredientRepository)
{
this.ingredientRepository = ingredientRepository;
}

[HttpGet]
[OpenApiOperation("Ingredient" + nameof(GetAll))]
public IList<IngredientEntity> GetAll()
{
return ingredientRepository.GetAll();
}

[HttpGet("{id}")]
[OpenApiOperation("Ingredient" + nameof(GetById))]
public IngredientEntity GetById(Guid id)
{
return ingredientRepository.GetById(id);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using CookBook.BL.Api.Facades;
using CookBook.Common.Resources;
using CookBook.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CookBook.Api.Controllers.v3
{
[ApiController]
[Route("api/[controller]")]
[ApiVersion("3.0")]
public class IngredientController : ControllerBase
{
private const string ApiOperationBaseName = "Ingredient";
private readonly IngredientFacade ingredientFacade;
private readonly IStringLocalizer<IngredientControllerResources> ingredientControllerLocalizer;

public IngredientController(
IngredientFacade ingredientFacade,
IStringLocalizer<IngredientControllerResources> ingredientControllerLocalizer)
{
this.ingredientFacade = ingredientFacade;
this.ingredientControllerLocalizer = ingredientControllerLocalizer;
}

[HttpGet]
[OpenApiOperation(ApiOperationBaseName + nameof(GetAll))]
public ActionResult<List<IngredientListModel>> GetAll()
{
return ingredientFacade.GetAll().ToList();
}

[HttpGet("{id}")]
[OpenApiOperation(ApiOperationBaseName + nameof(GetById))]
public ActionResult<IngredientDetailModel> GetById(Guid id)
{
var ingredient = ingredientFacade.GetById(id);
if (ingredient == null)
{
return NotFound(ingredientControllerLocalizer[nameof(IngredientControllerResources.GetById_NotFound), id].Value);
}

return ingredient;
}

[HttpPost]
[OpenApiOperation(ApiOperationBaseName + nameof(Create))]
public ActionResult<Guid> Create(IngredientDetailModel ingredient)
{
return ingredientFacade.Create(ingredient);
}

[HttpPut]
[OpenApiOperation(ApiOperationBaseName + nameof(Update))]
public ActionResult<Guid> Update(IngredientDetailModel ingredient)
{
return ingredientFacade.Update(ingredient);
}

[HttpDelete("{id}")]
[OpenApiOperation(ApiOperationBaseName + nameof(Delete))]
public IActionResult Delete(Guid id)
{
ingredientFacade.Delete(id);
return Ok();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSwag.Annotations;
using System;

namespace CookBook.Api.Controllers.v3
{
[ApiController]
[Route("api/[controller]")]
[ApiVersion("3.0")]
public class LoggingController : ControllerBase
{
private const string ApiOperationBaseName = "Logging";
private readonly ILogger<LoggingController> logger;

public LoggingController(ILogger<LoggingController> logger)
{
this.logger = logger;
}

[HttpGet]
[OpenApiOperation(ApiOperationBaseName + nameof(LogSomething))]
public void LogSomething()
{
logger.LogInformation("Something");
logger.LogWarning("Log something called in {0}", DateTime.Now);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using CookBook.BL.Api.Facades;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using CookBook.Models;

namespace CookBook.Api.Controllers.v3
{
[ApiController]
[Route("api/[controller]")]
[ApiVersion("3.0")]
public class RecipeController : ControllerBase
{
private const string ApiOperationBaseName = "Recipe";
private readonly RecipeFacade recipeFacade;

public RecipeController(RecipeFacade recipeFacade)
{
this.recipeFacade = recipeFacade;
}

[HttpGet]
[OpenApiOperation(ApiOperationBaseName + nameof(GetAll))]
public ActionResult<List<RecipeListModel>> GetAll()
{
return recipeFacade.GetAll();
}

[HttpGet("{id}")]
[OpenApiOperation(ApiOperationBaseName + nameof(GetById))]
public ActionResult<RecipeDetailModel> GetById(Guid id)
{
return recipeFacade.GetById(id);
}

[HttpPost]
[OpenApiOperation(ApiOperationBaseName + nameof(Create))]
public ActionResult<Guid> Create(RecipeDetailModel recipe)
{
return recipeFacade.Create(recipe);
}

[HttpPut]
[OpenApiOperation(ApiOperationBaseName + nameof(Update))]
public ActionResult<Guid?> Update(RecipeDetailModel recipe)
{
return recipeFacade.Update(recipe);
}

[HttpDelete("{id}")]
[OpenApiOperation(ApiOperationBaseName + nameof(Delete))]
public IActionResult Delete(Guid id)
{
recipeFacade.Delete(id);
return Ok();
}
}
}
24 changes: 24 additions & 0 deletions Lectures/Lecture_12/sln/CookBook.Api/CookBook.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>70e22f70-c609-4d6a-9be8-b462915b90e5</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
<PackageReference Include="NSwag.AspNetCore" Version="13.2.2" />
<PackageReference Include="Scrutor" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CookBook.BL.Api\CookBook.BL.Api.csproj" />
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CookBook.Api.Middlewares;
using Microsoft.AspNetCore.Builder;

namespace CookBook.Api.Extensions
{
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseRequestCulture(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestCultureMiddleware>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Http;
using System.Globalization;
using System.Threading.Tasks;

namespace CookBook.Api.Middlewares
{
public class RequestCultureMiddleware
{
private readonly RequestDelegate next;

public RequestCultureMiddleware(RequestDelegate next)
{
this.next = next;
}

public async Task InvokeAsync(HttpContext context)
{
var culture = context.Request.Query["culture"];
if (!string.IsNullOrWhiteSpace(culture))
{
var cultureInfo = new CultureInfo(culture);
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
}

await next(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NJsonSchema;
using NSwag;
using NSwag.Generation.Processors;
using NSwag.Generation.Processors.Contexts;

namespace CookBook.Api.Processors
{
public class RequestCultureOperationProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
context.OperationDescription.Operation.Parameters.Add(new OpenApiParameter
{
Name = "culture",
Kind = OpenApiParameterKind.Query,
Type = JsonObjectType.String,
IsRequired = false
});

return true;
}
}
}
33 changes: 33 additions & 0 deletions Lectures/Lecture_12/sln/CookBook.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 CookBook.Api
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(builder =>
{
builder.ClearProviders()
.AddConsole()
.AddFilter("System", LogLevel.Debug)
.SetMinimumLevel(LogLevel.Warning);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Loading

0 comments on commit 06f417a

Please sign in to comment.