11namespace Identity . API . Controllers ;
2+
23using System . Linq ;
34using System . Threading . Tasks ;
45using Microsoft . AspNetCore . Mvc ;
56using Microsoft . EntityFrameworkCore ;
67using eShop . Identity . API . Data ;
78using eShop . Identity . API . Models . AccountViewModels ;
89
9- public class RegisterViewModelsController : Controller
10+ public class RegisterController : Controller
1011{
1112 private readonly ApplicationDbContext _context ;
13+ private readonly UserManager < ApplicationUser > _userManager ;
14+ private readonly SignInManager < ApplicationUser > _signInManager ;
1215
13- public RegisterViewModelsController ( ApplicationDbContext context )
16+ public RegisterController ( ApplicationDbContext context , UserManager < ApplicationUser > userManager , SignInManager < ApplicationUser > signInManager )
1417 {
1518 _context = context ;
19+ _userManager = userManager ;
20+ _signInManager = signInManager ;
21+ _context = context ;
1622 }
1723
18- // GET: RegisterViewModels
24+ // GET: Register
1925 public async Task < IActionResult > Index ( )
2026 {
2127 return View ( await _context . RegisterViewModel . ToListAsync ( ) ) ;
2228 }
2329
24- // GET: RegisterViewModels /Details/5
30+ // GET: Register /Details/5
2531 public async Task < IActionResult > Details ( int ? id )
2632 {
2733 if ( id == null )
@@ -39,18 +45,63 @@ public async Task<IActionResult> Details(int? id)
3945 return View ( registerViewModel ) ;
4046 }
4147
42- // GET: RegisterViewModels/Create
48+ [ HttpGet ]
49+ public IActionResult Register ( )
50+ {
51+ return View ( ) ;
52+ }
53+
54+ [ HttpPost ]
55+ [ ValidateAntiForgeryToken ]
56+ public async Task < IActionResult > Create ( RegisterViewModel model )
57+ {
58+ if ( ModelState . IsValid )
59+ {
60+ var user = new ApplicationUser
61+ {
62+ UserName = model . Email ,
63+ Email = model . Email ,
64+ CardNumber = model . CardNumber ,
65+ SecurityNumber = model . SecurityNumber ,
66+ Expiration = model . Expiration ,
67+ CardHolderName = model . CardHolderName ,
68+ CardType = model . CardType ,
69+ Street = model . Street ,
70+ City = model . City ,
71+ State = model . State ,
72+ Country = model . Country ,
73+ ZipCode = model . ZipCode ,
74+ Name = model . Name ,
75+ LastName = model . LastName
76+ } ;
77+ var result = await _userManager . CreateAsync ( user , model . Password ) ;
78+ if ( result . Succeeded )
79+ {
80+ await _signInManager . SignInAsync ( user , isPersistent : false ) ;
81+ return RedirectToAction ( nameof ( HomeController . Index ) , "Home" ) ;
82+ }
83+ foreach ( var error in result . Errors )
84+ {
85+ ModelState . AddModelError ( string . Empty , error . Description ) ;
86+ }
87+ }
88+
89+ // If we got this far, something failed, redisplay form
90+ return View ( model ) ;
91+ }
92+
93+ // GET: Register/Create
4394 public IActionResult Create ( )
4495 {
4596 return View ( ) ;
4697 }
4798
48- // POST: RegisterViewModels /Create
99+ // POST: Register /Create
49100 // To protect from overposting attacks, enable the specific properties you want to bind to.
50101 // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
51102 [ HttpPost ]
52103 [ ValidateAntiForgeryToken ]
53- public async Task < IActionResult > Create ( [ Bind ( "Id,Email,Password,ConfirmPassword" ) ] RegisterViewModel registerViewModel )
104+ public async Task < IActionResult > OriginalCreate ( [ Bind ( "Id,Email,Password,ConfirmPassword" ) ] RegisterViewModel registerViewModel )
54105 {
55106 if ( ModelState . IsValid )
56107 {
@@ -61,7 +112,7 @@ public async Task<IActionResult> Create([Bind("Id,Email,Password,ConfirmPassword
61112 return View ( registerViewModel ) ;
62113 }
63114
64- // GET: RegisterViewModels /Edit/5
115+ // GET: Register /Edit/5
65116 public async Task < IActionResult > Edit ( int ? id )
66117 {
67118 if ( id == null )
@@ -77,7 +128,7 @@ public async Task<IActionResult> Edit(int? id)
77128 return View ( registerViewModel ) ;
78129 }
79130
80- // POST: RegisterViewModels /Edit/5
131+ // POST: Register /Edit/5
81132 // To protect from overposting attacks, enable the specific properties you want to bind to.
82133 // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
83134 [ HttpPost ]
@@ -112,7 +163,7 @@ public async Task<IActionResult> Edit(int id, [Bind("Id,Email,Password,ConfirmPa
112163 return View ( registerViewModel ) ;
113164 }
114165
115- // GET: RegisterViewModels /Delete/5
166+ // GET: Register /Delete/5
116167 public async Task < IActionResult > Delete ( int ? id )
117168 {
118169 if ( id == null )
@@ -130,7 +181,7 @@ public async Task<IActionResult> Delete(int? id)
130181 return View ( registerViewModel ) ;
131182 }
132183
133- // POST: RegisterViewModels /Delete/5
184+ // POST: Register /Delete/5
134185 [ HttpPost , ActionName ( "Delete" ) ]
135186 [ ValidateAntiForgeryToken ]
136187 public async Task < IActionResult > DeleteConfirmed ( int id )
0 commit comments