Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav2404 committed Nov 30, 2024
1 parent 0419155 commit 475857d
Show file tree
Hide file tree
Showing 14 changed files with 462 additions and 44 deletions.
13 changes: 5 additions & 8 deletions demoMvcCore/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using demoMvcCore.Models;
using demoMvcCore.Models;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
Expand Down Expand Up @@ -38,13 +34,14 @@ public IActionResult Edit(Category category)
CategoryRepository.UpdateCategory(category.CategoryId, category);
return RedirectToAction(nameof(Index));
}
ViewBag.OnClick = "edit";
return View(category);
}


public IActionResult Add()
{
//ViewData["OnClick"] = "add";

ViewBag.OnClick = "add";
return View();
}
Expand All @@ -58,11 +55,11 @@ public IActionResult Add(Category category)
return RedirectToAction(nameof(Index));

}

ViewBag.OnClick = "add";
return View(category);
}

[HttpGet]

public IActionResult Delete(int categoryId)
{
CategoryRepository.DeleteCategory(categoryId);
Expand Down
93 changes: 93 additions & 0 deletions demoMvcCore/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using demoMvcCore.Models;
using demoMvcCore.ViewModels;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace demoMvcCore.Controllers
{
public class ProductsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
var products = ProductsRepository.GetProducts(loadCategory:true);
return View(products);

}

public IActionResult Add()
{
ViewBag.Action = "add";
var productViewModels = new ProductsViewModel
{
Categories = CategoryRepository.GetCategories(),
//Product
};
return View(productViewModels);


}

[HttpPost]
public IActionResult Add(ProductsViewModel productsViewModel)
{
if (ModelState.IsValid)
{
ProductsRepository.AddProduct(productsViewModel.Product);
return RedirectToAction(nameof(Index));
}
ViewBag.Action = "edit";
productsViewModel.Categories = CategoryRepository.GetCategories();
return View(productsViewModel);
}




public IActionResult Edit(int id)
{
ViewBag.Action = "edit";
var productView = new ProductsViewModel
{
Product = ProductsRepository.GetProductById(id) ?? new Product(),
Categories = CategoryRepository.GetCategories()
};
return View(productView);
}

[HttpPost]

public IActionResult Edit(ProductsViewModel productsViewModel)
{
if (ModelState.IsValid)
{
ProductsRepository.UpdateProduct(productsViewModel.Product.ProductId, productsViewModel.Product);

return RedirectToAction(nameof(Index));
}
ViewBag.Action = "edit";
productsViewModel.Categories = CategoryRepository.GetCategories();
return View(productsViewModel);
}


public IActionResult Delete(int id)
{
ProductsRepository.DeleteProduct(id);
return RedirectToAction(nameof(Index));
}





}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public CategoryRepository()

public static void AddCategory(Category category)
{
var maxId = _categories.Max(x => x.CategoryId);
category.CategoryId = maxId + 1;
_categories.Add(category);
}
if (_categories is not null && _categories.Count > 0)
{
var maxId = _categories.Max(x => x.CategoryId);
category.CategoryId = maxId + 1;

}
else
{
var maxId = 1;
category.CategoryId = maxId;
}
_categories ??= new List<Category>();
_categories.Add(category);
}
public static Category? GetCategoryById(int categoryId) =>
_categories.FirstOrDefault(x => x.CategoryId == categoryId);

Expand Down
28 changes: 28 additions & 0 deletions demoMvcCore/Models/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel.DataAnnotations;

namespace demoMvcCore.Models
{

public class Product
{
public int ProductId { get; set; }

[Required]
[Display(Name = "Category")]
public int? CategoryId { get; set; }

[Required]
public string Name { get; set; } = string.Empty;

[Required]
public int? Quantity { get; set; }

[Required]
[Range(0, int.MaxValue)]
public double? Price { get; set; }
public Category? Category { get; set; }


}
}

100 changes: 100 additions & 0 deletions demoMvcCore/Models/ProductRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
namespace demoMvcCore.Models
{
public class ProductsRepository
{
private static List<Product> _products = new List<Product>()
{
new Product { ProductId = 1, CategoryId = 1, Name = "Iced Tea", Quantity = 100, Price = 1.99 },
new Product { ProductId = 2, CategoryId = 1, Name = "Canada Dry", Quantity = 200, Price = 1.99 },
new Product { ProductId = 3, CategoryId = 2, Name = "Whole Wheat Bread", Quantity = 300, Price = 1.50 },
new Product { ProductId = 4, CategoryId = 2, Name = "White Bread", Quantity = 300, Price = 1.50 }
};

public static void AddProduct(Product product)
{
if (_products is not null && _products.Count() > 0)
{
var maxId = _products.Max(x => x.ProductId);
product.ProductId = maxId + 1;

}
else
{
var maxId = 1;
product.ProductId = maxId;
}
_products ??= new List<Product>();
_products.Add(product);
}

public static List<Product> GetProducts(bool loadCategory = false)
{
if(!loadCategory)
{
return _products;
}
else
{
if(_products !=null && _products.Count > 0 )
{
_products.ForEach(x =>
{
if(x.CategoryId.HasValue)
x.Category = CategoryRepository.GetCategoryById(x.CategoryId.Value);
});
}
}
return _products;
}

public static Product? GetProductById(int productId,bool loadCategory=false)
{
var product = _products.FirstOrDefault(x => x.ProductId == productId);
if (product != null)
{
var prod = new Product
{
ProductId = product.ProductId,
Name = product.Name,
Quantity = product.Quantity,
Price = product.Price,
CategoryId = product.CategoryId
};

if(loadCategory && product.CategoryId.HasValue)
{
prod.Category = CategoryRepository.GetCategoryById(prod.CategoryId.Value);
}

return prod;
}

return null;
}

public static void UpdateProduct(int productId, Product product)
{
if (productId != product.ProductId) return;

var productToUpdate = _products.FirstOrDefault(x => x.ProductId == productId);
if (productToUpdate != null)
{
productToUpdate.Name = product.Name;
productToUpdate.Quantity = product.Quantity;
productToUpdate.Price = product.Price;
productToUpdate.CategoryId = product.CategoryId;
}
}

public static void DeleteProduct(int productId)
{
var product = _products.FirstOrDefault(x => x.ProductId == productId);
if (product != null)
{
_products.Remove(product);
}
}
}
}


10 changes: 8 additions & 2 deletions demoMvcCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddCors(options =>
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins("*").WithMethods("GET", "POST").WithHeaders("Content-Type");
}));
var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -16,13 +21,14 @@
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseCors();
app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
;

app.Run();

18 changes: 18 additions & 0 deletions demoMvcCore/ViewModels/ProductsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

using demoMvcCore.Models;

namespace demoMvcCore.ViewModels
{
public class ProductsViewModel
{
public ProductsViewModel()
{


}

public IEnumerable<Category> Categories { get; set; } = new List<Category>();
public Product Product { get; set; } = new();
}
}

Loading

0 comments on commit 475857d

Please sign in to comment.