source: PostgreSqlDotnetCore/Controllers/HomeController.cs@ e90ba32

main
Last change on this file since e90ba32 was e9bb9d1, checked in by ElenaMoskova <elena.moskova99@…>, 3 months ago

Use of views

  1. Use of views in VetCenters.
  2. Ability to provide a response to a given response.
  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
[e9bb9d1]3using Microsoft.EntityFrameworkCore;
[2aea0fd]4using PostgreSqlDotnetCore.Data;
5using PostgreSqlDotnetCore.Models;
6using System.Diagnostics;
[6782104]7using System.Web.Mvc;
[2aea0fd]8
9namespace PostgreSqlDotnetCore.Controllers
10{
11
[6782104]12 public class HomeController : BaseController
[2aea0fd]13 {
14 private ApplicationDbContext db = new ApplicationDbContext();
15 private UserManager<IdentityUser> _userManager;
16
17 private readonly ILogger<HomeController> _logger;
18
[6782104]19 public HomeController(ILogger<HomeController> logger, UserManager<IdentityUser> userManager) : base(userManager)
[2aea0fd]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 {
[e9bb9d1]33 UsersClass customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
[2aea0fd]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 }
[e9bb9d1]46 }
47 catch (Exception ex)
48 {
[2aea0fd]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 }
[6782104]62 // set if is authenticated
63 ViewBag.isAuthenticated = await getCrrentUser();
[118e414]64 // проба на 23.08
65 // no access for standard user
66 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
67
[2aea0fd]68 }
69
[e9bb9d1]70 }
71 else
[6782104]72 {
73 ViewBag.isAuthenticated = null;
[2aea0fd]74 }
75 ViewBag.ShowTopBar = true;
[6782104]76
[2aea0fd]77 return View();
78 }
79
[6782104]80 public async Task<IActionResult> PrivacyAsync()
[2aea0fd]81 {
[6782104]82
83 // set if is authenticated
84 ViewBag.isAuthenticated = await getCrrentUser();
[118e414]85 // no access for standard user
86 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
87
[2aea0fd]88 return View();
89 }
[6782104]90 public async Task<IActionResult> ContactAsync()
[2aea0fd]91 {
[e9bb9d1]92
93 //var query = db.Database.ExecuteSqlRaw("CALL get_pet_details()");
94 //var query = db.Database.ExecuteSqlRaw("SELECT * FROM get_pet_details()");
95
96
[6782104]97 // set if is authenticated
98 ViewBag.isAuthenticated = await getCrrentUser();
[118e414]99 // no access for standard user
100 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
101
[2aea0fd]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}
Note: See TracBrowser for help on using the repository browser.