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; } /// /// Get all static pages, only available for coordinator /// /// List of static pages with titles and content /// Static pages list returned successfully [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] public async Task>> GetStaticPages(CancellationToken cancellationToken) => await Context.StaticPages .ToListAsync(cancellationToken); /// /// Get static page, only available for coordinator /// /// Name of page /// Static page title and content /// Static page returned successfully /// Static page with given access name do not exist [HttpGet("{accessName}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> 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); } /// /// Add or update static page, only available for coordinator /// /// Static page updated successfully /// Static page form is not valid /// This action is only available for authorized internship admin /// Static page with given id do not exist [HttpPut] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] public async Task 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"); } /// /// Delete static page, only available for coordinator /// /// Static page deleted successfully /// This action is only available for authorized internship admin /// Static page with given access name do not exist [HttpDelete("{accessName}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] public async Task 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"); } } }