diff --git a/Controllers/DisplayController.cs b/Controllers/DisplayController.cs new file mode 100644 index 0000000..f969da3 --- /dev/null +++ b/Controllers/DisplayController.cs @@ -0,0 +1,71 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using NoticeBoard.Data; + +namespace NoticeBoard.Controllers; + +public class DisplayController : Controller +{ + private readonly AppDbContext _db; + + public DisplayController(AppDbContext db) + { + _db = db; + } + + public async Task Show(string slug) + { + var device = await _db.Devices + .Include(d => d.DeviceSlides.Where(ds => ds.Enabled).OrderBy(ds => ds.DisplayOrder)) + .ThenInclude(ds => ds.Slide) + .FirstOrDefaultAsync(d => d.Slug == slug.ToLower()); + + if (device == null) + { + var devices = await _db.Devices.OrderBy(d => d.Name).ToListAsync(); + return View("NotFound", devices); + } + + return View(device); + } + + [HttpGet] + [Route("api/playlist/{slug}")] + public async Task GetPlaylist(string slug) + { + var device = await _db.Devices + .Include(d => d.DeviceSlides.Where(ds => ds.Enabled).OrderBy(ds => ds.DisplayOrder)) + .ThenInclude(ds => ds.Slide) + .FirstOrDefaultAsync(d => d.Slug == slug.ToLower()); + + if (device == null) return NotFound(); + + var playlist = device.DeviceSlides.Select(ds => new + { + id = ds.Slide.Id, + name = ds.Slide.Name, + type = ds.Slide.SlideType.ToString().ToLower(), + content = ds.Slide.Content, + embedUrl = ds.Slide.EmbedUrl, + icsSource = ds.Slide.IcsSource, + backgroundColor = ds.Slide.BackgroundColor, + backgroundImage = ds.Slide.BackgroundImage, + customCss = ds.Slide.CustomCss, + duration = ds.DurationSeconds, + updatedAt = ds.Slide.UpdatedAt + }); + + return Json(new + { + device = new + { + name = device.Name, + slug = device.Slug, + width = device.ResolutionWidth, + height = device.ResolutionHeight, + transition = device.Transition + }, + slides = playlist + }); + } +}