1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/demos/belgrade-product-catalog-demo/Controllers/ProductCatalogController.cs
Jovan Popovic 61c69c1c3c Added new samples
BCP, string_agg sample, and comparisng with EF.
2017-03-05 20:38:57 +01:00

68 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Mvc;
using ProductCatalog.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ProductCatalog.Controllers
{
public class ProductCatalogController : Controller
{
private ProductCatalogContext _context;
public ProductCatalogController (ProductCatalogContext context)
{
_context = context;
}
[HttpGet]
public IActionResult Index()
{
ViewData["page"] = "index";
return View(_context.Products.AsEnumerable());
}
// POST api/ProductCatalog/Add
public IActionResult Add(Product p)
{
try
{
_context.Products.Add(p);
_context.SaveChanges();
return Redirect("/ProductCatalog/Index");
} catch (Exception)
{
return Redirect("/ProductCatalog/Index");
}
}
public IActionResult Report1()
{
ViewData["page"] = "report1";
return View();
}
public IActionResult Report2()
{
ViewData["page"] = "report2";
return View();
}
[HttpGet(Name = "GetAll")]
public IEnumerable<Product> GetAll()
{
return _context.Products.AsEnumerable<Product>();
}
[HttpGet("GetById")]
public IActionResult GetById(int id)
{
var product = _context.Products.FirstOrDefault(p=>p.ProductId == id);
if (product == null)
{
return NotFound();
}
return new ObjectResult(product);
}
}
}