| 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 |
|
|---|
| 65 | [Authorize]
|
|---|
| 66 | [AcceptVerbs(HttpVerbs.Get)]
|
|---|
| 67 | public ActionResult Edit(int id)
|
|---|
| 68 | {
|
|---|
| 69 | return View(_service.Find(id));
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | [Authorize]
|
|---|
| 73 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 74 | public ActionResult Edit(User user)
|
|---|
| 75 | {
|
|---|
| 76 | try
|
|---|
| 77 | {
|
|---|
| 78 | _service.Update(user);
|
|---|
| 79 | }
|
|---|
| 80 | catch (RulesException ex)
|
|---|
| 81 | {
|
|---|
| 82 | ex.AddModelStateErrors(ModelState, "user");
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | if (ModelState.IsValid)
|
|---|
| 86 | return View("Confirm", user);
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | return View(user);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | } |
|---|