1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using PostgreSqlDotnetCore.Data;
|
---|
5 | using PostgreSqlDotnetCore.Models;
|
---|
6 | using System.Diagnostics;
|
---|
7 | using System.Web.Mvc;
|
---|
8 |
|
---|
9 | namespace PostgreSqlDotnetCore.Controllers
|
---|
10 | {
|
---|
11 |
|
---|
12 | public class HomeController : BaseController
|
---|
13 | {
|
---|
14 | private ApplicationDbContext db = new ApplicationDbContext();
|
---|
15 | private UserManager<IdentityUser> _userManager;
|
---|
16 |
|
---|
17 | private readonly ILogger<HomeController> _logger;
|
---|
18 |
|
---|
19 | public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager) : base(userManager)
|
---|
20 | {
|
---|
21 | _logger = logger;
|
---|
22 | _userManager = userManager;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public async Task<IActionResult> IndexAsync()
|
---|
26 | {
|
---|
27 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
28 | if (isAuthenticated)
|
---|
29 | {
|
---|
30 | var user = await _userManager.GetUserAsync(User);
|
---|
31 | if (user != null)
|
---|
32 | {
|
---|
33 | UsersClass customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
34 | if (customerClass == null)
|
---|
35 | {
|
---|
36 | string[] nameLastName = user.Email.ToString().Split('@');
|
---|
37 | string name = nameLastName[0];
|
---|
38 | string lastName = "-";
|
---|
39 | try
|
---|
40 | {
|
---|
41 | if (nameLastName[0].Contains('.'))
|
---|
42 | {
|
---|
43 | name = nameLastName[0].Split('.')[0];
|
---|
44 | lastName = nameLastName[0].Split('.')[1];
|
---|
45 | }
|
---|
46 | }
|
---|
47 | catch (Exception ex)
|
---|
48 | {
|
---|
49 | }
|
---|
50 | db.CustomerObj.Add(new UsersClass(
|
---|
51 | user.Email,
|
---|
52 | name,
|
---|
53 | lastName,
|
---|
54 | user.PasswordHash != null ? user.PasswordHash : "-",
|
---|
55 | user.PhoneNumber != null ? user.PhoneNumber : user.Email,
|
---|
56 | RoleConstants.Standard,
|
---|
57 | null
|
---|
58 | )
|
---|
59 | );
|
---|
60 | db.SaveChanges();
|
---|
61 | }
|
---|
62 | // set if is authenticated
|
---|
63 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
64 | // проба на 23.08
|
---|
65 | // no access for standard user
|
---|
66 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | ViewBag.isAuthenticated = null;
|
---|
74 | }
|
---|
75 | ViewBag.ShowTopBar = true;
|
---|
76 |
|
---|
77 | return View();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public async Task<IActionResult> PrivacyAsync()
|
---|
81 | {
|
---|
82 |
|
---|
83 | // set if is authenticated
|
---|
84 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
85 | // no access for standard user
|
---|
86 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
87 |
|
---|
88 | return View();
|
---|
89 | }
|
---|
90 | public async Task<IActionResult> ContactAsync()
|
---|
91 | {
|
---|
92 |
|
---|
93 | //var query = db.Database.ExecuteSqlRaw("CALL get_pet_details()");
|
---|
94 | //var query = db.Database.ExecuteSqlRaw("SELECT * FROM get_pet_details()");
|
---|
95 |
|
---|
96 |
|
---|
97 | // set if is authenticated
|
---|
98 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
99 | // no access for standard user
|
---|
100 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
101 |
|
---|
102 | return View();
|
---|
103 | }
|
---|
104 |
|
---|
105 | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
---|
106 | public IActionResult Error()
|
---|
107 | {
|
---|
108 | return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
---|
109 | }
|
---|
110 | }
|
---|
111 | } |
---|