Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav2404 committed Nov 26, 2024
0 parents commit 9f42a68
Show file tree
Hide file tree
Showing 124 changed files with 78,054 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
74 changes: 74 additions & 0 deletions Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using demoMvcCore.Models;
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 CategoriesController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
var categories = CategoryRepository.GetCategories();
return View(categories);

}

[HttpGet]
public IActionResult Edit([FromRoute]int? id)
{

ViewBag.OnClick = "edit";
var category = CategoryRepository.GetCategoryById(id ?? 0);
return View(category);


}

[HttpPost]
public IActionResult Edit(Category category)
{
if (ModelState.IsValid)
{
CategoryRepository.UpdateCategory(category.CategoryId, category);
return RedirectToAction(nameof(Index));
}
return View(category);
}


public IActionResult Add()
{
//ViewData["OnClick"] = "add";
ViewBag.OnClick = "add";
return View();
}

[HttpPost]
public IActionResult Add(Category category)
{
if(ModelState.IsValid)
{
CategoryRepository.AddCategory(category);
return RedirectToAction(nameof(Index));

}

return View(category);
}

[HttpGet]
public IActionResult Delete(int categoryId)
{
CategoryRepository.DeleteCategory(categoryId);
return RedirectToAction(nameof(Index));
}

}
}

32 changes: 32 additions & 0 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using demoMvcCore.Models;

namespace demoMvcCore.Controllers;

public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}

18 changes: 18 additions & 0 deletions Models/Category.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace demoMvcCore.Models
{


public class Category
{
public int CategoryId { get; set; }
[Required]
public string Name { get; set; } = string.Empty;

public string? Description { get; set; } = string.Empty;

}
}

48 changes: 48 additions & 0 deletions Models/CategoryRepositoruy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
namespace demoMvcCore.Models
{
public class CategoryRepository
{
public CategoryRepository()
{ }
private static List<Category> _categories = new List<Category>()
{ new Category {CategoryId = 1,Name="Beverage",Description="Beverage"},
new Category {CategoryId = 2,Name="Snacks",Description="Snacks" } };

public static List<Category> GetCategories() => _categories;

public static void AddCategory(Category category)
{
var maxId = _categories.Max(x => x.CategoryId);
category.CategoryId = maxId + 1;
_categories.Add(category);
}
public static Category? GetCategoryById(int categoryId) =>
_categories.FirstOrDefault(x => x.CategoryId == categoryId);



public static void UpdateCategory(int id ,Category category)
{
var Category = _categories.FirstOrDefault(x => x.CategoryId==id);
if(Category is not null)
{
Category.Name = category.Name;
Category.Description = category.Description;
}
}

public static void DeleteCategory(int id)
{
var category = GetCategoryById(id);
if (category is not null)
_categories.Remove(category);

}



}
}


9 changes: 9 additions & 0 deletions Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace demoMvcCore.Models;

public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}

28 changes: 28 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

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

app.Run();

38 changes: 38 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51613",
"sslPort": 44388
}
},
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5285",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:7030;http://localhost:5285",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Binary file added Views/.DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions Views/Categories/Add.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@using demoMvcCore.Models
@model Category
@section title
{
<h3>Add Category</h3>

}

<partial name="_Categories" model="@Model" />
20 changes: 20 additions & 0 deletions Views/Categories/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@using demoMvcCore.Models
@model Category
@section title
{
@if (Model is not null)
{
<h3>Category: @Model.Name </h3>
<p> Description: @Model.Description</p>
}
}

@if (Model is not null)
{
<partial name="_Categories" model="@Model" />
}





44 changes: 44 additions & 0 deletions Views/Categories/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

@section title
{
<h3>Categories</h3>
}

@model List<Category>
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
@if (Model is not null && Model.Count() > 0)
{
<table class="table table-striped">
<thead>
<tr>
<th> Name</th>
<th> Description </th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var category in Model)
{
<tr>
<td>@category.Name</td>
<td>@category.Description</td>
<td><a class="btn btn-link" asp-controller="categories" asp-action="edit" asp-route-id="@category.CategoryId">Edit</a></td>
<td><a class="btn btn-link" asp-controller="categories" asp-action="delete" asp-route-categoryId="@category.CategoryId" onclick="return confirmDelete()">Delete</a></td>
</tr>
}
</tbody>
</table>

<br/>

<a class="btn btn-primary" asp-controller="categories" asp-action="add">Add</a>
}
@section Scripts
{
<script>
function confirmDelete() {
return confirm("U sure you want to confirm?");
}
</script>
}
52 changes: 52 additions & 0 deletions Views/Categories/_Categories.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@model Category

@{
string onclick = ViewBag.OnClick ?? string.Empty;
}


<form method="post" asp-controller="categories" asp-action="@onclick">

@if (onclick?.ToLower() == "edit")
{
<input type="hidden" asp-for="@Model.CategoryId" />
}
<div class="tet-danger" asp-validation-summary="ModelOnly"></div>
<div class="row mb-3">
<div class="col-2">
<label asp-for="Name" class="col-form-label"></label>
</div>
<div class="">
<input type="text" asp-for="Name" class="form-control" />
</div>
<div class="col">
<span class="text-danger" asp-validation-for="Name"></span>

</div>

</div>
<div class="row mb-3">
<div class="col-2">
<label asp-for="Description" class="col-form-label"></label>
</div>
<div class="">
<input type="text" asp-for="Description" class="form-control" />
</div>
<div class="col">
<span class="text-danger" asp-validation-for="Description"></span>

</div>

</div>

<div class="row mb-3">
<div class="col-2">
<input type="submit" class="btn btn-primary" value="Save"></input>
</div>
<div class="">

</div>

</div>

</form>
Loading

0 comments on commit 9f42a68

Please sign in to comment.