From 9a299b106c2a527b9e5d56538a06b2a8a91bda32 Mon Sep 17 00:00:00 2001
From: MaxchilKH <m.w.bohdanowicz@gmail.com>
Date: Sun, 18 Oct 2020 01:28:02 +0200
Subject: [PATCH] whatever

---
 .../Controllers/CompaniesController.cs        |  2 +
 .../Controllers/DocumentsController.cs        | 22 ++++++--
 .../Controllers/InternshipController.cs       | 54 +++++++++++++++++++
 .../InternshipRegistrationController.cs       | 34 +-----------
 .../Queries/DocumentPublishRequest.cs         |  5 --
 src/InternshipSystem.Api/Startup.cs           |  7 ++-
 .../Entity/Internship/Internship.cs           |  2 -
 7 files changed, 80 insertions(+), 46 deletions(-)
 create mode 100644 src/InternshipSystem.Api/Controllers/InternshipController.cs

diff --git a/src/InternshipSystem.Api/Controllers/CompaniesController.cs b/src/InternshipSystem.Api/Controllers/CompaniesController.cs
index ed374c6..312d02e 100644
--- a/src/InternshipSystem.Api/Controllers/CompaniesController.cs
+++ b/src/InternshipSystem.Api/Controllers/CompaniesController.cs
@@ -38,6 +38,7 @@ namespace InternshipSystem.Api.Controllers
         public async Task<ActionResult<IReadOnlyCollection<Company>>> SearchByNameAsync([FromQuery] CompanySearchQuery searchQuery, CancellationToken cancellationToken) =>
             await Context.Companies
                 .Where(c => c.Name.ToLower().Contains(searchQuery.Name.ToLower()))
+                .Where(c => c.Provider == 0)
                 .OrderBy(o => o.Name)
                 .Skip(searchQuery.Page * searchQuery.PerPage)
                 .Take(searchQuery.PerPage)
@@ -62,6 +63,7 @@ namespace InternshipSystem.Api.Controllers
                 .Collection(c => c.Branches)
                 .Query()
                 .Where(office => office.Address.City.ToLower().Contains(searchQuery.City.ToLower()))
+                .Where(office => office.Provider == 0)
                 .Skip(searchQuery.Page * searchQuery.PerPage)
                 .Take(searchQuery.PerPage)
                 .ToListAsync(token);
diff --git a/src/InternshipSystem.Api/Controllers/DocumentsController.cs b/src/InternshipSystem.Api/Controllers/DocumentsController.cs
index a59efb5..2f59065 100644
--- a/src/InternshipSystem.Api/Controllers/DocumentsController.cs
+++ b/src/InternshipSystem.Api/Controllers/DocumentsController.cs
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
 using InternshipSystem.Api.Queries;
 using InternshipSystem.Api.Security;
 using InternshipSystem.Api.Service;
+using InternshipSystem.Core;
 using InternshipSystem.Repository;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
@@ -47,7 +48,7 @@ namespace InternshipSystem.Api.Controllers
             CancellationToken cancellationToken)
         {
             var validator = new DocumentPublishRequest.Validator();
-            var result = await validator.ValidateAsync(documentRequest, cancellationToken);
+            var result = await validator.ValidateAsync(documentRequest, cancellationToken);     
 
             if (!result.IsValid)
             {
@@ -85,11 +86,18 @@ namespace InternshipSystem.Api.Controllers
                 await _context.Entry(edition)
                     .Collection(e => e.Internships)
                     .Query()
-                    .Include(i => i.Documentation)
                     .FirstAsync(i => i.Student.Id == user.PersonNumber, cancellationToken);
 
-            internship.UpdateDocumentScan(documentId, memoryStream.ToArray());
+            var document = await _context.Entry(internship)
+                .Collection(i => i.Documentation)
+                .Query()
+                .FirstOrDefaultAsync(d => d.Id == documentId, cancellationToken);
 
+            document.Scan = memoryStream.ToArray();
+            document.State = DocumentState.Submitted;
+            
+            await _context.SaveChangesAsync(cancellationToken);
+            
             return Ok();
         }
         
@@ -103,10 +111,14 @@ namespace InternshipSystem.Api.Controllers
                 await _context.Entry(edition)
                     .Collection(e => e.Internships)
                     .Query()
-                    .Include(i => i.Documentation)
                     .FirstAsync(i => i.Student.Id == user.PersonNumber, cancellationToken);
 
-            var document = internship.Documentation.First(d => d.Id == documentId);
+            var document =
+                await _context.Entry(internship)
+                    .Collection(i => i.Documentation)
+                    .Query()
+                    .FirstOrDefaultAsync(d => d.Id == documentId, cancellationToken);
+
             var stream = new MemoryStream(document.Scan);
             
             return File(stream, "application/pdf");
diff --git a/src/InternshipSystem.Api/Controllers/InternshipController.cs b/src/InternshipSystem.Api/Controllers/InternshipController.cs
new file mode 100644
index 0000000..e52bc44
--- /dev/null
+++ b/src/InternshipSystem.Api/Controllers/InternshipController.cs
@@ -0,0 +1,54 @@
+using System.Threading;
+using System.Threading.Tasks;
+using InternshipSystem.Api.Security;
+using InternshipSystem.Core.Entity.Internship;
+using InternshipSystem.Repository;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.EntityFrameworkCore;
+
+namespace InternshipSystem.Api.Controllers
+{
+    [ApiController]
+    [Route("internship")]
+    public class InternshipController : ControllerBase
+    {
+        private readonly InternshipDbContext _context;
+
+        public InternshipController(InternshipDbContext context)
+        {
+            _context = context;
+        }
+
+        /// <summary>
+        /// Get internship for current edition
+        /// </summary>
+        /// <response code="200">If current internship returned successfully</response>
+        /// <response code="401">This action is only available for authorized student registered for current edition</response>
+        [HttpGet]
+        [ProducesResponseType(StatusCodes.Status200OK)]
+        [ProducesResponseType(StatusCodes.Status400BadRequest)]
+        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
+        [Authorize(Policy = Policies.RegisteredOnly)]
+        public async Task<ActionResult<Internship>> GetCurrentEditionInternship([FromServices] User user, CancellationToken cancellationToken)
+        {
+            var edition = await _context.Editions
+                .FindAsync(user.EditionId);
+            
+            var internship = await _context.Entry(edition)
+                .Collection(e => e.Internships)
+                .Query()
+                .Include(i => i.Student)
+                .Include(i => i.InternshipRegistration)
+                .Include(i => i.InternshipRegistration.Company)
+                .Include(i => i.InternshipRegistration.BranchAddress)
+                .Include(i => i.InternshipRegistration.Type)
+                .Include(i => i.Report)
+                .Include(i => i.Documentation)
+                .SingleAsync(i => i.Student.Id == user.PersonNumber, cancellationToken);
+
+            return Ok(internship);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
index d2a98e9..c99b4d4 100644
--- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
+++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
@@ -7,13 +7,13 @@ using InternshipSystem.Api.Security;
 using InternshipSystem.Api.UseCases;
 using InternshipSystem.Repository;
 using Microsoft.AspNetCore.Authorization;
-using InternshipSystem.Core.Entity.Internship;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.EntityFrameworkCore;
 
 namespace InternshipSystem.Api.Controllers
 {
+    [ApiController]
     [Route("internshipRegistration")]
     public class InternshipRegistrationController : ControllerBase
     {
@@ -81,38 +81,6 @@ namespace InternshipSystem.Api.Controllers
             {
                 return BadRequest(e.Message);
             }
-
-        }
-        
-        /// <summary>
-        /// Get internship for current edition
-        /// </summary>
-        /// <response code="200">If current internship returned successfully</response>
-        /// <response code="401">This action is only available for authorized student registered for current edition</response>
-        [HttpGet]
-        [ProducesResponseType(StatusCodes.Status200OK)]
-        [ProducesResponseType(StatusCodes.Status400BadRequest)]
-        [ProducesResponseType(StatusCodes.Status401Unauthorized)]
-        [Authorize(Policy = Policies.RegisteredOnly)]
-        public async Task<ActionResult<Internship>> GetCurrentEditionInternship([FromServices] User user, CancellationToken cancellationToken)
-        {
-            var edition = await _context.Editions
-                .FindAsync(user.EditionId);
-            
-            var internship = await _context.Entry(edition)
-                .Collection(e => e.Internships)
-                .Query()
-                .Include(i => i.Student)
-                .Include(i => i.InternshipRegistration)
-                .Include(i => i.InternshipRegistration.Company)
-                .Include(i => i.InternshipRegistration.BranchAddress)
-                .Include(i => i.InternshipRegistration.Type)
-                .Include(i => i.Report)
-                .Include(i => i.Approvals)
-                .Include(i => i.Documentation)
-                .SingleAsync(i => i.Student.Id == user.PersonNumber, cancellationToken);
-
-            return Ok(internship);
         }
     }
 }
\ No newline at end of file
diff --git a/src/InternshipSystem.Api/Queries/DocumentPublishRequest.cs b/src/InternshipSystem.Api/Queries/DocumentPublishRequest.cs
index 272775a..c7c3e6d 100644
--- a/src/InternshipSystem.Api/Queries/DocumentPublishRequest.cs
+++ b/src/InternshipSystem.Api/Queries/DocumentPublishRequest.cs
@@ -6,16 +6,11 @@ namespace InternshipSystem.Api.Queries
 {
     public class DocumentPublishRequest
     {
-        public long? Id { get; set; }
         public string Description { get; set; }
         public DocumentType Type { get; set; }
         
         public class Validator : AbstractValidator<DocumentPublishRequest>
         {
-            public Validator()
-            {
-                RuleFor(document => document.Type).NotEmpty();
-            }
         }
     }
 }
\ No newline at end of file
diff --git a/src/InternshipSystem.Api/Startup.cs b/src/InternshipSystem.Api/Startup.cs
index 2eddcfe..94246e8 100644
--- a/src/InternshipSystem.Api/Startup.cs
+++ b/src/InternshipSystem.Api/Startup.cs
@@ -17,6 +17,7 @@ using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
 using Microsoft.OpenApi.Models;
 using Newtonsoft.Json;
+using Newtonsoft.Json.Converters;
 
 namespace InternshipSystem.Api
 {
@@ -57,7 +58,11 @@ namespace InternshipSystem.Api
                     options.IncludeXmlComments(xmlPath);
                 })
                 .AddControllers(o => { o.ModelBinderProviders.Insert(0, new UserBinderProvider()); })
-                .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
+                .AddNewtonsoftJson(options =>
+                {
+                    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
+                    options.SerializerSettings.Converters.Add(new StringEnumConverter());
+                });
         }
 
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
diff --git a/src/InternshipSystem.Core/Entity/Internship/Internship.cs b/src/InternshipSystem.Core/Entity/Internship/Internship.cs
index 8251873..91ff865 100644
--- a/src/InternshipSystem.Core/Entity/Internship/Internship.cs
+++ b/src/InternshipSystem.Core/Entity/Internship/Internship.cs
@@ -10,7 +10,6 @@ namespace InternshipSystem.Core.Entity.Internship
         public Student Student { get; set; }
         public InternshipRegistration InternshipRegistration { get; set; }
         public Report Report { get; set; }
-        public List<Document> Approvals { get; set; }
         public List<Document> Documentation { get; set; }
 
         public Edition Edition { get; set; }
@@ -25,7 +24,6 @@ namespace InternshipSystem.Core.Entity.Internship
 
             internship.InternshipRegistration = InternshipRegistration.Create();
             internship.Report = Report.Create();
-            internship.Approvals = new List<Document>();
             internship.Documentation = new List<Document>();
 
             return internship;