From d0972673d84d91d9d0cee8ad0673754500654a62 Mon Sep 17 00:00:00 2001
From: mborzyszkowski <maciej.borzyszkowski@gmail.com>
Date: Thu, 24 Sep 2020 18:14:24 +0200
Subject: [PATCH] New static pages endpoints

---
 src/InternshipSystem.Api/ApiProfile.cs        |  2 -
 .../Controllers/StaticPagesController.cs      | 83 +++++++++++++++++++
 .../Controllers/StudentsController.cs         |  2 +-
 .../Queries/BranchOfficeForm.cs               | 10 +--
 .../Queries/StaticPageForm.cs                 | 36 ++++++++
 5 files changed, 125 insertions(+), 8 deletions(-)
 create mode 100644 src/InternshipSystem.Api/Queries/StaticPageForm.cs

diff --git a/src/InternshipSystem.Api/ApiProfile.cs b/src/InternshipSystem.Api/ApiProfile.cs
index b7c6fce..e28ca30 100644
--- a/src/InternshipSystem.Api/ApiProfile.cs
+++ b/src/InternshipSystem.Api/ApiProfile.cs
@@ -22,8 +22,6 @@ namespace InternshipSystem.Api
             CreateMap<Edition, EditionConfigurationResult>();
             
             CreateMap<EditionSubject, InternshipSubject>().IncludeMembers(es => es.Subject);
-
-            CreateMap<CurrentStudentForm, Student>();
         }
     }
 }
\ No newline at end of file
diff --git a/src/InternshipSystem.Api/Controllers/StaticPagesController.cs b/src/InternshipSystem.Api/Controllers/StaticPagesController.cs
index e819202..67fdde1 100644
--- a/src/InternshipSystem.Api/Controllers/StaticPagesController.cs
+++ b/src/InternshipSystem.Api/Controllers/StaticPagesController.cs
@@ -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");
+        }
     }
 }
\ No newline at end of file
diff --git a/src/InternshipSystem.Api/Controllers/StudentsController.cs b/src/InternshipSystem.Api/Controllers/StudentsController.cs
index 52de493..a125f84 100644
--- a/src/InternshipSystem.Api/Controllers/StudentsController.cs
+++ b/src/InternshipSystem.Api/Controllers/StudentsController.cs
@@ -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);
diff --git a/src/InternshipSystem.Api/Queries/BranchOfficeForm.cs b/src/InternshipSystem.Api/Queries/BranchOfficeForm.cs
index 75219d5..fd240bf 100644
--- a/src/InternshipSystem.Api/Queries/BranchOfficeForm.cs
+++ b/src/InternshipSystem.Api/Queries/BranchOfficeForm.cs
@@ -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);
             }
         }
diff --git a/src/InternshipSystem.Api/Queries/StaticPageForm.cs b/src/InternshipSystem.Api/Queries/StaticPageForm.cs
new file mode 100644
index 0000000..fc79b4c
--- /dev/null
+++ b/src/InternshipSystem.Api/Queries/StaticPageForm.cs
@@ -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);
+            }
+        }
+    }
+}
\ No newline at end of file
-- 
2.45.2