85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
|
|
using System.Security.Claims;
|
||
|
|
using JobsMedical.Web.Data;
|
||
|
|
using JobsMedical.Web.Models;
|
||
|
|
using JobsMedical.Web.Services;
|
||
|
|
using Microsoft.AspNetCore.Authentication;
|
||
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace JobsMedical.Web.Pages.Employer;
|
||
|
|
|
||
|
|
[Authorize]
|
||
|
|
public class RegisterFacilityModel : PageModel
|
||
|
|
{
|
||
|
|
private readonly AppDbContext _db;
|
||
|
|
public RegisterFacilityModel(AppDbContext db) => _db = db;
|
||
|
|
|
||
|
|
public List<City> Cities { get; private set; } = new();
|
||
|
|
public List<District> Districts { get; private set; } = new();
|
||
|
|
|
||
|
|
[BindProperty] public string Name { get; set; } = "";
|
||
|
|
[BindProperty] public FacilityType Type { get; set; }
|
||
|
|
[BindProperty] public int CityId { get; set; }
|
||
|
|
[BindProperty] public int? DistrictId { get; set; }
|
||
|
|
[BindProperty] public string? Address { get; set; }
|
||
|
|
[BindProperty] public string? Phone { get; set; }
|
||
|
|
[BindProperty] public string? BaleId { get; set; }
|
||
|
|
[BindProperty] public double? Lat { get; set; }
|
||
|
|
[BindProperty] public double? Lng { get; set; }
|
||
|
|
public string? Error { get; private set; }
|
||
|
|
|
||
|
|
public async Task OnGetAsync() => await LoadListsAsync();
|
||
|
|
|
||
|
|
public async Task<IActionResult> OnPostAsync()
|
||
|
|
{
|
||
|
|
await LoadListsAsync();
|
||
|
|
if (string.IsNullOrWhiteSpace(Name) || CityId == 0)
|
||
|
|
{
|
||
|
|
Error = "نام مرکز و شهر الزامی است.";
|
||
|
|
return Page();
|
||
|
|
}
|
||
|
|
|
||
|
|
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
||
|
|
var facility = new Facility
|
||
|
|
{
|
||
|
|
Name = Name.Trim(),
|
||
|
|
Type = Type,
|
||
|
|
CityId = CityId,
|
||
|
|
DistrictId = DistrictId,
|
||
|
|
Address = Address?.Trim(),
|
||
|
|
Phone = Phone?.Trim(),
|
||
|
|
BaleId = BaleId?.Trim(),
|
||
|
|
Lat = Lat,
|
||
|
|
Lng = Lng,
|
||
|
|
OwnerUserId = userId,
|
||
|
|
IsVerified = false, // platform verifies later
|
||
|
|
};
|
||
|
|
_db.Facilities.Add(facility);
|
||
|
|
|
||
|
|
// Promote the user to FacilityAdmin (keep Admin if already admin) and refresh the cookie.
|
||
|
|
var user = await _db.Users.FindAsync(userId);
|
||
|
|
if (user is not null && user.Role == UserRole.Doctor)
|
||
|
|
{
|
||
|
|
user.Role = UserRole.FacilityAdmin;
|
||
|
|
await _db.SaveChangesAsync();
|
||
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
|
||
|
|
AuthHelper.BuildPrincipal(user));
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
await _db.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
return RedirectToPage("/Employer/Index");
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task LoadListsAsync()
|
||
|
|
{
|
||
|
|
Cities = await _db.Cities.OrderByDescending(c => c.IsActive).ThenBy(c => c.Name).ToListAsync();
|
||
|
|
Districts = await _db.Districts.Where(d => d.IsActive).OrderBy(d => d.Name).ToListAsync();
|
||
|
|
}
|
||
|
|
}
|