diff --git a/CRUD application 2.csproj b/CRUD application 2.csproj index 553546be..53b84bec 100644 --- a/CRUD application 2.csproj +++ b/CRUD application 2.csproj @@ -45,6 +45,9 @@ 4 + + packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + @@ -111,11 +114,6 @@ packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll - - - packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll - - @@ -222,4 +220,4 @@ --> - + \ No newline at end of file diff --git a/CRUD application 2.sln b/CRUD application 2.sln index 98ddfc3a..2ea220c7 100644 --- a/CRUD application 2.sln +++ b/CRUD application 2.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.33801.447 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35013.160 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CRUD application 2", "CRUD application 2.csproj", "{D44C1C21-0289-4DD0-B139-6825EE5C13B2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContactDatabase.nUnitTest", "..\ContactDatabase.nUnitTest\ContactDatabase.nUnitTest.csproj", "{643EE887-9C0E-491A-B340-5615117FAE0C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {D44C1C21-0289-4DD0-B139-6825EE5C13B2}.Debug|Any CPU.Build.0 = Debug|Any CPU {D44C1C21-0289-4DD0-B139-6825EE5C13B2}.Release|Any CPU.ActiveCfg = Release|Any CPU {D44C1C21-0289-4DD0-B139-6825EE5C13B2}.Release|Any CPU.Build.0 = Release|Any CPU + {643EE887-9C0E-491A-B340-5615117FAE0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {643EE887-9C0E-491A-B340-5615117FAE0C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {643EE887-9C0E-491A-B340-5615117FAE0C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {643EE887-9C0E-491A-B340-5615117FAE0C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index f9ecbda2..bc77e605 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -1,4 +1,5 @@ using CRUD_application_2.Models; +using System; using System.Linq; using System.Web.Mvc; @@ -7,36 +8,71 @@ namespace CRUD_application_2.Controllers public class UserController : Controller { public static System.Collections.Generic.List userlist = new System.Collections.Generic.List(); - // GET: User - public ActionResult Index() + + public ActionResult Index(string searchString) { - // Implement the Index method here + var users = from u in userlist + select u; + + if (!String.IsNullOrEmpty(searchString)) + { + users = users.Where(s => s.Name.Contains(searchString)); + } + + return View(users.ToList()); } + + // GET: User/Details/5 public ActionResult Details(int id) { - // Implement the details method here + // Find the user by ID + var user = userlist.FirstOrDefault(u => u.Id == id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - + // GET: User/Create public ActionResult Create() { - //Implement the Create method here + // Return the Create view + return View(); } - + // POST: User/Create [HttpPost] public ActionResult Create(User user) { - // Implement the Create method (POST) here + try + { + // Add the new user to the list + userlist.Add(user); + return RedirectToAction("Index"); + } + catch + { + // If an error occurs, return the Create view again + return View(); + } } - + // GET: User/Edit/5 public ActionResult Edit(int id) { // This method is responsible for displaying the view to edit an existing user with the specified ID. // It retrieves the user from the userlist based on the provided ID and passes it to the Edit view. + // If no user is found with the provided ID, it returns a HttpNotFoundResult. + var user = userlist.FirstOrDefault(u => u.Id == id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } // POST: User/Edit/5 @@ -48,19 +84,47 @@ public ActionResult Edit(int id, User user) // If successful, it redirects to the Index action to display the updated list of users. // If no user is found with the provided ID, it returns a HttpNotFoundResult. // If an error occurs during the process, it returns the Edit view to display any validation errors. + var existingUser = userlist.FirstOrDefault(u => u.Id == id); + if (existingUser != null) + { + existingUser.Name = user.Name; + existingUser.Email = user.Email; + // Update other properties as needed + return RedirectToAction("Index"); + } + else + { + return HttpNotFound(); + } } - + // GET: User/Delete/5 public ActionResult Delete(int id) { - // Implement the Delete method here + // Find the user by ID + var userToDelete = userlist.FirstOrDefault(u => u.Id == id); + if (userToDelete == null) + { + return HttpNotFound(); + } + // Pass the user to the Delete view for confirmation + return View(userToDelete); } - + // POST: User/Delete/5 [HttpPost] public ActionResult Delete(int id, FormCollection collection) { - // Implement the Delete method (POST) here + // Find the user by ID + var userToDelete = userlist.FirstOrDefault(u => u.Id == id); + if (userToDelete == null) + { + return HttpNotFound(); + } + // Remove the user from the list + userlist.Remove(userToDelete); + // Redirect to the Index action to display the updated list of users + return RedirectToAction("Index"); } } } diff --git a/Views/User/Index.cshtml b/Views/User/Index.cshtml index 543196b1..3dd88d0a 100644 --- a/Views/User/Index.cshtml +++ b/Views/User/Index.cshtml @@ -1,4 +1,11 @@ -@model IEnumerable +@using (Html.BeginForm("Index", "User", FormMethod.Get)) +{ +

+ Name: @Html.TextBox("searchString") + +

+} +@model IEnumerable @{ ViewBag.Title = "Index"; @@ -20,20 +27,21 @@ -@foreach (var item in Model) { - - - @Html.DisplayFor(modelItem => item.Name) - - - @Html.DisplayFor(modelItem => item.Email) - - - @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | - @Html.ActionLink("Details", "Details", new { id=item.Id }) | - @Html.ActionLink("Delete", "Delete", new { id=item.Id }) - - -} + @foreach (var item in Model) + { + + + @Html.DisplayFor(modelItem => item.Name) + + + @Html.DisplayFor(modelItem => item.Email) + + + @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | + @Html.ActionLink("Details", "Details", new { id = item.Id }) | + @Html.ActionLink("Delete", "Delete", new { id = item.Id }) + + + } diff --git a/Web.config b/Web.config index d246aab1..8ce4f449 100644 --- a/Web.config +++ b/Web.config @@ -30,7 +30,7 @@ - + diff --git a/deploy.json b/deploy.json new file mode 100644 index 00000000..503fccc1 --- /dev/null +++ b/deploy.json @@ -0,0 +1,69 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + + "resourceGroupName": { + "type": "string", + "metadata": { + "description": "The name of the resource group." + } + }, + "subscriptionId": { + "type": "string", + "metadata": { + "description": "The subscription ID where resources will be deployed." + } + }, + "webAppName": { + "type": "string", + "metadata": { + "description": "Contact Database" + } + }, + "location": { + "type": "string", + "defaultValue": "[resourceGroup().location]", + "metadata": { + "description": "Location for all resources." + } + } + }, + "variables": { + "appServicePlanName": "[concat(parameters('webAppName'), '-asp')]" + }, + "resources": [ + { + "type": "Microsoft.Web/serverfarms", + "apiVersion": "2020-06-01", + "name": "[variables('appServicePlanName')]", + "location": "[parameters('location')]", + "sku": { + "name": "F1", + "tier": "Free" + }, + "properties": { + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "type": "Microsoft.Web/sites", + "apiVersion": "2020-06-01", + "name": "[parameters('webAppName')]", + "location": "[parameters('location')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" + ], + "properties": { + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" + } + } + ], + "outputs": { + "webAppUrl": { + "type": "string", + "value": "[concat('https://', parameters('webAppName'), '.azurewebsites.net')]" + } + } +} \ No newline at end of file diff --git a/deploy.parameters.json b/deploy.parameters.json new file mode 100644 index 00000000..2970e694 --- /dev/null +++ b/deploy.parameters.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "resourceGroupName": { + "value": "GitHub-Copilot-Challenges" + }, + "subscriptionId": { + "value": "d9d175f7-9888-472e-9013-2ede6f9029c0" + }, + "webAppName": { + "value": "MyContactDatabaseApp" + }, + "location": { + "value": "East US" + } + } +} \ No newline at end of file