Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Fabian Gosebrink

February 25, 2020
Tweet

More Decks by Fabian Gosebrink

Other Decks in Technology

Transcript

  1. Web

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

    3 dotnet new webapi dotnet new ... 1 2 3
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. Service Registration Method Controllers Views Pages AddControllers(); Y N N

    AddControllersWithViews(); Y Y N AddRazorPages(); N N Y
  12. { "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
  13. { "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
  14. 2xx

  15. 4xx

  16. 500

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

    1 https://github.com/angular/angular/blob/master/packages/common/http/src/xhr.ts#L180
  18. [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
  19. [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
  20. [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
  21. [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
  22. [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
  23. [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
  24. [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
  25. [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
  26. [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
  27. [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
  28. [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
  29. [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
  30. [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
  31. [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
  32. [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
  33. [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
  34. [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
  35. [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
  36. [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
  37. [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
  38. [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
  39. [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
  40. [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
  41. [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
  42. [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
  43. [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
  44. [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
  45. [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
  46. 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