Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BulletinBoard.Models;
using BulletinBoard.Repository;
namespace BulletinBoard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AnnouncementsController : ControllerBase
{
private readonly AnnouncementRepository _announcementRepository;
private readonly NotificationLogRepository _notificationLogRepository;
private readonly ILogger<AnnouncementsController> _logger;
public AnnouncementsController(AnnouncementRepository announcementRepository, NotificationLogRepository notificationLogRepository, ILogger<AnnouncementsController> logger)
{
_announcementRepository = announcementRepository;
_notificationLogRepository = notificationLogRepository;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AnnouncementLog>>> GetAnnouncements()
{
var announcements = await _announcementRepository.GetAnnouncementsAsync();
// Filter to only include pinned announcements and limit to 9
var pinnedAnnouncements = announcements.Where(a => a.Pinned).OrderByDescending(a => a.PinnedTimestamp).Take(9);
return Ok(pinnedAnnouncements);
}
[HttpGet("{id}")]
public async Task<ActionResult<AnnouncementLog>> GetAnnouncement(int id)
{
var announcement = await _announcementRepository.GetAnnouncementAsync(id);
if (announcement == null)
{
return NotFound("Announcement not found");
}
return Ok(announcement);
}
[HttpPost]
public async Task<ActionResult<AnnouncementLog>> CreateAnnouncement([FromBody] AnnouncementLog announcement)
{
try
{
var networkID = "ahmad"; // Hardcoded NetworkID
announcement.NetworkID = networkID;
announcement.Timestamp = DateTime.UtcNow; // Use UTC for consistency
announcement.Pinned = true; // Default to true as per requirement
announcement.PinnedTimestamp = DateTime.UtcNow.AddDays(3); // Set to unpin after 3 days
var createdAnnouncement = await _announcementRepository.CreateAnnouncementAsync(announcement);
return CreatedAtAction(nameof(GetAnnouncement), new { id = createdAnnouncement.ID }, createdAnnouncement);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while creating the announcement.");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateAnnouncement(int id, [FromBody] AnnouncementLog announcement)
{
var existingAnnouncement = await _announcementRepository.GetAnnouncementAsync(id);
if (existingAnnouncement == null)
{
return NotFound("Announcement not found");
}
announcement.ID = id;
await _announcementRepository.UpdateAnnouncementAsync(announcement);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteAnnouncement(int id)
{
var announcement = await _announcementRepository.GetAnnouncementAsync(id);
if (announcement == null)
{
return NotFound("Announcement not found");
}
await _announcementRepository.DeleteAnnouncementAsync(id);
return NoContent();
}
[HttpPost("{id}/toggle-pin")]
public async Task<IActionResult> TogglePin(int id)
{
try
{
await _announcementRepository.TogglePinAsync(id);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while toggling the pin.");
return StatusCode(500, "Internal server error");
}
}
[HttpPost("{id}/notifications")]
public async Task<IActionResult> AddNotification(int id, [FromBody] NotificationLog notificationLog)
{
try
{
if (notificationLog == null)
{
return BadRequest("NotificationLog is required.");
}
notificationLog.AnnouncementID = id;
notificationLog.Timestamp = DateTime.Now;
await _notificationLogRepository.AddNotificationAsync(notificationLog);
return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while adding the notification.");
return StatusCode(500, "Internal server error");
}
}
[HttpGet("with-details")]
public async Task<ActionResult<IEnumerable<dynamic>>> GetAnnouncementsWithDetails()
{
try
{
var announcementsWithDetails = await _announcementRepository.GetAnnouncementsWithDetailsAsync();
return Ok(announcementsWithDetails);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while fetching the announcements with details.");
return StatusCode(500, "Internal server error");
}
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team