Skip to content

Lab #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Lab #55

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions CRUD application 2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -111,11 +114,6 @@
<HintPath>packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
<HintPath>packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
Expand Down Expand Up @@ -222,4 +220,4 @@
</Target>
<Target Name="AfterBuild">
</Target> -->
</Project>
</Project>
10 changes: 8 additions & 2 deletions CRUD application 2.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
90 changes: 77 additions & 13 deletions Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CRUD_application_2.Models;
using System;
using System.Linq;
using System.Web.Mvc;

Expand All @@ -7,36 +8,71 @@ namespace CRUD_application_2.Controllers
public class UserController : Controller
{
public static System.Collections.Generic.List<User> userlist = new System.Collections.Generic.List<User>();
// 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
Expand All @@ -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");
}
}
}
40 changes: 24 additions & 16 deletions Views/User/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@model IEnumerable<CRUD_application_2.Models.User>
@using (Html.BeginForm("Index", "User", FormMethod.Get))
{
<p>
Name: @Html.TextBox("searchString")
<input type="submit" value="Search" />
</p>
}
@model IEnumerable<CRUD_application_2.Models.User>

@{
ViewBag.Title = "Index";
Expand All @@ -20,20 +27,21 @@
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}

</table>
2 changes: 1 addition & 1 deletion Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
Expand Down
69 changes: 69 additions & 0 deletions deploy.json
Original file line number Diff line number Diff line change
@@ -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')]"
}
}
}
18 changes: 18 additions & 0 deletions deploy.parameters.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}