using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using ZpcBulletinBoard.Data; using ZpcBulletinBoard.Models.Editor; namespace ZpcBulletinBoard.Pages.Editor { [Authorize] public class EditMainModel(ILogger logger, ApplicationDbContext context) : PageModel { private readonly ILogger _logger = logger; public void OnGet() { } //Get public IActionResult OnGetBoards() { var boards= context.BulletinBoards.ToList(); return new JsonResult(new { successful = true, error = $"", boards }); } public IActionResult OnGetBoard(int id) { var board = context.BulletinBoards.Include(x => x.Pages) .FirstOrDefault(x => x.IdBulletinBoard == id); //Ce je ntre board pole json vrze vn foreach (var bulletinBoardPage in board.Pages) { bulletinBoardPage.BulletinBoard = null; } return board == null ? new JsonResult(new { successful = false, error = $"Board with ID {id} not exists!"}) : new JsonResult(new { successful = true, error = $"", board }); } public IActionResult OnGetPage(int id) { var page = context.BulletinBoardPage.Include(x => x.Notes) .FirstOrDefault(x => x.IdBulletinBoardPage == id); return page == null ? new JsonResult(new { successful = false, error = $"Page with ID {id} not exists!" }) : new JsonResult(new { successful = true, error = $"", page }); } //Post public IActionResult OnPostBoardPage(BulletinBoardPage page) { if (page.IdBulletinBoardPage <= 0) { context.BulletinBoardPage.Add(page); context.SaveChanges(); } else { var tmpPage = context.BulletinBoardPage.FirstOrDefault(x => x.IdBulletinBoardPage == page.IdBulletinBoardPage); if (tmpPage == null) return new JsonResult(new { successful = false, error = $"Page with ID {page.IdBulletinBoardPage} not exists!", page }); tmpPage.Name = page.Name; tmpPage.Duration = page.Duration; context.SaveChanges(); } return new JsonResult(new { successful = true, error = $"", page }); } } }