-
Notifications
You must be signed in to change notification settings - Fork 19
/
UsersController.cs
84 lines (75 loc) · 2.48 KB
/
UsersController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace SampleMvc.Web.Controllers
{
using System.Web.Mvc;
using SampleMvc.Business.Domain;
using SampleMvc.Business.Repositories;
using SampleMvc.Web.Mappers;
using SampleMvc.Web.Models;
using SampleMvc.Web.Mvc;
using System.Collections.Generic;
[HandleError]
public class UsersController : BaseController<IUsersRepository>
{
public UsersController(IUsersRepository repository, IMapper userMapper)
: base(repository, userMapper)
{ }
[AutoMap(typeof(IEnumerable<User>), typeof(IEnumerable<UserViewModel>))]
public ActionResult Index()
{
// return all users
var users = Repository.GetUsers();
return View(users);
}
public ActionResult New()
{
// return an HTML form for describing a new user
return View();
}
[HttpPost]
[AutoMap(typeof(User), typeof(UserViewModel))]
public ActionResult Create(UserViewModel userView)
{
// create a new user
if (!ModelState.IsValid)
{
return View("New", userView);
}
var user = (User)ModelMapper.Map(userView, typeof(UserViewModel), typeof(User));
Repository.Save(user);
return RedirectToAction("Index", "Users");
}
[AutoMap(typeof(User), typeof(UserViewModel))]
public ActionResult Show(int id)
{
// find and return a specific user
var user = Repository.Get(id);
return View(user);
}
[AutoMap(typeof(User), typeof(UserViewModel))]
public ActionResult Edit(int id)
{
// return an HTML form for editing a specific user
var user = Repository.Get(id);
return View(user);
}
[HttpPut]
public ActionResult Update(UserViewModel userView)
{
// find and update a specific user
if (!ModelState.IsValid)
{
return View("Edit", userView);
}
var user = (User)ModelMapper.Map(userView, typeof(UserViewModel), typeof(User));
Repository.Update(user);
return RedirectToAction("Index", "Users");
}
[HttpDelete]
public ActionResult Destroy(int id)
{
// delete a specific user
Repository.Delete(id);
return RedirectToAction("Index", "Users");
}
}
}