Skip to content

Commit

Permalink
Add notice system
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed Jun 16, 2024
1 parent 81c478f commit 3dcd6fd
Show file tree
Hide file tree
Showing 11 changed files with 814 additions and 6 deletions.
72 changes: 72 additions & 0 deletions ReplayBrowser/Controllers/NoticeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.AspNetCore.Mvc;
using ReplayBrowser.Data;
using ReplayBrowser.Data.Models;
using ReplayBrowser.Helpers;
using ReplayBrowser.Services;

namespace ReplayBrowser.Controllers;

/// <summary>
/// The controller used for managing notices. Pretty much everything here is admin-only.
/// </summary>
[Controller]
[Route("api/Notices/")]
public class NoticeController : Controller
{
private readonly NoticeHelper _noticeHelper;
private readonly AccountService _accountService;

public NoticeController(NoticeHelper noticeHelper, AccountService accountService)
{
_noticeHelper = noticeHelper;
_accountService = accountService;
}

[HttpDelete]
[Route("DeleteNotice/{id}")]
public Task<IActionResult> DeleteNotice(int id)
{
if (!_accountService.IsAdmin(User))
{
return Task.FromResult<IActionResult>(Unauthorized());
}

_noticeHelper.DeleteNotice(id);

return Task.FromResult<IActionResult>(Ok());
}

[HttpPatch]
[Route("UpdateNotice")]
public Task<IActionResult> UpdateNotice([FromBody] Notice notice)
{
if (!_accountService.IsAdmin(User))
{
return Task.FromResult<IActionResult>(Unauthorized());
}

notice.StartDate = notice.StartDate.ToUniversalTime();
notice.EndDate = notice.EndDate.ToUniversalTime();

_noticeHelper.UpdateNotice((int)notice.Id!, notice.Title, notice.Message, notice.StartDate, notice.EndDate);

return Task.FromResult<IActionResult>(Ok());
}

[HttpPost]
[Route("CreateNotice")]
public Task<IActionResult> CreateNotice([FromBody] Notice notice)
{
if (!_accountService.IsAdmin(User))
{
return Task.FromResult<IActionResult>(Unauthorized());
}

notice.StartDate = notice.StartDate.ToUniversalTime();
notice.EndDate = notice.EndDate.ToUniversalTime();

_noticeHelper.CreateNotice(notice.Title, notice.Message, notice.StartDate, notice.EndDate);

return Task.FromResult<IActionResult>(Ok());
}
}
Loading

0 comments on commit 3dcd6fd

Please sign in to comment.