49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
|
|
using JobsMedical.Web.Data;
|
||
|
|
using JobsMedical.Web.Models;
|
||
|
|
using JobsMedical.Web.Services;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace JobsMedical.Web.Pages.Admin;
|
||
|
|
|
||
|
|
[Authorize(Roles = "Admin")]
|
||
|
|
public class BroadcastModel : PageModel
|
||
|
|
{
|
||
|
|
private readonly AppDbContext _db;
|
||
|
|
private readonly NotificationService _notify;
|
||
|
|
|
||
|
|
public BroadcastModel(AppDbContext db, NotificationService notify)
|
||
|
|
{
|
||
|
|
_db = db;
|
||
|
|
_notify = notify;
|
||
|
|
}
|
||
|
|
|
||
|
|
[BindProperty] public string Title { get; set; } = "";
|
||
|
|
[BindProperty] public string? Body { get; set; }
|
||
|
|
[BindProperty] public string? Url { get; set; }
|
||
|
|
[BindProperty] public string Audience { get; set; } = "all"; // all | staff | employers
|
||
|
|
[TempData] public string? Result { get; set; }
|
||
|
|
|
||
|
|
public void OnGet() { }
|
||
|
|
|
||
|
|
public async Task<IActionResult> OnPostAsync()
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(Title)) { Result = "عنوان لازم است."; return Page(); }
|
||
|
|
|
||
|
|
IQueryable<User> q = _db.Users.Where(u => !u.IsBanned);
|
||
|
|
q = Audience switch
|
||
|
|
{
|
||
|
|
"staff" => q.Where(u => u.Role == UserRole.Doctor),
|
||
|
|
"employers" => q.Where(u => u.Role == UserRole.FacilityAdmin),
|
||
|
|
_ => q,
|
||
|
|
};
|
||
|
|
var ids = await q.Select(u => u.Id).ToListAsync();
|
||
|
|
await _notify.BroadcastAsync(ids, Title.Trim(), Body?.Trim(), string.IsNullOrWhiteSpace(Url) ? "/" : Url.Trim());
|
||
|
|
|
||
|
|
Result = $"اعلان برای {ids.Count} کاربر ارسال شد (دراپ + پوش در صورت فعالبودن).";
|
||
|
|
return RedirectToPage();
|
||
|
|
}
|
||
|
|
}
|