Skip to content

Implement CRUD operations in UserController #51

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 2 commits into
base: master
Choose a base branch
from
Open
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
92 changes: 71 additions & 21 deletions Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,116 @@
using CRUD_application_2.Models;
using System.Linq;
using System.Web.Mvc;

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()
{
// Implement the Index method here
return View(userlist);
}

// GET: User/Details/5
public ActionResult Details(int id)
{
// Implement the details method here
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 View();
}

// POST: User/Create
[HttpPost]
public ActionResult Create(User user)
{
// Implement the Create method (POST) here
try
{
userlist.Add(user);
return RedirectToAction("Index");
}
catch
{
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.
var user = userlist.FirstOrDefault(u => u.Id == id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}

// POST: User/Edit/5
[HttpPost]
public ActionResult Edit(int id, User user)
{
// This method is responsible for handling the HTTP POST request to update an existing user with the specified ID.
// It receives user input from the form submission and updates the corresponding user's information in the userlist.
// 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.
try
{
var userToUpdate = userlist.FirstOrDefault(u => u.Id == id);
if (userToUpdate == null)
{
return HttpNotFound();
}
// Assuming User model has properties like Name, Email, etc. Update them as needed.
userToUpdate.Name = user.Name;
userToUpdate.Email = user.Email;
// Update other fields as necessary

return RedirectToAction("Index");
}
catch
{
return View(user);
}
}

// GET: User/Delete/5
public ActionResult Delete(int id)
{
// Implement the Delete method here
var user = userlist.FirstOrDefault(u => u.Id == id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}

// POST: User/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
// Implement the Delete method (POST) here
try
{
var user = userlist.FirstOrDefault(u => u.Id == id);
if (user != null)
{
userlist.Remove(user);
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}


}
}
83 changes: 83 additions & 0 deletions UserControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using NUnit.Framework;
using Moq;
using System.Web.Mvc;
using CRUD_application_2.Controllers;
using CRUD_application_2.Models;
using System.Collections.Generic;
using System.Linq;

namespace CRUD_application_2.Tests.Controllers
{
[TestFixture]
public class UserControllerTests
{
private UserController controller;
private List<User> users;

[SetUp]
public void Setup()
{
// Initialize UserController and a test user list before each test
controller = new UserController();
users = new List<User>
{
new User { Id = 1, Name = "John Doe", Email = "[email protected]" },
new User { Id = 2, Name = "Jane Doe", Email = "[email protected]" }
};

// Simulate the static user list in UserController
UserController.userlist = users;
}

[Test]
public void Index_ReturnsViewWithUsers()
{
// Act
var result = controller.Index() as ViewResult;

// Assert
Assert.IsNotNull(result);
var model = result.Model as List<User>;
Assert.AreEqual(2, model.Count);
}

[Test]
public void Details_UserExists_ReturnsViewWithUser()
{
// Act
var result = controller.Details(1) as ViewResult;

// Assert
Assert.IsNotNull(result);
var user = result.Model as User;
Assert.AreEqual("John Doe", user.Name);
}

[Test]
public void Details_UserDoesNotExist_ReturnsHttpNotFound()
{
// Act
var result = controller.Details(999);

// Assert
Assert.IsInstanceOf<HttpNotFoundResult>(result);
}

[Test]
public void Create_PostAddsUserAndRedirects()
{
// Arrange
var newUser = new User { Id = 3, Name = "New User", Email = "[email protected]" };

// Act
var result = controller.Create(newUser) as RedirectToRouteResult;

// Assert
Assert.IsNotNull(result);
Assert.AreEqual("Index", result.RouteValues["action"]);
Assert.AreEqual(3, UserController.userlist.Count);
}

// Additional tests for Edit, Delete, etc. can be implemented similarly
}
}
68 changes: 68 additions & 0 deletions deploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"sqlServerName": {
"type": "string",
"metadata": {
"description": "Name of the SQL Server."
}
},
"sqlDatabaseName": {
"type": "string",
"metadata": {
"description": "Name of the SQL Database."
}
},
"sqlServerAdminLogin": {
"type": "string",
"metadata": {
"description": "Admin login for the SQL Server."
}
},
"sqlServerAdminPassword": {
"type": "securestring",
"metadata": {
"description": "Admin password for the SQL Server."
}
},
"jwtSecret": {
"type": "securestring",
"metadata": {
"description": "The JWT secret key for the application."
}
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2019-06-01-preview",
"name": "[parameters('sqlServerName')]",
"location": "[resourceGroup().location]",
"properties": {
"administratorLogin": "[parameters('sqlServerAdminLogin')]",
"administratorLoginPassword": "[parameters('sqlServerAdminPassword')]",
"version": "12.0"
},
"resources": [
{
"type": "databases",
"apiVersion": "2019-06-01-preview",
"name": "[parameters('sqlDatabaseName')]",
"location": "[resourceGroup().location]",
"properties": {
"collation": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "2147483648",
"sampleName": "AdventureWorksLT",
"zoneRedundant": false
},
"dependsOn": [
"[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
]
}
]
}
],
"outputs": {}
}
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/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"sqlServerName": {
"value": "yourSqlServerName"
},
"sqlDatabaseName": {
"value": "yourSqlDatabaseName"
},
"sqlServerAdminLogin": {
"value": "yourAdminLogin"
},
"sqlServerAdminPassword": {
"value": "yourAdminPassword"
}
}
}