From b846d0613950d4e3afc577aae3c356d9a21a68c5 Mon Sep 17 00:00:00 2001
From: MaxchilKH <m.w.bohdanowicz@gmail.com>
Date: Sun, 18 Oct 2020 10:23:26 +0200
Subject: [PATCH] make proper result type for update

---
 .../InternshipRegistrationController.cs       |  4 ++--
 .../UpdateInternshipRegistrationUseCase.cs    |  2 +-
 src/InternshipSystem.Core/Entity/Company.cs   |  8 ++++++--
 .../Internship/InternshipRegistration.cs      | 20 +++++++++++++------
 .../ValueObject/BranchAddress.cs              | 15 +++++++++-----
 .../ValueObject/Mentor.cs                     | 12 +++++++----
 6 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
index c99b4d4..6032c1b 100644
--- a/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
+++ b/src/InternshipSystem.Api/Controllers/InternshipRegistrationController.cs
@@ -71,11 +71,11 @@ namespace InternshipSystem.Api.Controllers
 
             try
             {
-                var result = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken);
+                var (status, result) = await useCase.UpdateInternshipRegistration(registrationCommand, cancellationToken);
             
                 await _context.SaveChangesAsync(cancellationToken);
 
-                return Ok(result);
+                return new JsonResult(new {Status = status, Errors = result});
             }
             catch (ArgumentException e)
             {
diff --git a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs
index a490cf4..726ec00 100644
--- a/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs
+++ b/src/InternshipSystem.Api/UseCases/UpdateInternshipRegistrationUseCase.cs
@@ -30,7 +30,7 @@ namespace InternshipSystem.Api.UseCases
             subjectRegistration = internshipRegistration;
         }
 
-        public async Task<string> UpdateInternshipRegistration(
+        public async Task<(DocumentState State, IEnumerable<string>)> UpdateInternshipRegistration(
             UpdateRegistrationForm registrationCommand,
             CancellationToken cancellationToken)
         {
diff --git a/src/InternshipSystem.Core/Entity/Company.cs b/src/InternshipSystem.Core/Entity/Company.cs
index a34d9da..bd16a25 100644
--- a/src/InternshipSystem.Core/Entity/Company.cs
+++ b/src/InternshipSystem.Core/Entity/Company.cs
@@ -31,8 +31,12 @@ namespace InternshipSystem.Core
         {
             public Validator()
             {
-                RuleFor(x => x.Nip).NotNull();
-                RuleFor(x => x.Name).NotNull();
+                RuleFor(x => x.Nip)
+                    .NotEmpty()
+                    .WithMessage("error.company.nip.empty");
+                RuleFor(x => x.Name)
+                    .NotEmpty()
+                    .WithMessage("error.company.name.empty");
             }
         }
     }
diff --git a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs
index d220a42..595ab71 100644
--- a/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs
+++ b/src/InternshipSystem.Core/Entity/Internship/InternshipRegistration.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Data;
 using System.Linq;
 using FluentValidation;
 using InternshipSystem.Core.UglyOrmArtifacts;
@@ -26,7 +27,7 @@ namespace InternshipSystem.Core.Entity.Internship
         }
 
 
-        public string ValidateStatus(Edition edition)
+        public (DocumentState State, IEnumerable<string>) ValidateStatus(Edition edition)
         {
             var validator = new Validator(edition);
 
@@ -34,7 +35,7 @@ namespace InternshipSystem.Core.Entity.Internship
 
             State = result.IsValid ? DocumentState.Submitted : DocumentState.Draft;
 
-            return result.ToString();
+            return (State, result.Errors.Select(failure => failure.ToString()));
         }
 
         public class Validator : AbstractValidator<InternshipRegistration>
@@ -52,18 +53,25 @@ namespace InternshipSystem.Core.Entity.Internship
                     .NotNull();
                 RuleFor(x => x.Subjects)
                     .NotEmpty()
-                    .Must(s => edition.AreSubjectsAvailable(s.Select(su => su.InternshipSubjectId)));
+                    .Must(s => edition.AreSubjectsAvailable(s.Select(su => su.InternshipSubjectId)))
+                    .WithMessage("error.subjects.not_available");
                 RuleFor(x => x.Type)
                     .NotNull()
-                    .Must(edition.IsTypeAvailable);
+                    .Must(edition.IsTypeAvailable)
+                    .WithMessage("error.type.not_available");
                 RuleFor(x => x.Start)
                     .GreaterThanOrEqualTo(edition.EditionStart)
                     .LessThan(x => x.End)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.start_date.empty");
                 RuleFor(x => x.End)
                     .LessThanOrEqualTo(edition.EditionFinish)
                     .GreaterThan(x => x.Start)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.end_date.empty");
+                RuleFor(x => x.DeclaredHours)
+                    .NotEmpty()
+                    .WithMessage("error.declared_hours.empty");
             }
         }
     }
diff --git a/src/InternshipSystem.Core/ValueObject/BranchAddress.cs b/src/InternshipSystem.Core/ValueObject/BranchAddress.cs
index f3be361..9e923a6 100644
--- a/src/InternshipSystem.Core/ValueObject/BranchAddress.cs
+++ b/src/InternshipSystem.Core/ValueObject/BranchAddress.cs
@@ -16,15 +16,20 @@ namespace InternshipSystem.Core
             public Validator()
             {
                 RuleFor(x => x.Country)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.branch.country.empty");
                 RuleFor(x => x.City)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.branch.city.empty");
                 RuleFor(x => x.PostalCode)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.branch.postal_code.empty");
                 RuleFor(x => x.Street)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.branch.street.empty");
                 RuleFor(x => x.Building)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.branch.building.empty");
             }
         }
     }
diff --git a/src/InternshipSystem.Core/ValueObject/Mentor.cs b/src/InternshipSystem.Core/ValueObject/Mentor.cs
index bae5499..d0363f5 100644
--- a/src/InternshipSystem.Core/ValueObject/Mentor.cs
+++ b/src/InternshipSystem.Core/ValueObject/Mentor.cs
@@ -22,13 +22,17 @@ namespace InternshipSystem.Core
             public Validate()
             {
                 RuleFor(x => x.FirstName)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.mentor.first_name.empty");
                 RuleFor(x => x.LastName)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.mentor.last_name.empty");
                 RuleFor(x => x.Email)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.mentor.email.empty");
                 RuleFor(x => x.PhoneNumber)
-                    .NotEmpty();
+                    .NotEmpty()
+                    .WithMessage("error.mentor.phone_number.empty");
                 
             }
         }
-- 
2.45.2