diff --git a/Controllers/AccountController.cs b/Controllers/AccountController.cs index 15e1bbe..256cdd5 100644 --- a/Controllers/AccountController.cs +++ b/Controllers/AccountController.cs @@ -55,30 +55,44 @@ namespace WebApplication1.Controllers if (ModelState.IsValid) { + // Replace this with actual user creation logic, including password hashing // and storing user information in a database /*var allusers = db.users.ToList(); - - foreach (var nuser in allusers) + + foreach (var user in allusers) + { - if (model.username == nuser.ToString()) + if (model.username.ToString() == user.usename.ToString()) { ModelState.AddModelError("", "username already exists"); - break; + return RedirectToAction("Register"); } }*/ - /*db.users.Add(new user - { - usename = model.username, - password = model.password, - displayname = model.displayname - }); - db.SaveChanges(); - }*/ - - return RedirectToAction("Login"); + db.users.Add( new user + { + usename = model.username, + password = model.password, + displayname = model.displayname + }); + db.SaveChanges(); + return RedirectToAction("Home","Index"); + } + + + /*db.users.Add(new user + { + usename = model.username, + password = model.password, + displayname = model.displayname + }); + db.SaveChanges(); + }*/ + + // return RedirectToAction("Login"); + return View(model); } } diff --git a/Controllers/TheNewsController.cs b/Controllers/TheNewsController.cs deleted file mode 100644 index c4df8ae..0000000 --- a/Controllers/TheNewsController.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; -using WebApplication1.Models; - -namespace WebApplication1.Controllers -{ - public class TheNewsController : Controller - { - // GET: TheNews - public ActionResult submit() - { - return View(); - } - } -} \ No newline at end of file diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs new file mode 100644 index 0000000..1b449ef --- /dev/null +++ b/Controllers/usersController.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Entity; +using System.Linq; +using System.Net; +using System.Web; +using System.Web.Mvc; +using WebApplication1.Models; + +namespace WebApplication1.Controllers +{ + public class usersController : Controller + { + private newswebappEntities db = new newswebappEntities(); + + // GET: users + public ActionResult Index() + { + return View(db.users.ToList()); + } + + // GET: users/Details/5 + public ActionResult Details(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // GET: users/Create + public ActionResult Register() + { + return View(); + } + + // POST: users/Create + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Register([Bind(Include = "ID,usename,password,displayname")] user user) + { + if (ModelState.IsValid) + { + var check = db.users.FirstOrDefault(u => u.usename == user.usename); + if (user.usename != null && user.password != null) + { + if (check != null) + { + ModelState.AddModelError("", " username already exists"); + } + else + { + db.users.Add(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } + } + else { + ModelState.AddModelError(""," username and password field can not be empty"); + } + } + + return View(user); + } + + // GET: users/Login + public ActionResult Login() + { + return View(); + } + + // POST: users/Login + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Login([Bind(Include = "usename,password")] user user) + { + if (ModelState.IsValid) + { + var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); + if (existingUser != null && existingUser.password == user.password) + { + return RedirectToAction("Index"); + } + else + { + ModelState.AddModelError("", "Invalid username or password"); + } + } + + return View(user); + } + + // GET: users/Edit/5 + public ActionResult Edit(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: users/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to, for + // more details see https://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit([Bind(Include = "ID,usename,password,displayname")] user user) + { + if (ModelState.IsValid) + { + db.Entry(user).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(user); + } + + + + // GET: users/Delete/5 + public ActionResult Delete(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: users/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public ActionResult DeleteConfirmed(int id) + { + user user = db.users.Find(id); + db.users.Remove(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + db.Dispose(); + } + base.Dispose(disposing); + } + } +} diff --git a/Models/Login.cs b/Models/Login.cs index 362522d..325f3da 100644 --- a/Models/Login.cs +++ b/Models/Login.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; @@ -7,6 +8,8 @@ namespace WebApplication1.Models { public class Login { + [Key] + public int ID { get; set; } public string username { get; set; } public string password { get; set; } } diff --git a/Models/Register.cs b/Models/Register.cs index 646b315..9d585ee 100644 --- a/Models/Register.cs +++ b/Models/Register.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; @@ -7,6 +8,8 @@ namespace WebApplication1.Models { public class Register { + [Key] + public int ID { get; set; } public string username { get; set; } public string password { get; set; } public string ConfirmPassword { get; set; } diff --git a/Views/Account/Login.cshtml b/Views/Account/Login.cshtml index 9a41fb3..e97d1e5 100644 --- a/Views/Account/Login.cshtml +++ b/Views/Account/Login.cshtml @@ -9,9 +9,9 @@
+ @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) | + @Html.ActionLink("Back to List", "Index") +
diff --git a/Views/users/Edit.cshtml b/Views/users/Edit.cshtml new file mode 100644 index 0000000..2dbd952 --- /dev/null +++ b/Views/users/Edit.cshtml @@ -0,0 +1,53 @@ +@model WebApplication1.Models.user + +@{ + ViewBag.Title = "Edit"; +} + ++ @Html.ActionLink("Create New", "Create") +
+| + @Html.DisplayNameFor(model => model.usename) + | ++ @Html.DisplayNameFor(model => model.password) + | ++ @Html.DisplayNameFor(model => model.displayname) + | ++ |
|---|---|---|---|
| + @Html.DisplayFor(modelItem => item.usename) + | ++ @Html.DisplayFor(modelItem => item.password) + | ++ @Html.DisplayFor(modelItem => item.displayname) + | ++ @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | + @Html.ActionLink("Details", "Details", new { id=item.ID }) | + @Html.ActionLink("Delete", "Delete", new { id=item.ID }) + | +