Creating Modern WebAPIs with ASP.NET Core

Creating Modern WebAPIs with ASP.NET Core

In distributed applications, the backend plays a decisive role, and the development of the backend has become a separate area. It should be quick and easy to use, provide the data for the frontend and be well documented. In this talk by Fabian Gosebrink you will not only hear how to create a RESTful Web API with ASP.NET Core. You'll also learn how to document it with tools and how to extend it with useful features. The end result is a clean backend that can be used from any client.

Fb89953d3a1b1fcb862a186585c37c25?s=128

Fabian Gosebrink

February 25, 2020
Tweet

Transcript

  1. Creating Modern Creating Modern WebAPIs With WebAPIs With ASP.NET Core

    3 ASP.NET Core 3
  2. Web

  3. Web Desktop

  4. Mobile Desktop Web

  5. Mobile Desktop Web ...

  6. None
  7. WebAPI Mobile Desktop Web ...

  8. Tooling

  9. Tooling DOTNET CLI

  10. .NET CLI dotnet new ... 1 2 dotnet new webapi

    3
  11. .NET CLI dotnet new ... 1 2 dotnet new webapi

    3 dotnet new webapi dotnet new ... 1 2 3
  12. None
  13. None
  14. None
  15. public class Program { } 1 2 public static void

    Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 14
  16. public class Program { } 1 2 public static void

    Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 14 public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public class Program 1 { 2 3 4 5 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 } 14
  17. public class Program { } 1 2 public static void

    Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 14 public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public class Program 1 { 2 3 4 5 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 } 14 public static IHostBuilder CreateHostBuilder(string[] args Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 8 9 10 11 12 13 } 14
  18. public class Program { } 1 2 public static void

    Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 14 public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public class Program 1 { 2 3 4 5 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 } 14 public static IHostBuilder CreateHostBuilder(string[] args Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 8 9 10 11 12 13 } 14 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 10 11 12 13 } 14
  19. public class Program { } 1 2 public static void

    Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 14 public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public class Program 1 { 2 3 4 5 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 webBuilder.UseStartup<Startup>(); 12 }); 13 } 14 public static IHostBuilder CreateHostBuilder(string[] args Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 8 9 10 11 12 13 } 14 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 10 11 12 13 } 14 webBuilder.UseStartup<Startup>(); public class Program 1 { 2 public static void Main(string[] args) 3 { 4 CreateHostBuilder(args).Build().Run(); 5 } 6 7 public static IHostBuilder CreateHostBuilder(string[] args 8 Host.CreateDefaultBuilder(args) 9 .ConfigureWebHostDefaults(webBuilder => 10 { 11 12 }); 13 } 14
  20. public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration

    Configuration { get; } public class Startup 1 { 2 3 4 5 6 7 8 9 public void ConfigureServices(IServiceCollection services) 10 { 11 services.AddControllers(); 12 } 13 14 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 15 { 16 if (env.IsDevelopment()) 17 { 18 app.UseDeveloperExceptionPage(); 19 } 20 21 app.UseHttpsRedirection(); 22 23 app.UseRouting(); 24 25 app.UseAuthorization(); 26 27 app.UseEndpoints(endpoints => 28 { 29 endpoints.MapControllers(); 30 }); 31 } 32 } 33
  21. public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration

    Configuration { get; } public class Startup 1 { 2 3 4 5 6 7 8 9 public void ConfigureServices(IServiceCollection services) 10 { 11 services.AddControllers(); 12 } 13 14 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 15 { 16 if (env.IsDevelopment()) 17 { 18 app.UseDeveloperExceptionPage(); 19 } 20 21 app.UseHttpsRedirection(); 22 23 app.UseRouting(); 24 25 app.UseAuthorization(); 26 27 app.UseEndpoints(endpoints => 28 { 29 endpoints.MapControllers(); 30 }); 31 } 32 } 33 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } public class Startup 1 { 2 public Startup(IConfiguration configuration) 3 { 4 Configuration = configuration; 5 } 6 7 public IConfiguration Configuration { get; } 8 9 10 11 12 13 14 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 15 { 16 if (env.IsDevelopment()) 17 { 18 app.UseDeveloperExceptionPage(); 19 } 20 21 app.UseHttpsRedirection(); 22 23 app.UseRouting(); 24 25 app.UseAuthorization(); 26 27 app.UseEndpoints(endpoints => 28 { 29 endpoints.MapControllers(); 30 }); 31 } 32 } 33
  22. public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration

    Configuration { get; } public class Startup 1 { 2 3 4 5 6 7 8 9 public void ConfigureServices(IServiceCollection services) 10 { 11 services.AddControllers(); 12 } 13 14 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 15 { 16 if (env.IsDevelopment()) 17 { 18 app.UseDeveloperExceptionPage(); 19 } 20 21 app.UseHttpsRedirection(); 22 23 app.UseRouting(); 24 25 app.UseAuthorization(); 26 27 app.UseEndpoints(endpoints => 28 { 29 endpoints.MapControllers(); 30 }); 31 } 32 } 33 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } public class Startup 1 { 2 public Startup(IConfiguration configuration) 3 { 4 Configuration = configuration; 5 } 6 7 public IConfiguration Configuration { get; } 8 9 10 11 12 13 14 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 15 { 16 if (env.IsDevelopment()) 17 { 18 app.UseDeveloperExceptionPage(); 19 } 20 21 app.UseHttpsRedirection(); 22 23 app.UseRouting(); 24 25 app.UseAuthorization(); 26 27 app.UseEndpoints(endpoints => 28 { 29 endpoints.MapControllers(); 30 }); 31 } 32 } 33 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } public class Startup 1 { 2 public Startup(IConfiguration configuration) 3 { 4 Configuration = configuration; 5 } 6 7 public IConfiguration Configuration { get; } 8 9 public void ConfigureServices(IServiceCollection services) 10 { 11 services.AddControllers(); 12 } 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 } 33
  23. JSON Serialization

  24. JSON Serialization

  25. JSON Serialization Newtonsoft.Jso n

  26. JSON Serialization Newtonsoft.Jso n System.Text.Js on

  27. JSON Serialization > Install­Package Microsoft.AspNetCore.Mvc.NewtonsoftJs on

  28. JSON Serialization > Install­Package Microsoft.AspNetCore.Mvc.NewtonsoftJs on

  29. JSON Serialization

  30. Service Registration

  31. Service Registration

  32. Service Registration services.AddControllers(); services.AddControllersWithViews() ; services.AddRazorPages();

  33. Service Registration Method Controllers Views Pages AddControllers(); Y N N

    AddControllersWithViews(); Y Y N AddRazorPages(); N N Y
  34. Routing

  35. Startup

  36. REST

  37. REST What is ?

  38. REST is not WebAPI

  39. REST is not HTTP

  40. Architectural Style

  41. GET /api/houses/ GET /api/houses/1 POST /api/houses/ PUT /api/houses/1 DELETE /api/houses/1

  42. REST Constraints

  43. REST Constraints

  44. REST Constraints Client / Server Cacheable Stateless Layered System Uniform

    Interface Code on Demand
  45. Richardson Maturity Model

  46. https://martinfowler.com/articles/richardsonMaturityModel.html

  47. HATEOAS

  48. Hypermedia As The Engine Of Application State

  49. { "id": 1, "name": "Lasagne", "type": "Starter", "calories": 1000, "created":

    "2020-02-23T15:42:45.4923318+01:0 } 1 2 3 4 5 6 7 GET api/v1/foods/1
  50. { "id": 1, "name": "Lasagne", "type": "Starter", "calories": 1000, "created":

    "2020-02-23T15:42:45.4923318+01:00", "links": [ { "href": "https://localhost:5001/api/v1/foods/1", "rel": "self", "method": "GET" }, { "href": "https://localhost:5001/api/v1/foods/1", "rel": "delete_food", "method": "DELETE" }, { "href": "https://localhost:5001/api/v1/foods", "rel": "create_food", "method": "POST" }, { "href": "https://localhost:5001/api/v1/foods/1", "rel": "update_food", "method": "PUT" } ] } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 GET api/v1/foods/1
  51. Statuscodes

  52. None
  53. 2xx

  54. 4xx

  55. 500

  56. let ok = status >= 200 && status < 30

    1 https://github.com/angular/angular/blob/master/packages/common/http/src/xhr.ts#L180
  57. The Controller

  58. [HttpGet(Name = nameof(GetAllFoods))] public ActionResult GetAllFoods() { List<FoodEntity> foodItems =

    _foodRepository.GetAll().ToLi return Ok(_mapper.Map(foodItems)); } 1 2 3 4 5 6 7
  59. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13
  60. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 [Route("{id:int}", Name = nameof(GetSingleFood))] [HttpGet] 1 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13
  61. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 [Route("{id:int}", Name = nameof(GetSingleFood))] [HttpGet] 1 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 public ActionResult GetSingleFood(int id) [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13
  62. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 [Route("{id:int}", Name = nameof(GetSingleFood))] [HttpGet] 1 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 public ActionResult GetSingleFood(int id) [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 FoodEntity foodItem = _foodRepository.GetSingle(id); [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13
  63. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 [Route("{id:int}", Name = nameof(GetSingleFood))] [HttpGet] 1 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 public ActionResult GetSingleFood(int id) [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 FoodEntity foodItem = _foodRepository.GetSingle(id); [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 if (foodItem == null) { return NotFound(); } [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 7 8 9 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13
  64. [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 [Route("{id:int}", Name = nameof(GetSingleFood))] [HttpGet] 1 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 public ActionResult GetSingleFood(int id) [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 FoodEntity foodItem = _foodRepository.GetSingle(id); [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 if (foodItem == null) { return NotFound(); } [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 7 8 9 10 11 return Ok(_mapper.Map(foodItem)); 12 } 13 return Ok(_mapper.Map(foodItem)); [HttpGet] 1 [Route("{id:int}", Name = nameof(GetSingleFood))] 2 public ActionResult GetSingleFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 12 } 13
  65. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  66. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  67. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (foodCreateDto == null) { return BadRequest(); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 4 5 6 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  68. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (foodCreateDto == null) { return BadRequest(); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 4 5 6 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  69. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (foodCreateDto == null) { return BadRequest(); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 4 5 6 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 _foodRepository.Add(toAdd); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  70. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (foodCreateDto == null) { return BadRequest(); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 4 5 6 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 _foodRepository.Add(toAdd); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (!_foodRepository.Save()) { throw new Exception("Creating a fooditem failed on save."); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 13 14 15 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22
  71. [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2

    { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) [HttpPost(Name = nameof(AddFood))] 1 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (foodCreateDto == null) { return BadRequest(); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 4 5 6 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 _foodRepository.Add(toAdd); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 if (!_foodRepository.Save()) { throw new Exception("Creating a fooditem failed on save."); } [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 13 14 15 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, 20 _mapper.Map<FoodDto>(newFoodItem)); 21 } 22 return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id }, _mapper.Map<FoodDto>(newFoodItem)); [HttpPost(Name = nameof(AddFood))] 1 public ActionResult<FoodDto> AddFood([FromBody] FoodCreateDto foodCreateDto) 2 { 3 if (foodCreateDto == null) 4 { 5 return BadRequest(); 6 } 7 8 FoodEntity toAdd = _mapper.Map<FoodEntity>(foodCreateDto); 9 10 _foodRepository.Add(toAdd); 11 12 if (!_foodRepository.Save()) 13 { 14 throw new Exception("Creating a fooditem failed on save."); 15 } 16 17 FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id); 18 19 20 21 } 22
  72. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  73. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  74. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  75. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (foodUpdateDto == null) { return BadRequest(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 5 6 7 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  76. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (foodUpdateDto == null) { return BadRequest(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 5 6 7 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 var existingFoodItem = _foodRepository.GetSingle(id); if (existingFoodItem == null) { return NotFound(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 10 11 12 13 14 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  77. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (foodUpdateDto == null) { return BadRequest(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 5 6 7 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 var existingFoodItem = _foodRepository.GetSingle(id); if (existingFoodItem == null) { return NotFound(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 10 11 12 13 14 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 _mapper.Map(foodUpdateDto, existingFoodItem); _foodRepository.Update(id, existingFoodItem); [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 17 18 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  78. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (foodUpdateDto == null) { return BadRequest(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 5 6 7 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 var existingFoodItem = _foodRepository.GetSingle(id); if (existingFoodItem == null) { return NotFound(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 10 11 12 13 14 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 _mapper.Map(foodUpdateDto, existingFoodItem); _foodRepository.Update(id, existingFoodItem); [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 17 18 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (!_foodRepository.Save()) { throw new Exception("Updating a fooditem failed on save."); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 21 22 23 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27
  79. [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int

    id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 [Route("{id:int}", Name = nameof(UpdateFood))] [HttpPut] 1 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (foodUpdateDto == null) { return BadRequest(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 5 6 7 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 var existingFoodItem = _foodRepository.GetSingle(id); if (existingFoodItem == null) { return NotFound(); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 10 11 12 13 14 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 _mapper.Map(foodUpdateDto, existingFoodItem); _foodRepository.Update(id, existingFoodItem); [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 17 18 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 if (!_foodRepository.Save()) { throw new Exception("Updating a fooditem failed on save."); } [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 21 22 23 24 25 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); 26 } 27 return Ok(_mapper.Map<FoodDto>(existingFoodItem)); [HttpPut] 1 [Route("{id:int}", Name = nameof(UpdateFood))] 2 public ActionResult<FoodDto> UpdateFood(int id, [FromBody]FoodUpdateDto foodUpda 3 { 4 if (foodUpdateDto == null) 5 { 6 return BadRequest(); 7 } 8 9 var existingFoodItem = _foodRepository.GetSingle(id); 10 11 if (existingFoodItem == null) 12 { 13 return NotFound(); 14 } 15 16 _mapper.Map(foodUpdateDto, existingFoodItem); 17 18 _foodRepository.Update(id, existingFoodItem); 19 20 if (!_foodRepository.Save()) 21 { 22 throw new Exception("Updating a fooditem failed on save."); 23 } 24 25 26 } 27
  80. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20
  81. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 [Route("{id:int}", Name = nameof(RemoveFood))] [HttpDelete] 1 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20
  82. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 [Route("{id:int}", Name = nameof(RemoveFood))] [HttpDelete] 1 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 public ActionResult RemoveFood(int id) [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20
  83. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 [Route("{id:int}", Name = nameof(RemoveFood))] [HttpDelete] 1 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 public ActionResult RemoveFood(int id) [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 FoodEntity foodItem = _foodRepository.GetSingle(id); if (foodItem == null) { return NotFound(); } [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 5 6 7 8 9 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20
  84. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 [Route("{id:int}", Name = nameof(RemoveFood))] [HttpDelete] 1 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 public ActionResult RemoveFood(int id) [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 FoodEntity foodItem = _foodRepository.GetSingle(id); if (foodItem == null) { return NotFound(); } [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 5 6 7 8 9 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 _foodRepository.Delete(id); if (!_foodRepository.Save()) { throw new Exception("Deleting a fooditem failed on save } [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 12 13 14 15 16 17 18 return NoContent(); 19 } 20
  85. [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int

    id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 [Route("{id:int}", Name = nameof(RemoveFood))] [HttpDelete] 1 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 public ActionResult RemoveFood(int id) [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 FoodEntity foodItem = _foodRepository.GetSingle(id); if (foodItem == null) { return NotFound(); } [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 5 6 7 8 9 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 return NoContent(); 19 } 20 _foodRepository.Delete(id); if (!_foodRepository.Save()) { throw new Exception("Deleting a fooditem failed on save } [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 12 13 14 15 16 17 18 return NoContent(); 19 } 20 return NoContent(); [HttpDelete] 1 [Route("{id:int}", Name = nameof(RemoveFood))] 2 public ActionResult RemoveFood(int id) 3 { 4 FoodEntity foodItem = _foodRepository.GetSingle(id); 5 6 if (foodItem == null) 7 { 8 return NotFound(); 9 } 10 11 _foodRepository.Delete(id); 12 13 if (!_foodRepository.Save()) 14 { 15 throw new Exception("Deleting a fooditem failed on save 16 } 17 18 19 } 20
  86. Use DTOs

  87. Before going to the Demo ...

  88. Paging /api/foods?page=2&count=5

  89. Sorting /api/foods?orderby=id

  90. Datashaping /api/foods?fields=calories,nam

  91. Versioning is hard!

  92. Versioning document.docx

  93. Versioning document-final.docx

  94. Versioning document-final-final.docx

  95. Versioning document-really-final.docx

  96. Versioning document-really-final-end- of-discussion.docx

  97. Versioning document-really-final-approved-by- customer.docx

  98. Versioning document-really-final-approved-by- customer-with-alterations- suggested-by-his-assistant.docx

  99. Versioning document-really-final-approved-by- customer-with-alterations-suggested- by-his-assistant-and-the-janitor- cause-apparently-the-janitor-thinks- he-knows-all-about-good-api-design

  100. Versioning document-really-final-approved-by- customer-with-alterations-suggested- by-his-assistant-and-the-janitor-cause- apparently-the-janitor-thinks-he- knows-all-about-good-api-design.docx

  101. None
  102. Versioning /api/v1/foods/

  103. Versioning /api/v1/foods/ api-version: 1.0

  104. Versioning /api/v1/foods/ api-version: 1.0 api/foods?version=1.

  105. Documentation

  106. Documentation Swagger

  107. None
  108. Demo

  109. SignalR

  110. public class Startup { public Startup(IConfiguration configuration) { Configuration =

    configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { // ... services.AddSignalR(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env { // ... app.UseEndpoints(endpoints => { // ... endpoints.MapHub<CoolHub>("/coolHub"); }); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  111. Takeaways Takeaways

  112. Takeaways Takeaways Use DTOs

  113. Takeaways Takeaways Use DTOs Abstract HTTP Layer

  114. Takeaways Takeaways Use DTOs Abstract HTTP Layer Correct Statuscodes

  115. Takeaways Takeaways Use DTOs Abstract HTTP Layer Correct Statuscodes REST

    Constraints
  116. None
  117. T h e E n d

  118. Links Links https://aip.dev/ https://martinfowler.com/articles/richardsonMaturityModel.html https://www.youtube.com/watch?v=nSKp2StlS6s https://github.com/FabianGosebrink/ASPNETCore-WebAPI-Sample