using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using OT_lib.Interfaces; using OT_lib.BusinessLayer; using OT_lib.Entity; using OverTime.Models; using PagedList; namespace OverTime.Controllers { public class UserController : Controller { [HttpGet] public ActionResult Pagination(int? page) { var dummyItems = Enumerable.Range(1, 150).Select(x => "Items" + x); var pager = new Pager(dummyItems.Count(), page); var viewModel = new UserModel() { Items = dummyItems.Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize), Pager = pager }; return View(viewModel); } public ActionResult List(UserModel usermodel) { //UserBusinessLayer userService = new UserBusinessLayer("sqlconn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); List list = userService.GetAllUsers(); List listModel = new List(); foreach (var item in list) { listModel.Add(new UserModel() { email = item.email, password = item.password, username = item.username, usr_Id = item.usr_Id }); } return View(listModel); } public ActionResult CreateUser() { return View(); } [HttpPost] public ActionResult CreateUser(UserModel userModel) { //UserBusinessLayer userService = new UserBusinessLayer("sqlConn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); userService.CreateUser(new UserEntity() { username = userModel.username, email = userModel.email, password = userModel.password }); return RedirectToAction("List"); } public ActionResult EditUser(int id) { //UserBusinessLayer userService = new UserBusinessLayer("sqlconn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); UserModel userModel = new UserModel(); try { UserEntity userEntity = userService.GetUserId(id); userModel.usr_Id = userEntity.usr_Id; userModel.email = userEntity.email; userModel.username = userEntity.username; userModel.password = userEntity.password; } catch (Exception ex) { throw ex; } return View(userModel); } //Update user info [HttpPost] public ActionResult EditUser(UserModel userModel) { //UserBusinessLayer userService = new UserBusinessLayer("sqlConn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); userService.EditUser(new UserEntity() { usr_Id = userModel.usr_Id, username = userModel.username, email = userModel.email, password = userModel.password }); return RedirectToAction("List"); } public ActionResult DeleteUser(int id) { //UserBusinessLayer userService = new UserBusinessLayer("sqlconn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); UserModel userModel = new UserModel(); try { UserEntity userEntity = userService.GetUserId(id); userModel.usr_Id = userEntity.usr_Id; userModel.email = userEntity.email; userModel.username = userEntity.username; userModel.password = userEntity.password; } catch (Exception ex) { throw ex; } return View(userModel); } [HttpPost, ActionName("DeleteUser")] public ActionResult DeleteUserConfirmed(int id) { //UserBusinessLayer userService = new UserBusinessLayer("sqlconn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); try { userService.DeleteUser(id); } catch (Exception eDelete) { throw eDelete; } return RedirectToAction("List"); } public ActionResult Details(int id) { //UserBusinessLayer userService = new UserBusinessLayer("sqlconn"); IUserInterface userService = new UserBusinessLayer("sqlConn"); UserModel userModel = new UserModel(); try { UserEntity userEntity = userService.GetUserId(id); userModel.usr_Id = userEntity.usr_Id; userModel.email = userEntity.email; userModel.username = userEntity.username; userModel.password = userEntity.password; } catch (Exception ex) { throw ex; } return View(userModel); } } }