Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Commands;
using Application.DataTransfer;
using Application.Exceptions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Northwind.API.Controllers
{
[Produces("application/json")]
[Route("api/Orders")]
public class OrdersController : Controller
{
private readonly ICreateOrderCommand _createOrder;
private readonly IDeleteOrderCommand _deleteOrders;
// GET: api/Orders
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET: api/Orders/5
[HttpGet("{id}", Name = "Get")]
public string Get(int id)
{
return "value";
}
// POST: api/Orders
[HttpPost]
public IActionResult Post([FromBody] CreateOrderDto dto)
{
try
{
_createOrder.Execute(dto);
return Ok();
}
catch (EntityNotFoundException e)
{
throw new EntityNotFoundException("nema");
}
catch (Exception)
{
return StatusCode(500);
}
}
// PUT: api/Orders/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/ApiWithActions/5
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
try
{
_deleteOrders.Execute(id);
return NoContent();
}
catch (EntityNotFoundException e)
{
return NotFound(e.Message);
}
catch
{
return StatusCode(500, "server error");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Commands;
using Commands;
using DataAccess.Domain;
using EfCommands;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Northwind.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services
to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<NorthwindContext>();
services.AddTransient<ICreateOrderCommand, EfCreateOrderCommand>();
services.AddTransient<IDeleteOrderCommand, EfDeleteOrderCommand>();
services.AddTransient<IEditProductCommand, EfEditProductCommand>();
}
// This method gets called by the runtime. Use this method to configure the
HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}