using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authorization; using ZpcBulletinBoard.Data; using ZpcBulletinBoard.Models.Editor; namespace ZpcBulletinBoard.Pages.BoardsLinks { [Authorize] public class IndexModel(ApplicationDbContext context) : PageModel { public IList Pages { get;set; } public async Task OnGetAsync() { Pages = await context.BulletinBoardPage.OrderBy(x => x.Name).ToListAsync(); } //Get public IActionResult OnGetBoards() { var boards = context.BulletinBoards .Include(x => x.Links.OrderBy(y => y.Order)) .ThenInclude(x => x.BulletinBoardPage) .ToList(); foreach (var board in boards) { foreach (var link in board.Links) { link.BulletinBoardPage.Links = null; link.BulletinBoard = null; } } return new JsonResult(new { successful = true, error = $"", boards }); } //Post public IActionResult OnPostAddLink(int idBoard, int idPage) { var order = 1; var links = context.BulletinBoardPageLinks.Where(x => x.IdBulletinBoardFk == idBoard).ToList(); if (links.Any()) order = links.Max(x => x.IdLink) + 1; var link = new BulletinBoardPageLink { Duration = 30, IdBulletinBoardFk = idBoard, IdBulletinBoardPageFk = idPage, Order = order }; context.BulletinBoardPageLinks.Add(link); context.SaveChanges(); link = context.BulletinBoardPageLinks.Include(x => x.BulletinBoardPage).First(x => x.IdLink == link.IdLink); link.BulletinBoardPage.Links = null; return new JsonResult(new { successful = true, error = $"", link }); } } }