-
Notifications
You must be signed in to change notification settings - Fork 57
/
Index.cshtml.cs
76 lines (61 loc) · 2.71 KB
/
Index.cshtml.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
using System.Collections.Generic;
using System.Threading.Tasks;
using DevBetterWeb.Core;
using DevBetterWeb.Core.Entities;
using DevBetterWeb.Core.Interfaces;
using DevBetterWeb.Core.Specs;
using DevBetterWeb.Infrastructure.Identity.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace DevBetterWeb.Web.Pages.User.MyProfile;
[Authorize(Roles = AuthConstants.Roles.ADMINISTRATORS_MEMBERS_ALUMNI)]
public class IndexModel : PageModel
{
#nullable disable
[BindProperty]
public UserProfileViewModel UserProfileViewModel { get; set; }
public string AvatarUrl { get; set; }
public List<Book> Books { get; set; } = new List<Book>();
public string AlumniProgressPercentage { get; set; }
public MemberSubscriptionPercentBarViewModel Model { get; set; }
#nullable enable
private readonly UserManager<ApplicationUser> _userManager;
private readonly IMemberRegistrationService _memberRegistrationService;
private readonly IRepository<Member> _memberRepository;
private readonly IRepository<Book> _bookRepository;
private readonly IMemberSubscriptionPeriodCalculationsService _memberSubscriptionPeriodCalculationsService;
public IndexModel(UserManager<ApplicationUser> userManager,
IMemberRegistrationService memberRegistrationService,
IRepository<Member> memberRepository,
IRepository<Book> bookRepository,
IMemberSubscriptionPeriodCalculationsService memberSubscriptionPeriodCalculationsService)
{
_userManager = userManager;
_memberRegistrationService = memberRegistrationService;
_memberRepository = memberRepository;
_bookRepository = bookRepository;
_memberSubscriptionPeriodCalculationsService = memberSubscriptionPeriodCalculationsService;
}
public async Task OnGetAsync()
{
var currentUserName = User.Identity!.Name;
var applicationUser = await _userManager.FindByNameAsync(currentUserName!);
AvatarUrl = string.Format(Constants.AVATAR_IMGURL_FORMAT_STRING, applicationUser!.Id);
var spec = new MemberByUserIdWithBooksReadAndMemberSubscriptionsSpec(applicationUser.Id);
Member? member = await _memberRepository.FirstOrDefaultAsync(spec);
if (member == null)
{
member = await _memberRegistrationService.RegisterMemberAsync(applicationUser.Id);
}
Books = await _bookRepository.ListAsync();
int percentage = _memberSubscriptionPeriodCalculationsService.GetPercentageProgressToAlumniStatus(member);
Model = new MemberSubscriptionPercentBarViewModel(percentage);
UserProfileViewModel = new UserProfileViewModel(member);
}
public bool GetIsAlumni()
{
return User.IsInRole(AuthConstants.Roles.ALUMNI);
}
}