| [752] | 1 | using System.Linq;
|
|---|
| 2 | using System.Web.Mvc;
|
|---|
| 3 | using Wierszowki.Core.Interfaces;
|
|---|
| 4 | using Wierszowki.Core.Linq;
|
|---|
| 5 | using Wierszowki.Services;
|
|---|
| 6 | using Wierszowki.Services.Interfaces;
|
|---|
| 7 | using xVal.ServerSide;
|
|---|
| 8 |
|
|---|
| 9 | namespace Wierszowki.Controllers
|
|---|
| 10 | {
|
|---|
| 11 | [HandleError]
|
|---|
| 12 | [Authorize]
|
|---|
| 13 | public class UserController : Controller
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IUserService _service;
|
|---|
| 16 |
|
|---|
| 17 | public UserController()
|
|---|
| 18 | {
|
|---|
| 19 | _service = new UserService(new LinqRepository<User>());
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | public UserController(IUserService service)
|
|---|
| 23 | {
|
|---|
| 24 | _service = service;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | public ActionResult Index()
|
|---|
| 28 | {
|
|---|
| 29 | var users = _service.FindAll();
|
|---|
| 30 | return View(users);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | [Authorize]
|
|---|
| 34 | [AcceptVerbs(HttpVerbs.Get)]
|
|---|
| 35 | public ActionResult Create()
|
|---|
| 36 | {
|
|---|
| 37 | return View(new User());
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | [Authorize]
|
|---|
| 41 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 42 | public ActionResult Create(User user)
|
|---|
| 43 | {
|
|---|
| 44 | try
|
|---|
| 45 | {
|
|---|
| 46 | _service.Create(user);
|
|---|
| 47 | }
|
|---|
| 48 | catch (RulesException ex)
|
|---|
| 49 | {
|
|---|
| 50 | ex.AddModelStateErrors(ModelState, "user");
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (ModelState.IsValid)
|
|---|
| 54 | return View("Confirm", user);
|
|---|
| 55 |
|
|---|
| 56 | return View(user);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | [Authorize]
|
|---|
| 60 | public ActionResult Confirm(User user)
|
|---|
| 61 | {
|
|---|
| 62 | return View(user);
|
|---|
| 63 | }
|
|---|
| 64 | [Authorize]
|
|---|
| 65 | [AcceptVerbs(HttpVerbs.Get)]
|
|---|
| 66 | public ActionResult Edit(int id)
|
|---|
| 67 | {
|
|---|
| [838] | 68 | User user = _service.Find(id);
|
|---|
| 69 | if (user != null)
|
|---|
| 70 | {
|
|---|
| 71 | string name = ControllerContext.HttpContext.User.Identity.Name;
|
|---|
| 72 | var LogInUser = _service.FindOne(u => u.Login == name);
|
|---|
| 73 | if (LogInUser.Id == user.Id)
|
|---|
| 74 | {
|
|---|
| [752] | 75 | return View(_service.Find(id));
|
|---|
| 76 | }
|
|---|
| [838] | 77 | else
|
|---|
| 78 | {
|
|---|
| 79 | ViewData["message"] = "error_user";
|
|---|
| 80 | return View("Confirm");
|
|---|
| 81 | }
|
|---|
| [752] | 82 |
|
|---|
| [838] | 83 | }
|
|---|
| 84 | ViewData["message"] = "error";
|
|---|
| 85 | return View("Confirm");
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| [752] | 88 | [Authorize]
|
|---|
| 89 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 90 | public ActionResult Edit(User user)
|
|---|
| 91 | {
|
|---|
| 92 | try
|
|---|
| 93 | {
|
|---|
| 94 | _service.Update(user);
|
|---|
| [838] | 95 |
|
|---|
| [752] | 96 | }
|
|---|
| 97 | catch (RulesException ex)
|
|---|
| 98 | {
|
|---|
| 99 | ex.AddModelStateErrors(ModelState, "user");
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (ModelState.IsValid)
|
|---|
| 103 | return View("Confirm", user);
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | return View(user);
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | } |
|---|