164 lines
7.5 KiB
C#
164 lines
7.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using InternshipSystem.Api.Queries;
|
|
using InternshipSystem.Core;
|
|
using InternshipSystem.Repository;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace InternshipSystem.Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("staticPage")]
|
|
public class StaticPagesController : ControllerBase
|
|
{
|
|
public StaticPagesController(InternshipDbContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
private InternshipDbContext Context { get; }
|
|
|
|
/// <summary>
|
|
/// Get all static pages, only available for coordinator
|
|
/// </summary>
|
|
/// <returns>List of static pages with titles and content</returns>
|
|
/// <response code="200">Static pages list returned successfully</response>
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<IList<StaticPage>>> GetStaticPages(CancellationToken cancellationToken) =>
|
|
await Context.StaticPages
|
|
.ToListAsync(cancellationToken);
|
|
|
|
/// <summary>
|
|
/// Get static page, only available for coordinator
|
|
/// </summary>
|
|
/// <param name="accessName">Name of page</param>
|
|
/// <returns>Static page title and content</returns>
|
|
/// <response code="200">Static page returned successfully</response>
|
|
/// <response code="404">Static page with given access name do not exist</response>
|
|
[HttpGet("{accessName}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<ActionResult<StaticPage>> GetStaticPage(string accessName, CancellationToken cancellationToken)
|
|
{
|
|
var page =
|
|
await Context.StaticPages
|
|
.Where(p => p.AccessName.Trim().ToLower().Equals(accessName.Trim().ToLower()))
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
if (page == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok(page);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add or update static page, only available for coordinator
|
|
/// </summary>
|
|
/// <response code="200">Static page updated successfully</response>
|
|
/// <response code="400">Static page form is not valid</response>
|
|
/// <response code="401">This action is only available for authorized internship admin</response>
|
|
/// <response code="404">Static page with given id do not exist</response>
|
|
[HttpPut]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpdateStaticPage([FromBody] StaticPageForm staticPageForm, CancellationToken cancellationToken)
|
|
{
|
|
var validator = new StaticPageForm.Validator();
|
|
var validationResult = await validator.ValidateAsync(staticPageForm, cancellationToken);
|
|
|
|
if (!validationResult.IsValid)
|
|
{
|
|
return BadRequest(validationResult.ToString());
|
|
}
|
|
|
|
if (staticPageForm.Id.HasValue)
|
|
{
|
|
var pageToUpdate = await Context.StaticPages.FindAsync(staticPageForm.Id);
|
|
|
|
if (pageToUpdate == null)
|
|
{
|
|
return NotFound($"Static page with id: {staticPageForm.Id} does not exist");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(staticPageForm.AccessName))
|
|
{
|
|
var pageWithSameAccessName = await Context.StaticPages
|
|
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(staticPageForm.AccessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
|
|
|
if (pageWithSameAccessName != null && !pageWithSameAccessName.Id.Equals(pageToUpdate.Id))
|
|
{
|
|
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
|
|
}
|
|
}
|
|
|
|
pageToUpdate.AccessName = string.IsNullOrEmpty(staticPageForm.AccessName) ? pageToUpdate.AccessName : staticPageForm.AccessName;
|
|
pageToUpdate.Title = string.IsNullOrEmpty(staticPageForm.Title) ? pageToUpdate.Title : staticPageForm.Title;
|
|
pageToUpdate.TitleEng = string.IsNullOrEmpty(staticPageForm.TitleEng) ? pageToUpdate.TitleEng : staticPageForm.TitleEng;
|
|
pageToUpdate.Content = string.IsNullOrEmpty(staticPageForm.Content) ? pageToUpdate.Content : staticPageForm.Content;
|
|
pageToUpdate.ContentEng = string.IsNullOrEmpty(staticPageForm.ContentEng) ? pageToUpdate.ContentEng : staticPageForm.ContentEng;
|
|
}
|
|
else
|
|
{
|
|
var pageWithSameAccessName = await Context.StaticPages
|
|
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(staticPageForm.AccessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
|
|
|
if (pageWithSameAccessName != null)
|
|
{
|
|
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
|
|
}
|
|
|
|
var newStaticPage = new StaticPage
|
|
{
|
|
AccessName = staticPageForm.AccessName.ToLower().Trim(),
|
|
Title = staticPageForm.Title,
|
|
TitleEng = staticPageForm.Title,
|
|
Content = staticPageForm.Content,
|
|
ContentEng = staticPageForm.ContentEng,
|
|
};
|
|
await Context.StaticPages.AddAsync(newStaticPage, cancellationToken);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
|
|
return Ok($"Static page updated successfully");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete static page, only available for coordinator
|
|
/// </summary>
|
|
/// <response code="200">Static page deleted successfully</response>
|
|
/// <response code="401">This action is only available for authorized internship admin</response>
|
|
/// <response code="404">Static page with given access name do not exist</response>
|
|
[HttpDelete("{accessName}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> DeleteStaticPage(string accessName, CancellationToken cancellationToken)
|
|
{
|
|
var pageToDelete = await Context.StaticPages
|
|
.FirstOrDefaultAsync(sp => sp.AccessName.ToLower().Trim().Equals(accessName.ToLower().Trim()), cancellationToken: cancellationToken);
|
|
|
|
if (pageToDelete == null)
|
|
{
|
|
return NotFound($"Static page with access name: {accessName} does not exist");
|
|
}
|
|
|
|
Context.StaticPages.Attach(pageToDelete);
|
|
Context.StaticPages.Remove(pageToDelete);
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok($"Static page with access name: {accessName} deleted successfully");
|
|
}
|
|
}
|
|
} |