Asp.Net Core 2.2 Web Api Client ile CRUD İşlemleri 2
Oluşturduğumuz ClientManager Controller da nasıl uygulayacağız görelim.
İlk önce ProductController düzenleyelim.
public class ProductController : Controller
İlk önce ProductController düzenleyelim.
public class ProductController : Controller
{
private readonly IProductClientManager _productClientManager;
private readonly ICategoryClientManager _categoryClientManager;
public ProductController(IProductClientManager productClientManager, ICategoryClientManager categoryClientManager)
{
_productClientManager = productClientManager;
_categoryClientManager = categoryClientManager;
_productClientManager.Url("https://localhost:44386/api/", "Product");
_categoryClientManager.Url("https://localhost:44386/api/", "Category");
}
// GET: Product
public IActionResult Index()
{
ICollection<Product> products = _productClientManager.GetAll();
return View(products);
}
// GET: Product/Create
public IActionResult Create()
{
EditProductViewModel productvm = new EditProductViewModel();
productvm.Categories= _categoryClientManager.GetAll();
return View(productvm);
}
// POST: Product/Create
[HttpPost]
public IActionResult Create(EditProductViewModel productvm)
{
if (ModelState.IsValid)
{
Product product = new Product()
{
Name=productvm.Name,
IsActive=productvm.IsActive,
Price=productvm.Price,
Stock=productvm.Stock,
CategoryId=productvm.CategoryId
};
_productClientManager.Add(product);
return RedirectToAction(nameof(Index));
}
return View(productvm);
}
// GET: Product/Edit/5
public async Task<ActionResult> Edit(int id)
{
var product = await _productClientManager.GetByIdAsync(id);
if (product==null)
{
return NotFound("Ürün bulunamadı.");
}
EditProductViewModel productvm = new EditProductViewModel()
{
Id=id,
Name=product.Name,
IsActive=product.IsActive,
Price=product.Price,
CategoryId=product.CategoryId,
Stock=product.Stock
};
productvm.Categories = await _categoryClientManager.GetAllAsync();
return View(productvm);
}
// POST: Product/Edit/5
[HttpPost]
public IActionResult Edit(int id, EditProductViewModel productvm)
{
if (ModelState.IsValid)
{
Product product = new Product()
{
Id=id,
Name = productvm.Name,
IsActive = productvm.IsActive,
Price = productvm.Price,
Stock = productvm.Stock,
CategoryId = productvm.CategoryId
};
_productClientManager.Update(id,product);
return RedirectToAction(nameof(Index));
}
return View(productvm);
}
// GET: Product/Delete/5
public IActionResult Delete(int id)
{
var product = _productClientManager.GetById(id);
if (product==null)
{
return NotFound("Ürün bulunamadı.");
}
return View(product);
}
// POST: Product/Delete/5
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_productClientManager.Delete(id);
return RedirectToAction(nameof(Index));
}
}
Şimdi CategoryController düzenleyelim.
public class CategoryController : Controller
{
private readonly ICategoryClientManager _categoryClientManager;
public CategoryController(ICategoryClientManager categoryClientManager)
{
_categoryClientManager = categoryClientManager;
_categoryClientManager.Url("https://localhost:44386/api/", "Category");
}
// GET: Category
public IActionResult Index()
{
ICollection<Category> category = _categoryClientManager.GetAll();
return View(category);
}
// GET: Category/Details/5
public IActionResult Details(int id)
{
var category = _categoryClientManager.GetById(id);
return View(category);
}
// GET: Category/Create
public IActionResult Create()
{
return View();
}
// POST: Category/Create
[HttpPost]
public IActionResult Create(EditCategoryViewModel category)
{
if (ModelState.IsValid)
{
Category dbcategory = new Category()
{
Id = category.Id,
CategoryName = category.CategoryName,
IsActive = category.IsActive
};
_categoryClientManager.Add(dbcategory);
RedirectToAction(nameof(Index));
}
return View(category);
}
// GET: Category/Edit/5
public async Task<ActionResult> Edit(int id)
{
var category = await _categoryClientManager.GetByIdAsync(id);
if (category == null)
{
return NotFound("Kategori bulunamadı.");
}
EditCategoryViewModel categoryvm = new EditCategoryViewModel()
{
Id = id,
IsActive = category.IsActive,
CategoryName = category.CategoryName,
Products = category.Products
};
return View(categoryvm);
}
// POST: Category/Edit/5
[HttpPost]
public IActionResult Edit(int id, EditCategoryViewModel category)
{
if (ModelState.IsValid)
{
Category dbcategory = new Category()
{
Id = category.Id,
CategoryName = category.CategoryName,
IsActive = category.IsActive
};
_categoryClientManager.Update(id, dbcategory);
return RedirectToAction(nameof(Index));
}
return View(category);
}
// GET: Category/Delete/5
public IActionResult Delete(int id)
{
var category = _categoryClientManager.GetById(id);
if (category == null)
{
return NotFound("Ürün bulunamadı.");
}
return View(category);
}
// POST: Category/Delete/5
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_categoryClientManager.Delete(id);
return RedirectToAction(nameof(Index));
}
}
yazının devamını Asp.Net Core 2.2 Web Api Client ile CRUD İşlemleri 3 sayfasından bakabilirsiniz.
Yorumlar
Yorum Gönder