Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created October 7, 2018 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icebeam7/f229d2ea534e138c4f200bd19c8fd290 to your computer and use it in GitHub Desktop.
Save icebeam7/f229d2ea534e138c4f200bd19c8fd290 to your computer and use it in GitHub Desktop.
StoreWebApi: OrderDetailsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using StoreWebApi.Models;
using AutoMapper;
using StoreWebApi.DTOs;
namespace StoreWebApi.Controllers
{
[Produces("application/json")]
[Route("api/OrderDetails")]
public class OrderDetailsController : Controller
{
private readonly StoreDBContext _context;
public OrderDetailsController(StoreDBContext context)
{
_context = context;
}
// GET: api/OrderDetails
[HttpGet]
public IEnumerable<OrderDetail> GetOrderDetail()
{
return _context.OrderDetail;
}
// GET: api/OrderDetails/Order/5
[HttpGet("Order/{orderId}")]
public async Task<IActionResult> GetOrder_OrderDetail([FromRoute] int orderId)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var orderDetail = _context.OrderDetail.Where(m => m.CustomerOrderId == orderId);
if (orderDetail == null)
{
return NotFound();
}
return Ok(Mapper.Map<IEnumerable<OrderDetailDTO>>(orderDetail));
}
// POST: api/OrderDetails
[HttpPost]
public async Task<IActionResult> PostOrderDetail([FromBody] List<OrderDetailDTO> orderDetail)
{
foreach (var item in orderDetail)
{
item.CustomerOrder = null;
item.Product = null;
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var od = Mapper.Map<IEnumerable<OrderDetail>>(orderDetail);
_context.OrderDetail.AddRange(od);
await _context.SaveChangesAsync();
return CreatedAtAction("GetOrderDetail", new { id = od.First().CustomerOrderId }, orderDetail);
}
// DELETE: api/OrderDetails/5
[HttpDelete("{orderId}")]
public async Task<IActionResult> DeleteOrderDetail([FromRoute] int orderId)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var orderDetail = _context.OrderDetail.Where(m => m.CustomerOrderId == orderId);
if (orderDetail == null)
{
return NotFound();
}
_context.OrderDetail.RemoveRange(orderDetail);
await _context.SaveChangesAsync();
return Ok(orderDetail);
}
private bool OrderDetailExists(int id)
{
return _context.OrderDetail.Any(e => e.CustomerOrderId == id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment