Merge pull request 'New static pages endpoints' (#43) from MoreEndpoints into master

This commit is contained in:
Zonar 2020-09-24 18:15:37 +02:00
commit 6d6cf68fe2
5 changed files with 125 additions and 8 deletions

View File

@ -22,8 +22,6 @@ namespace InternshipSystem.Api
CreateMap<Edition, EditionConfigurationResult>();
CreateMap<EditionSubject, InternshipSubject>().IncludeMembers(es => es.Subject);
CreateMap<CurrentStudentForm, Student>();
}
}
}

View File

@ -2,8 +2,10 @@
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;
@ -52,5 +54,86 @@ namespace InternshipSystem.Api.Controllers
return Ok(page);
}
[HttpPut]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[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 (!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)
{
return BadRequest($"Static page with access name: {staticPageForm.AccessName} already exist");
}
}
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");
}
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 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");
}
[HttpDelete("{accessName}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[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");
}
}
}

View File

@ -74,7 +74,7 @@ namespace InternshipSystem.Api.Controllers
if (!validationResult.IsValid)
{
return BadRequest(validationResult);
return BadRequest(validationResult.ToString());
}
var currentStudent = await _context.Students.FindAsync(studentNewData.Id);

View File

@ -20,15 +20,15 @@ namespace InternshipSystem.Api.Queries
string.IsNullOrEmpty(c.Country) || string.IsNullOrEmpty(c.City) ||
string.IsNullOrEmpty(c.PostalCode) || string.IsNullOrEmpty(c.Street) ||
string.IsNullOrEmpty(c.Building));
RuleFor(b => b.Country).NotNull()
RuleFor(b => b.Country).NotEmpty()
.When(b => !b.Id.HasValue);
RuleFor(b => b.City).NotNull()
RuleFor(b => b.City).NotEmpty()
.When(b => !b.Id.HasValue);
RuleFor(b => b.PostalCode).NotNull()
RuleFor(b => b.PostalCode).NotEmpty()
.When(b => !b.Id.HasValue);
RuleFor(b => b.Street).NotNull()
RuleFor(b => b.Street).NotEmpty()
.When(b => !b.Id.HasValue);
RuleFor(b => b.Building).NotNull()
RuleFor(b => b.Building).NotEmpty()
.When(b => !b.Id.HasValue);
}
}

View File

@ -0,0 +1,36 @@
using FluentValidation;
namespace InternshipSystem.Api.Queries
{
public class StaticPageForm
{
public long? Id { get; set; }
public string AccessName { get; set; }
public string Title { get; set; }
public string TitleEng { get; set; }
public string Content { get; set; }
public string ContentEng { get; set; }
public class Validator : AbstractValidator<StaticPageForm>
{
public Validator()
{
RuleFor(sp => sp.Id).NotNull()
.When(sp =>
string.IsNullOrEmpty(sp.AccessName) || string.IsNullOrEmpty(sp.Title) ||
string.IsNullOrEmpty(sp.TitleEng) || string.IsNullOrEmpty(sp.Content) ||
string.IsNullOrEmpty(sp.ContentEng));
RuleFor(sp => sp.AccessName).NotEmpty()
.When(sp => !sp.Id.HasValue);
RuleFor(sp => sp.Title).NotEmpty()
.When(sp => !sp.Id.HasValue);
RuleFor(sp => sp.TitleEng).NotEmpty()
.When(sp => !sp.Id.HasValue);
RuleFor(sp => sp.Content).NotEmpty()
.When(sp => !sp.Id.HasValue);
RuleFor(sp => sp.ContentEng).NotEmpty()
.When(sp => !sp.Id.HasValue);
}
}
}
}