1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using PostgreSqlDotnetCore.Data;
|
---|
4 | using PostgreSqlDotnetCore.Models;
|
---|
5 | using System.Diagnostics;
|
---|
6 |
|
---|
7 | namespace PostgreSqlDotnetCore.Controllers
|
---|
8 | {
|
---|
9 |
|
---|
10 | public class HomeController : Controller
|
---|
11 | {
|
---|
12 | private ApplicationDbContext db = new ApplicationDbContext();
|
---|
13 | private UserManager<IdentityUser> _userManager;
|
---|
14 |
|
---|
15 | private readonly ILogger<HomeController> _logger;
|
---|
16 |
|
---|
17 | public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager)
|
---|
18 | {
|
---|
19 | _logger = logger;
|
---|
20 | _userManager = userManager;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public async Task<IActionResult> IndexAsync()
|
---|
24 | {
|
---|
25 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
26 | if (isAuthenticated)
|
---|
27 | {
|
---|
28 | var user = await _userManager.GetUserAsync(User);
|
---|
29 | if (user != null)
|
---|
30 | {
|
---|
31 | UsersClass customerClass = db.CustomerObj.SingleOrDefault(x=> x.email == user.Email);
|
---|
32 | if (customerClass == null)
|
---|
33 | {
|
---|
34 | string[] nameLastName = user.Email.ToString().Split('@');
|
---|
35 | string name = nameLastName[0];
|
---|
36 | string lastName = "-";
|
---|
37 | try
|
---|
38 | {
|
---|
39 | if (nameLastName[0].Contains('.'))
|
---|
40 | {
|
---|
41 | name = nameLastName[0].Split('.')[0];
|
---|
42 | lastName = nameLastName[0].Split('.')[1];
|
---|
43 | }
|
---|
44 | } catch(Exception ex) {
|
---|
45 | }
|
---|
46 | db.CustomerObj.Add(new UsersClass(
|
---|
47 | user.Email,
|
---|
48 | name,
|
---|
49 | lastName,
|
---|
50 | user.PasswordHash != null ? user.PasswordHash : "-",
|
---|
51 | user.PhoneNumber != null ? user.PhoneNumber : user.Email,
|
---|
52 | RoleConstants.Standard,
|
---|
53 | null
|
---|
54 | )
|
---|
55 | );
|
---|
56 | db.SaveChanges();
|
---|
57 | }
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | }
|
---|
62 | ViewBag.ShowTopBar = true;
|
---|
63 | return View();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public IActionResult Privacy()
|
---|
67 | {
|
---|
68 | return View();
|
---|
69 | }
|
---|
70 | public IActionResult Contact()
|
---|
71 | {
|
---|
72 | return View();
|
---|
73 | }
|
---|
74 |
|
---|
75 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
---|
76 | public IActionResult Error()
|
---|
77 | {
|
---|
78 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
---|
79 | }
|
---|
80 | }
|
---|
81 | } |
---|