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. Creating Modern
    Creating Modern
    WebAPIs With
    WebAPIs With
    ASP.NET Core 3
    ASP.NET Core 3

    View Slide

  2. Web

    View Slide

  3. Web
    Desktop

    View Slide

  4. Mobile
    Desktop
    Web

    View Slide

  5. Mobile
    Desktop
    Web
    ...

    View Slide

  6. View Slide

  7. WebAPI
    Mobile
    Desktop
    Web
    ...

    View Slide

  8. Tooling

    View Slide

  9. Tooling
    DOTNET
    CLI

    View Slide

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

    View Slide

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

    View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  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();
    12
    });
    13
    14

    View Slide

  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();
    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();
    12
    });
    13
    }
    14

    View Slide

  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();
    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();
    12
    });
    13
    }
    14
    public static IHostBuilder CreateHostBuilder(string[] args
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
    webBuilder.UseStartup();
    });
    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

    View Slide

  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();
    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();
    12
    });
    13
    }
    14
    public static IHostBuilder CreateHostBuilder(string[] args
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
    webBuilder.UseStartup();
    });
    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();
    });
    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

    View Slide

  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();
    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();
    12
    });
    13
    }
    14
    public static IHostBuilder CreateHostBuilder(string[] args
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
    webBuilder.UseStartup();
    });
    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();
    });
    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();
    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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  23. JSON
    Serialization

    View Slide

  24. JSON Serialization

    View Slide

  25. JSON Serialization
    Newtonsoft.Jso
    n

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  29. JSON Serialization

    View Slide

  30. Service Registration

    View Slide

  31. Service Registration

    View Slide

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

    View Slide

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

    View Slide

  34. Routing

    View Slide

  35. Startup

    View Slide

  36. REST

    View Slide

  37. REST
    What is
    ?

    View Slide

  38. REST
    is not
    WebAPI

    View Slide

  39. REST
    is not
    HTTP

    View Slide

  40. Architectural
    Style

    View Slide

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

    View Slide

  42. REST
    Constraints

    View Slide

  43. REST Constraints

    View Slide

  44. REST Constraints
    Client / Server
    Cacheable
    Stateless
    Layered System
    Uniform Interface
    Code on Demand

    View Slide

  45. Richardson
    Maturity
    Model

    View Slide

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

    View Slide

  47. HATEOAS

    View Slide

  48. Hypermedia
    As
    The
    Engine
    Of
    Application
    State

    View Slide

  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

    View Slide

  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

    View Slide

  51. Statuscodes

    View Slide

  52. View Slide

  53. 2xx

    View Slide

  54. 4xx

    View Slide

  55. 500

    View Slide

  56. let ok = status >= 200 && status < 30
    1
    https://github.com/angular/angular/blob/master/packages/common/http/src/xhr.ts#L180

    View Slide

  57. The
    Controller

    View Slide

  58. [HttpGet(Name = nameof(GetAllFoods))]
    public ActionResult GetAllFoods()
    {
    List foodItems = _foodRepository.GetAll().ToLi
    return Ok(_mapper.Map(foodItems));
    }
    1
    2
    3
    4
    5
    6
    7

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  65. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22

    View Slide

  66. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22

    View Slide

  67. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22
    if (foodCreateDto == null)
    {
    return BadRequest();
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    4
    5
    6
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22

    View Slide

  68. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22
    if (foodCreateDto == null)
    {
    return BadRequest();
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    4
    5
    6
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    FoodEntity toAdd = _mapper.Map(foodCreateDto);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult 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(newFoodItem));
    21
    }
    22

    View Slide

  69. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22
    if (foodCreateDto == null)
    {
    return BadRequest();
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    4
    5
    6
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    FoodEntity toAdd = _mapper.Map(foodCreateDto);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult 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(newFoodItem));
    21
    }
    22
    _foodRepository.Add(toAdd);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22

    View Slide

  70. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22
    if (foodCreateDto == null)
    {
    return BadRequest();
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    4
    5
    6
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    FoodEntity toAdd = _mapper.Map(foodCreateDto);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult 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(newFoodItem));
    21
    }
    22
    _foodRepository.Add(toAdd);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    if (!_foodRepository.Save())
    {
    throw new Exception("Creating a fooditem failed on save.");
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22

    View Slide

  71. [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    public ActionResult 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(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(newFoodItem));
    21
    }
    22
    if (foodCreateDto == null)
    {
    return BadRequest();
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    4
    5
    6
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    FoodEntity toAdd = _mapper.Map(foodCreateDto);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult 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(newFoodItem));
    21
    }
    22
    _foodRepository.Add(toAdd);
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    if (!_foodRepository.Save())
    {
    throw new Exception("Creating a fooditem failed on save.");
    }
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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(newFoodItem));
    21
    }
    22
    return CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id },
    _mapper.Map(newFoodItem));
    [HttpPost(Name = nameof(AddFood))]
    1
    public ActionResult AddFood([FromBody] FoodCreateDto foodCreateDto)
    2
    {
    3
    if (foodCreateDto == null)
    4
    {
    5
    return BadRequest();
    6
    }
    7
    8
    FoodEntity toAdd = _mapper.Map(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

    View Slide

  72. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  73. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  74. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  75. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    if (foodUpdateDto == null)
    {
    return BadRequest();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  76. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    if (foodUpdateDto == null)
    {
    return BadRequest();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    var existingFoodItem = _foodRepository.GetSingle(id);
    if (existingFoodItem == null)
    {
    return NotFound();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  77. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    if (foodUpdateDto == null)
    {
    return BadRequest();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    var existingFoodItem = _foodRepository.GetSingle(id);
    if (existingFoodItem == null)
    {
    return NotFound();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    _mapper.Map(foodUpdateDto, existingFoodItem);
    _foodRepository.Update(id, existingFoodItem);
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27

    View Slide

  78. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    if (foodUpdateDto == null)
    {
    return BadRequest();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    var existingFoodItem = _foodRepository.GetSingle(id);
    if (existingFoodItem == null)
    {
    return NotFound();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    _mapper.Map(foodUpdateDto, existingFoodItem);
    _foodRepository.Update(id, existingFoodItem);
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(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 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(existingFoodItem));
    26
    }
    27

    View Slide

  79. [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    [Route("{id:int}", Name = nameof(UpdateFood))]
    [HttpPut]
    1
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    if (foodUpdateDto == null)
    {
    return BadRequest();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    var existingFoodItem = _foodRepository.GetSingle(id);
    if (existingFoodItem == null)
    {
    return NotFound();
    }
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(existingFoodItem));
    26
    }
    27
    _mapper.Map(foodUpdateDto, existingFoodItem);
    _foodRepository.Update(id, existingFoodItem);
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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(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 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(existingFoodItem));
    26
    }
    27
    return Ok(_mapper.Map(existingFoodItem));
    [HttpPut]
    1
    [Route("{id:int}", Name = nameof(UpdateFood))]
    2
    public ActionResult 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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  86. Use
    DTOs

    View Slide

  87. Before going
    to the
    Demo
    ...

    View Slide

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

    View Slide

  89. Sorting
    /api/foods?orderby=id

    View Slide

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

    View Slide

  91. Versioning
    is hard!

    View Slide

  92. Versioning
    document.docx

    View Slide

  93. Versioning
    document-final.docx

    View Slide

  94. Versioning
    document-final-final.docx

    View Slide

  95. Versioning
    document-really-final.docx

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  101. View Slide

  102. Versioning
    /api/v1/foods/

    View Slide

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

    View Slide

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

    View Slide

  105. Documentation

    View Slide

  106. Documentation
    Swagger

    View Slide

  107. View Slide

  108. Demo

    View Slide

  109. SignalR

    View Slide

  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");
    });
    }
    }
    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

    View Slide

  111. Takeaways
    Takeaways

    View Slide

  112. Takeaways
    Takeaways
    Use DTOs

    View Slide

  113. Takeaways
    Takeaways
    Use DTOs
    Abstract HTTP Layer

    View Slide

  114. Takeaways
    Takeaways
    Use DTOs
    Abstract HTTP Layer
    Correct Statuscodes

    View Slide

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

    View Slide

  116. View Slide

  117. T h e E n d

    View Slide

  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

    View Slide