From 20e79380552b95a939fd7e61699ea91c64152f9c Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Thu, 22 Apr 2021 11:35:14 +0200 Subject: [PATCH 01/18] [csharp] [courses_steps_csv] Kickstart base Executed command: `make examples/csharp/create ExampleName=csharp-courses_steps_csv-01_base ContextName=CoursesStepsCsv` --- .../csharp-courses_steps_csv-01_base.sln | 56 +++++++++++++++++++ .../src/CoursesStepsCsv/Class1.cs | 8 +++ .../CoursesStepsCsv/CoursesStepsCsv.csproj | 7 +++ .../CoursesStepsCsv.Tests.csproj | 22 ++++++++ .../test/CoursesStepsCsv.Tests/UnitTest1.cs | 14 +++++ 5 files changed, 107 insertions(+) create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/csharp-courses_steps_csv-01_base.sln create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CoursesStepsCsv.csproj create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/csharp-courses_steps_csv-01_base.sln b/examples/csharp/csharp-courses_steps_csv-01_base/csharp-courses_steps_csv-01_base.sln new file mode 100644 index 00000000..56a83fec --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/csharp-courses_steps_csv-01_base.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.6.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8B7D4918-2C37-4F16-B355-8EF19B1E23F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv", "src\CoursesStepsCsv\CoursesStepsCsv.csproj", "{C6E9F888-08F3-4D4B-9AEB-919F615C988F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C8A13F5C-212F-4163-885A-01C093B26450}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv.Tests", "test\CoursesStepsCsv.Tests\CoursesStepsCsv.Tests.csproj", "{C6D93799-5C86-410F-A957-1A531A757DAE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F} = {8B7D4918-2C37-4F16-B355-8EF19B1E23F8} + {C6D93799-5C86-410F-A957-1A531A757DAE} = {C8A13F5C-212F-4163-885A-01C093B26450} + EndGlobalSection +EndGlobal diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs new file mode 100644 index 00000000..db65748d --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs @@ -0,0 +1,8 @@ +using System; + +namespace CoursesStepsCsv +{ + public class Class1 + { + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CoursesStepsCsv.csproj b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CoursesStepsCsv.csproj new file mode 100644 index 00000000..f208d303 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CoursesStepsCsv.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj new file mode 100644 index 00000000..c0dc1ace --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -0,0 +1,22 @@ + + + + net5.0 + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs new file mode 100644 index 00000000..c9427e29 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs @@ -0,0 +1,14 @@ +using System; +using Xunit; + +namespace CoursesStepsCsv.Tests +{ + public class UnitTest1 + { + [Fact] + public void Test1() + { + + } + } +} From 60ae8c819b749f931a6a49620f89a7da82e1fc26 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Thu, 22 Apr 2021 11:46:06 +0200 Subject: [PATCH 02/18] =?UTF-8?q?[csharp]=20[courses=5Fsteps=5Fcsv]=20Test?= =?UTF-8?q?=20execution=20works=20=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/CoursesStepsCsv/Class1.cs | 8 -------- .../CoursesStepsCsv/CourseStepsGetController.cs | 12 ++++++++++++ .../CourseStepsGetControllerShould.cs | 16 ++++++++++++++++ .../CoursesStepsCsv.Tests.csproj | 5 +++++ .../test/CoursesStepsCsv.Tests/UnitTest1.cs | 14 -------------- 5 files changed, 33 insertions(+), 22 deletions(-) delete mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs delete mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs deleted file mode 100644 index db65748d..00000000 --- a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace CoursesStepsCsv -{ - public class Class1 - { - } -} diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs new file mode 100644 index 00000000..6c76e6a1 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -0,0 +1,12 @@ +using System; + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CourseStepsGetController + { + public string Lerele() + { + return "lerele"; + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs new file mode 100644 index 00000000..245b734f --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -0,0 +1,16 @@ +using System; +using Xunit; + +namespace CodelyTv.CoursesStepsCsv.Tests +{ + public class CourseStepsGetControllerShould + { + [Fact] + public void ReturnAString() + { + var courseStepsGetController = new CourseStepsGetController(); + + Assert.Equal("lerele", courseStepsGetController.Lerele()); + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj index c0dc1ace..7563be8b 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -2,6 +2,7 @@ net5.0 + CodelyTv.CoursesStepsCsv.Tests false @@ -19,4 +20,8 @@ + + + + diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs deleted file mode 100644 index c9427e29..00000000 --- a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Xunit; - -namespace CoursesStepsCsv.Tests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - - } - } -} From 22297c683070e3c340021805762ac58061bfe3d2 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Thu, 22 Apr 2021 12:15:24 +0200 Subject: [PATCH 03/18] =?UTF-8?q?[csharp]=20[courses=5Fsteps=5Fcsv]=20Test?= =?UTF-8?q?=20execution=20with=20mock=20works=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Command to execute tests: `dotnet test` --- .../CourseStepsGetController.cs | 11 ++++++-- .../src/CoursesStepsCsv/Platform.cs | 9 +++++++ .../CourseStepsGetControllerShould.cs | 25 ++++++++++++++++--- .../CoursesStepsCsv.Tests.csproj | 1 + 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Platform.cs diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs index 6c76e6a1..04df909a 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -4,9 +4,16 @@ namespace CodelyTv.CoursesStepsCsv { public sealed class CourseStepsGetController { - public string Lerele() + private readonly Platform platform; + + public CourseStepsGetController(Platform platform) + { + this.platform = platform; + } + + public string Get(string courseId) { - return "lerele"; + return platform.FindCourseSteps(courseId); } } } diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Platform.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Platform.cs new file mode 100644 index 00000000..e9ef3120 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Platform.cs @@ -0,0 +1,9 @@ +using System; + +namespace CodelyTv.CoursesStepsCsv +{ + public interface Platform + { + string FindCourseSteps(string courseId); + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs index 245b734f..5b239f82 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -1,16 +1,35 @@ using System; using Xunit; +using Moq; namespace CodelyTv.CoursesStepsCsv.Tests { public class CourseStepsGetControllerShould { + private readonly Mock platform; + private readonly CourseStepsGetController courseStepsGetController; + public CourseStepsGetControllerShould() + { + platform = new Mock(); + courseStepsGetController = new CourseStepsGetController(platform.Object); + } + [Fact] - public void ReturnAString() + public void ReturnEmptyStepList() { - var courseStepsGetController = new CourseStepsGetController(); + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var emptyCsv = ""; + + GivenPlatformReturnsCourseStepCsv(courseId, emptyCsv); - Assert.Equal("lerele", courseStepsGetController.Lerele()); + var actualCourseSteps = courseStepsGetController.Get(courseId); + + Assert.Equal("", actualCourseSteps); + } + + private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv) + { + platform.Setup(x => x.FindCourseSteps(courseId)).Returns(csv); } } } diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj index 7563be8b..528458bd 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -18,6 +18,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + From f96cfbee9bb195396fb887218483746e5c5c46d7 Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 12:12:45 +0200 Subject: [PATCH 04/18] [csharp] [courses_steps_csv] Implement 01 base --- .../CourseStepsGetController.cs | 65 ++++++++++++++++++- .../src/CoursesStepsCsv/Steps.cs | 11 ++++ .../CourseStepsGetControllerShould.cs | 30 ++++++++- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs index 04df909a..982f73b2 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -1,4 +1,8 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using Microsoft.VisualBasic.FileIO; namespace CodelyTv.CoursesStepsCsv { @@ -13,7 +17,66 @@ public CourseStepsGetController(Platform platform) public string Get(string courseId) { - return platform.FindCourseSteps(courseId); + var csv = platform.FindCourseSteps(courseId); + + var results = "["; + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var type = row[1]; + var duration = 0.0; + var points = 0.0; + + if (type == "video") + { + duration = int.Parse(row[3]) * 1.1; + } + + if (type == "quiz") + { + duration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question + } + + if (type != "video" && type != "quiz") + { + continue; + } + + if (type == "video") + { + points = int.Parse(row[3]) * 1.1 * 100; + } + + if (type == "quiz") + { + points = int.Parse(row[2]) * 0.5 * 10; + } + + var step = new Step + { + Id = row[0], + Type = row[1], + Duration = duration, + Points = points + }; + + results += JsonSerializer.Serialize(step, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + + if (i != lines.Length - 1) + { + results += ","; + } + } + results += "]"; + + return results; } } } diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs new file mode 100644 index 00000000..77a4b962 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs @@ -0,0 +1,11 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class Step + { + public string Id { get; set; } + public string Type { get; set; } + public double Duration { get; set; } + public double Points { get; set; } + } +} \ No newline at end of file diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs index 5b239f82..2354645b 100644 --- a/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs +++ b/examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -24,7 +24,35 @@ public void ReturnEmptyStepList() var actualCourseSteps = courseStepsGetController.Get(courseId); - Assert.Equal("", actualCourseSteps); + Assert.Equal("[]", actualCourseSteps); + } + + [Fact] + public void ReturnExistingCourseSteps() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = String.Join( + Environment.NewLine, + "1,video,,13", + "2,quiz,5,"); + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + var expected = "[{\"id\":\"1\",\"type\":\"video\",\"duration\":14.3,\"points\":1430},{\"id\":\"2\",\"type\":\"quiz\",\"duration\":2.5,\"points\":25}]"; + Assert.Equal(expected, results); + } + + [Fact] + public void IgnoreStepsWithInvalidType() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = "1,survey,,13"; + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", results); } private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv) From d881a3af9046896c9d821f94fdf39d91688db166 Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 12:21:03 +0200 Subject: [PATCH 05/18] [csharp] [courses_steps_csv] Add 02 semantics --- .../csharp-courses_steps_csv-02_semantics.sln | 56 +++++++++++++ .../CourseStepsGetController.cs | 82 +++++++++++++++++++ .../CoursesStepsCsv/CoursesStepsCsv.csproj | 7 ++ .../src/CoursesStepsCsv/Platform.cs | 9 ++ .../src/CoursesStepsCsv/Steps.cs | 11 +++ .../CourseStepsGetControllerShould.cs | 63 ++++++++++++++ .../CoursesStepsCsv.Tests.csproj | 28 +++++++ 7 files changed, 256 insertions(+) create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/csharp-courses_steps_csv-02_semantics.sln create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CoursesStepsCsv.csproj create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Platform.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/csharp-courses_steps_csv-02_semantics.sln b/examples/csharp/csharp-courses_steps_csv-02_semantics/csharp-courses_steps_csv-02_semantics.sln new file mode 100644 index 00000000..56a83fec --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/csharp-courses_steps_csv-02_semantics.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.6.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8B7D4918-2C37-4F16-B355-8EF19B1E23F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv", "src\CoursesStepsCsv\CoursesStepsCsv.csproj", "{C6E9F888-08F3-4D4B-9AEB-919F615C988F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C8A13F5C-212F-4163-885A-01C093B26450}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv.Tests", "test\CoursesStepsCsv.Tests\CoursesStepsCsv.Tests.csproj", "{C6D93799-5C86-410F-A957-1A531A757DAE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F} = {8B7D4918-2C37-4F16-B355-8EF19B1E23F8} + {C6D93799-5C86-410F-A957-1A531A757DAE} = {C8A13F5C-212F-4163-885A-01C093B26450} + EndGlobalSection +EndGlobal diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs new file mode 100644 index 00000000..982f73b2 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using Microsoft.VisualBasic.FileIO; + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CourseStepsGetController + { + private readonly Platform platform; + + public CourseStepsGetController(Platform platform) + { + this.platform = platform; + } + + public string Get(string courseId) + { + var csv = platform.FindCourseSteps(courseId); + + var results = "["; + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var type = row[1]; + var duration = 0.0; + var points = 0.0; + + if (type == "video") + { + duration = int.Parse(row[3]) * 1.1; + } + + if (type == "quiz") + { + duration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question + } + + if (type != "video" && type != "quiz") + { + continue; + } + + if (type == "video") + { + points = int.Parse(row[3]) * 1.1 * 100; + } + + if (type == "quiz") + { + points = int.Parse(row[2]) * 0.5 * 10; + } + + var step = new Step + { + Id = row[0], + Type = row[1], + Duration = duration, + Points = points + }; + + results += JsonSerializer.Serialize(step, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + + if (i != lines.Length - 1) + { + results += ","; + } + } + results += "]"; + + return results; + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CoursesStepsCsv.csproj b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CoursesStepsCsv.csproj new file mode 100644 index 00000000..f208d303 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CoursesStepsCsv.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Platform.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Platform.cs new file mode 100644 index 00000000..e9ef3120 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Platform.cs @@ -0,0 +1,9 @@ +using System; + +namespace CodelyTv.CoursesStepsCsv +{ + public interface Platform + { + string FindCourseSteps(string courseId); + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs new file mode 100644 index 00000000..77a4b962 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs @@ -0,0 +1,11 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class Step + { + public string Id { get; set; } + public string Type { get; set; } + public double Duration { get; set; } + public double Points { get; set; } + } +} \ No newline at end of file diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs new file mode 100644 index 00000000..2354645b --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -0,0 +1,63 @@ +using System; +using Xunit; +using Moq; + +namespace CodelyTv.CoursesStepsCsv.Tests +{ + public class CourseStepsGetControllerShould + { + private readonly Mock platform; + private readonly CourseStepsGetController courseStepsGetController; + public CourseStepsGetControllerShould() + { + platform = new Mock(); + courseStepsGetController = new CourseStepsGetController(platform.Object); + } + + [Fact] + public void ReturnEmptyStepList() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var emptyCsv = ""; + + GivenPlatformReturnsCourseStepCsv(courseId, emptyCsv); + + var actualCourseSteps = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", actualCourseSteps); + } + + [Fact] + public void ReturnExistingCourseSteps() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = String.Join( + Environment.NewLine, + "1,video,,13", + "2,quiz,5,"); + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + var expected = "[{\"id\":\"1\",\"type\":\"video\",\"duration\":14.3,\"points\":1430},{\"id\":\"2\",\"type\":\"quiz\",\"duration\":2.5,\"points\":25}]"; + Assert.Equal(expected, results); + } + + [Fact] + public void IgnoreStepsWithInvalidType() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = "1,survey,,13"; + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", results); + } + + private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv) + { + platform.Setup(x => x.FindCourseSteps(courseId)).Returns(csv); + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj new file mode 100644 index 00000000..528458bd --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -0,0 +1,28 @@ + + + + net5.0 + CodelyTv.CoursesStepsCsv.Tests + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + From 127aab0db82c2e5ad9cba540a32f76eab67762e1 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 12:45:01 +0200 Subject: [PATCH 06/18] [csharp] [courses_steps_csv] Provide semantics to video step type --- .../src/CoursesStepsCsv/CourseStepsGetController.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs index 982f73b2..c749d189 100644 --- a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -8,6 +8,8 @@ namespace CodelyTv.CoursesStepsCsv { public sealed class CourseStepsGetController { + private const double VIDEO_DURATION_PAUSES_MULTIPLIER = 1.1; + private readonly Platform platform; public CourseStepsGetController(Platform platform) @@ -28,17 +30,18 @@ public string Get(string courseId) var row = lines[i].Split(','); var type = row[1]; - var duration = 0.0; + var stepDuration = 0.0; var points = 0.0; if (type == "video") { - duration = int.Parse(row[3]) * 1.1; + var videoDuration = int.Parse(row[3]); + stepDuration = videoDuration * VIDEO_DURATION_PAUSES_MULTIPLIER; } if (type == "quiz") { - duration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question + stepDuration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question } if (type != "video" && type != "quiz") @@ -48,7 +51,7 @@ public string Get(string courseId) if (type == "video") { - points = int.Parse(row[3]) * 1.1 * 100; + points = stepDuration * 100; } if (type == "quiz") @@ -60,7 +63,7 @@ public string Get(string courseId) { Id = row[0], Type = row[1], - Duration = duration, + Duration = stepDuration, Points = points }; From a34e08dfbc186d55601bc1c375959b6b6cb99877 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:07:58 +0200 Subject: [PATCH 07/18] [csharp] [courses_steps_csv] Provide semantics to the overall CourseStepsGetController --- .../CourseStepsGetController.cs | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs index c749d189..a6f222a5 100644 --- a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -9,7 +9,11 @@ namespace CodelyTv.CoursesStepsCsv public sealed class CourseStepsGetController { private const double VIDEO_DURATION_PAUSES_MULTIPLIER = 1.1; - + private const double QUIZ_TIME_PER_QUESTION_MULTIPLIER = 0.5; + private const int QUIZ_POINTS_PER_MINUTE = 10; + private const string STEP_TYPE_QUIZ = "quiz"; + private const string STEP_TYPE_VIDEO = "video"; + private const int VIDEO_POINTS_PER_MINUTE = 100; private readonly Platform platform; public CourseStepsGetController(Platform platform) @@ -29,41 +33,44 @@ public string Get(string courseId) { var row = lines[i].Split(','); + var id = row[0]; var type = row[1]; - var stepDuration = 0.0; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + var stepDurationInMinutes = 0.0; var points = 0.0; - if (type == "video") + if (type == STEP_TYPE_VIDEO) { - var videoDuration = int.Parse(row[3]); - stepDuration = videoDuration * VIDEO_DURATION_PAUSES_MULTIPLIER; + stepDurationInMinutes = videoDuration.Value * VIDEO_DURATION_PAUSES_MULTIPLIER; } - if (type == "quiz") + if (type == STEP_TYPE_QUIZ) { - stepDuration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question + stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; } - if (type != "video" && type != "quiz") + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) { continue; } - if (type == "video") + if (type == STEP_TYPE_VIDEO) { - points = stepDuration * 100; + points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; } - if (type == "quiz") + if (type == STEP_TYPE_QUIZ) { - points = int.Parse(row[2]) * 0.5 * 10; + points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; } var step = new Step { - Id = row[0], - Type = row[1], - Duration = stepDuration, + Id = id, + Type = type, + Duration = stepDurationInMinutes, Points = points }; From 76559e1b16d983f0d53287d550c86e818affbdee Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:10:01 +0200 Subject: [PATCH 08/18] [csharp] [courses_steps_csv] [03_split_parsing_phase] Add base code ready to be refactored --- ...urses_steps_csv-03_split_parsing_phase.sln | 56 +++++++++++ .../CourseStepsGetController.cs | 92 +++++++++++++++++++ .../CoursesStepsCsv/CoursesStepsCsv.csproj | 7 ++ .../src/CoursesStepsCsv/Platform.cs | 9 ++ .../src/CoursesStepsCsv/Steps.cs | 11 +++ .../CourseStepsGetControllerShould.cs | 63 +++++++++++++ .../CoursesStepsCsv.Tests.csproj | 28 ++++++ 7 files changed, 266 insertions(+) create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/csharp-courses_steps_csv-03_split_parsing_phase.sln create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Platform.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/csharp-courses_steps_csv-03_split_parsing_phase.sln b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/csharp-courses_steps_csv-03_split_parsing_phase.sln new file mode 100644 index 00000000..56a83fec --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/csharp-courses_steps_csv-03_split_parsing_phase.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.6.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8B7D4918-2C37-4F16-B355-8EF19B1E23F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv", "src\CoursesStepsCsv\CoursesStepsCsv.csproj", "{C6E9F888-08F3-4D4B-9AEB-919F615C988F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C8A13F5C-212F-4163-885A-01C093B26450}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv.Tests", "test\CoursesStepsCsv.Tests\CoursesStepsCsv.Tests.csproj", "{C6D93799-5C86-410F-A957-1A531A757DAE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F} = {8B7D4918-2C37-4F16-B355-8EF19B1E23F8} + {C6D93799-5C86-410F-A957-1A531A757DAE} = {C8A13F5C-212F-4163-885A-01C093B26450} + EndGlobalSection +EndGlobal diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs new file mode 100644 index 00000000..a6f222a5 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using Microsoft.VisualBasic.FileIO; + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CourseStepsGetController + { + private const double VIDEO_DURATION_PAUSES_MULTIPLIER = 1.1; + private const double QUIZ_TIME_PER_QUESTION_MULTIPLIER = 0.5; + private const int QUIZ_POINTS_PER_MINUTE = 10; + private const string STEP_TYPE_QUIZ = "quiz"; + private const string STEP_TYPE_VIDEO = "video"; + private const int VIDEO_POINTS_PER_MINUTE = 100; + private readonly Platform platform; + + public CourseStepsGetController(Platform platform) + { + this.platform = platform; + } + + public string Get(string courseId) + { + var csv = platform.FindCourseSteps(courseId); + + var results = "["; + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var id = row[0]; + var type = row[1]; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + var stepDurationInMinutes = 0.0; + var points = 0.0; + + if (type == STEP_TYPE_VIDEO) + { + stepDurationInMinutes = videoDuration.Value * VIDEO_DURATION_PAUSES_MULTIPLIER; + } + + if (type == STEP_TYPE_QUIZ) + { + stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; + } + + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) + { + continue; + } + + if (type == STEP_TYPE_VIDEO) + { + points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; + } + + if (type == STEP_TYPE_QUIZ) + { + points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; + } + + var step = new Step + { + Id = id, + Type = type, + Duration = stepDurationInMinutes, + Points = points + }; + + results += JsonSerializer.Serialize(step, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + + if (i != lines.Length - 1) + { + results += ","; + } + } + results += "]"; + + return results; + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj new file mode 100644 index 00000000..f208d303 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Platform.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Platform.cs new file mode 100644 index 00000000..e9ef3120 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Platform.cs @@ -0,0 +1,9 @@ +using System; + +namespace CodelyTv.CoursesStepsCsv +{ + public interface Platform + { + string FindCourseSteps(string courseId); + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs new file mode 100644 index 00000000..77a4b962 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs @@ -0,0 +1,11 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class Step + { + public string Id { get; set; } + public string Type { get; set; } + public double Duration { get; set; } + public double Points { get; set; } + } +} \ No newline at end of file diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs new file mode 100644 index 00000000..2354645b --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -0,0 +1,63 @@ +using System; +using Xunit; +using Moq; + +namespace CodelyTv.CoursesStepsCsv.Tests +{ + public class CourseStepsGetControllerShould + { + private readonly Mock platform; + private readonly CourseStepsGetController courseStepsGetController; + public CourseStepsGetControllerShould() + { + platform = new Mock(); + courseStepsGetController = new CourseStepsGetController(platform.Object); + } + + [Fact] + public void ReturnEmptyStepList() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var emptyCsv = ""; + + GivenPlatformReturnsCourseStepCsv(courseId, emptyCsv); + + var actualCourseSteps = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", actualCourseSteps); + } + + [Fact] + public void ReturnExistingCourseSteps() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = String.Join( + Environment.NewLine, + "1,video,,13", + "2,quiz,5,"); + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + var expected = "[{\"id\":\"1\",\"type\":\"video\",\"duration\":14.3,\"points\":1430},{\"id\":\"2\",\"type\":\"quiz\",\"duration\":2.5,\"points\":25}]"; + Assert.Equal(expected, results); + } + + [Fact] + public void IgnoreStepsWithInvalidType() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = "1,survey,,13"; + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", results); + } + + private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv) + { + platform.Setup(x => x.FindCourseSteps(courseId)).Returns(csv); + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj new file mode 100644 index 00000000..528458bd --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -0,0 +1,28 @@ + + + + net5.0 + CodelyTv.CoursesStepsCsv.Tests + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + From b5c5410c7c6243ec21cdffc2a98855d4d2b6cc07 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:15:10 +0200 Subject: [PATCH 09/18] [csharp] [courses_steps_csv] [03_split_parsing_phase] Duplicate loop in order to allow parallel change refactoring technic --- .../CourseStepsGetController.cs | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index a6f222a5..76f7c6bb 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -86,6 +86,67 @@ public string Get(string courseId) } results += "]"; + results = "["; + + lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var id = row[0]; + var type = row[1]; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + var stepDurationInMinutes = 0.0; + var points = 0.0; + + if (type == STEP_TYPE_VIDEO) + { + stepDurationInMinutes = videoDuration.Value * VIDEO_DURATION_PAUSES_MULTIPLIER; + } + + if (type == STEP_TYPE_QUIZ) + { + stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; + } + + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) + { + continue; + } + + if (type == STEP_TYPE_VIDEO) + { + points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; + } + + if (type == STEP_TYPE_QUIZ) + { + points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; + } + + var step = new Step + { + Id = id, + Type = type, + Duration = stepDurationInMinutes, + Points = points + }; + + results += JsonSerializer.Serialize(step, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + + if (i != lines.Length - 1) + { + results += ","; + } + } + results += "]"; + return results; } } From 844007c53279973a9c22e4aa019dc550d2a8ecde Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:17:15 +0200 Subject: [PATCH 10/18] [csharp] [courses_steps_csv] Fix mini-typo in file name --- .../src/CoursesStepsCsv/{Steps.cs => Step.cs} | 0 .../src/CoursesStepsCsv/{Steps.cs => Step.cs} | 0 .../src/CoursesStepsCsv/{Steps.cs => Step.cs} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/{Steps.cs => Step.cs} (100%) rename examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/{Steps.cs => Step.cs} (100%) rename examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/{Steps.cs => Step.cs} (100%) diff --git a/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Step.cs similarity index 100% rename from examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Steps.cs rename to examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/Step.cs diff --git a/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Step.cs similarity index 100% rename from examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Steps.cs rename to examples/csharp/csharp-courses_steps_csv-02_semantics/src/CoursesStepsCsv/Step.cs diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs similarity index 100% rename from examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Steps.cs rename to examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs From 8508df15515c7b084fe3243c18eb6cf7124e9a7c Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:27:09 +0200 Subject: [PATCH 11/18] [csharp] [courses_steps_csv] [03_split_parsing_phase] Clean out the first loop only leaving the CSV parsing behaviour --- .../CourseStepsGetController.cs | 45 ++----------------- .../src/CoursesStepsCsv/CsvStep.cs | 19 ++++++++ 2 files changed, 23 insertions(+), 41 deletions(-) create mode 100644 examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CsvStep.cs diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 76f7c6bb..ad0ed035 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -25,7 +25,7 @@ public string Get(string courseId) { var csv = platform.FindCourseSteps(courseId); - var results = "["; + var csvSteps = new List(); var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); @@ -38,55 +38,18 @@ public string Get(string courseId) int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); - var stepDurationInMinutes = 0.0; - var points = 0.0; - - if (type == STEP_TYPE_VIDEO) - { - stepDurationInMinutes = videoDuration.Value * VIDEO_DURATION_PAUSES_MULTIPLIER; - } - - if (type == STEP_TYPE_QUIZ) - { - stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; - } if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) { continue; } - if (type == STEP_TYPE_VIDEO) - { - points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; - } + var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); - if (type == STEP_TYPE_QUIZ) - { - points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; - } - - var step = new Step - { - Id = id, - Type = type, - Duration = stepDurationInMinutes, - Points = points - }; - - results += JsonSerializer.Serialize(step, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); - - if (i != lines.Length - 1) - { - results += ","; - } + csvSteps.Add(csvStep); } - results += "]"; - results = "["; + var results = "["; lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CsvStep.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CsvStep.cs new file mode 100644 index 00000000..57d508ee --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CsvStep.cs @@ -0,0 +1,19 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CsvStep + { + public string StepId { get; } + public string Type { get; } + public double? QuizTotalQuestions { get; } + public double? VideoDuration { get; } + + public CsvStep(string stepId, string type, double? quizTotalQuestions, double? videoDuration) + { + StepId = stepId; + Type = type; + QuizTotalQuestions = quizTotalQuestions; + VideoDuration = videoDuration; + } + } +} \ No newline at end of file From 326099de6cba05280baf6e65e603b5c311db50a6 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:37:46 +0200 Subject: [PATCH 12/18] [csharp] [courses_steps_csv] [03_split_parsing_phase] Refactor the second loop in order to use the parsed DTOs --- .../CourseStepsGetController.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index ad0ed035..4a46cce9 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text.Json; using Microsoft.VisualBasic.FileIO; @@ -37,7 +38,6 @@ public string Get(string courseId) var type = row[1]; int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); - if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) { @@ -53,14 +53,15 @@ public string Get(string courseId) lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - for (int i = 0; i < lines.Length; i++) + for (int i = 0; i < csvSteps.Count; i++) { var row = lines[i].Split(','); - var id = row[0]; - var type = row[1]; - int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); - int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + var csvStep = csvSteps.ElementAt(i); + var id = csvStep.StepId; + var type = csvStep.Type; + var quizTotalQuestions = csvStep.QuizTotalQuestions; + var videoDuration = csvStep.VideoDuration; var stepDurationInMinutes = 0.0; var points = 0.0; @@ -75,11 +76,6 @@ public string Get(string courseId) stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; } - if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) - { - continue; - } - if (type == STEP_TYPE_VIDEO) { points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; From 78123d8ed7c4c485642f8ae9160d1b6db6c9485e Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:40:59 +0200 Subject: [PATCH 13/18] [csharp] [courses_steps_csv] [03_split_parsing_phase] Encapsulate `Step` class state management not allowing modifying it from the outside --- .../CoursesStepsCsv/CourseStepsGetController.cs | 8 +------- .../src/CoursesStepsCsv/Step.cs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 4a46cce9..92d2a413 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -86,13 +86,7 @@ public string Get(string courseId) points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; } - var step = new Step - { - Id = id, - Type = type, - Duration = stepDurationInMinutes, - Points = points - }; + var step = new Step(id, type, stepDurationInMinutes, points); results += JsonSerializer.Serialize(step, new JsonSerializerOptions { diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs index 77a4b962..6026404c 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/Step.cs @@ -3,9 +3,17 @@ namespace CodelyTv.CoursesStepsCsv { public sealed class Step { - public string Id { get; set; } - public string Type { get; set; } - public double Duration { get; set; } - public double Points { get; set; } + public string Id { get; } + public string Type { get; } + public double Duration { get; } + public double Points { get; } + + public Step(string id, string type, double duration, double points) + { + Id = id; + Type = type; + Duration = duration; + Points = points; + } } } \ No newline at end of file From 8ed79decd78ff9d37777ed735c1dc4cce0980d30 Mon Sep 17 00:00:00 2001 From: Javier Ferrer Date: Mon, 26 Apr 2021 13:44:33 +0200 Subject: [PATCH 14/18] =?UTF-8?q?[csharp]=20[courses=5Fsteps=5Fcsv]=20[03?= =?UTF-8?q?=5Fsplit=5Fparsing=5Fphase]=20Extract=20method=20for=20the=20Pa?= =?UTF-8?q?rseCsv=20phase=20=F0=9F=8C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit VS Code is my new best friend. It can extract method ✨ --- .../CourseStepsGetController.cs | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 92d2a413..8f6f3e74 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -24,45 +24,19 @@ public CourseStepsGetController(Platform platform) public string Get(string courseId) { - var csv = platform.FindCourseSteps(courseId); - - var csvSteps = new List(); - - var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - - for (int i = 0; i < lines.Length; i++) - { - var row = lines[i].Split(','); - - var id = row[0]; - var type = row[1]; - int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); - int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); - - if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) - { - continue; - } - - var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); - - csvSteps.Add(csvStep); - } + List csvSteps = ParseCsv(courseId); var results = "["; - lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - for (int i = 0; i < csvSteps.Count; i++) { - var row = lines[i].Split(','); - var csvStep = csvSteps.ElementAt(i); + var id = csvStep.StepId; var type = csvStep.Type; var quizTotalQuestions = csvStep.QuizTotalQuestions; var videoDuration = csvStep.VideoDuration; - + var stepDurationInMinutes = 0.0; var points = 0.0; @@ -93,7 +67,7 @@ public string Get(string courseId) PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); - if (i != lines.Length - 1) + if (i != csvSteps.Count - 1) { results += ","; } @@ -102,5 +76,35 @@ public string Get(string courseId) return results; } + + private List ParseCsv(string courseId) + { + var csv = platform.FindCourseSteps(courseId); + + var csvSteps = new List(); + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var id = row[0]; + var type = row[1]; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) + { + continue; + } + + var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); + + csvSteps.Add(csvStep); + } + + return csvSteps; + } } } From 67e5ba08f10557fcb397261e1a13cf705d62818f Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 14:46:14 +0200 Subject: [PATCH 15/18] [csharp] [courses_steps_csv] [04_split_serialization_phase] Add base code ready to be refactored --- ...steps_csv-04_split_serialization_phase.sln | 56 +++++++++ .../CourseStepsGetController.cs | 110 ++++++++++++++++++ .../CoursesStepsCsv/CoursesStepsCsv.csproj | 7 ++ .../src/CoursesStepsCsv/CsvStep.cs | 19 +++ .../src/CoursesStepsCsv/Platform.cs | 9 ++ .../src/CoursesStepsCsv/Step.cs | 19 +++ .../CourseStepsGetControllerShould.cs | 63 ++++++++++ .../CoursesStepsCsv.Tests.csproj | 28 +++++ 8 files changed, 311 insertions(+) create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/csharp-courses_steps_csv-04_split_serialization_phase.sln create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CsvStep.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Platform.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Step.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs create mode 100644 examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/csharp-courses_steps_csv-04_split_serialization_phase.sln b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/csharp-courses_steps_csv-04_split_serialization_phase.sln new file mode 100644 index 00000000..56a83fec --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/csharp-courses_steps_csv-04_split_serialization_phase.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.6.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8B7D4918-2C37-4F16-B355-8EF19B1E23F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv", "src\CoursesStepsCsv\CoursesStepsCsv.csproj", "{C6E9F888-08F3-4D4B-9AEB-919F615C988F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{C8A13F5C-212F-4163-885A-01C093B26450}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoursesStepsCsv.Tests", "test\CoursesStepsCsv.Tests\CoursesStepsCsv.Tests.csproj", "{C6D93799-5C86-410F-A957-1A531A757DAE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x64.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Debug|x86.Build.0 = Debug|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|Any CPU.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x64.Build.0 = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.ActiveCfg = Release|Any CPU + {C6E9F888-08F3-4D4B-9AEB-919F615C988F}.Release|x86.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x64.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.ActiveCfg = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Debug|x86.Build.0 = Debug|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|Any CPU.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x64.Build.0 = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.ActiveCfg = Release|Any CPU + {C6D93799-5C86-410F-A957-1A531A757DAE}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C6E9F888-08F3-4D4B-9AEB-919F615C988F} = {8B7D4918-2C37-4F16-B355-8EF19B1E23F8} + {C6D93799-5C86-410F-A957-1A531A757DAE} = {C8A13F5C-212F-4163-885A-01C093B26450} + EndGlobalSection +EndGlobal diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs new file mode 100644 index 00000000..8f6f3e74 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; +using Microsoft.VisualBasic.FileIO; + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CourseStepsGetController + { + private const double VIDEO_DURATION_PAUSES_MULTIPLIER = 1.1; + private const double QUIZ_TIME_PER_QUESTION_MULTIPLIER = 0.5; + private const int QUIZ_POINTS_PER_MINUTE = 10; + private const string STEP_TYPE_QUIZ = "quiz"; + private const string STEP_TYPE_VIDEO = "video"; + private const int VIDEO_POINTS_PER_MINUTE = 100; + private readonly Platform platform; + + public CourseStepsGetController(Platform platform) + { + this.platform = platform; + } + + public string Get(string courseId) + { + List csvSteps = ParseCsv(courseId); + + var results = "["; + + for (int i = 0; i < csvSteps.Count; i++) + { + var csvStep = csvSteps.ElementAt(i); + + var id = csvStep.StepId; + var type = csvStep.Type; + var quizTotalQuestions = csvStep.QuizTotalQuestions; + var videoDuration = csvStep.VideoDuration; + + var stepDurationInMinutes = 0.0; + var points = 0.0; + + if (type == STEP_TYPE_VIDEO) + { + stepDurationInMinutes = videoDuration.Value * VIDEO_DURATION_PAUSES_MULTIPLIER; + } + + if (type == STEP_TYPE_QUIZ) + { + stepDurationInMinutes = quizTotalQuestions.Value * QUIZ_TIME_PER_QUESTION_MULTIPLIER; + } + + if (type == STEP_TYPE_VIDEO) + { + points = stepDurationInMinutes * VIDEO_POINTS_PER_MINUTE; + } + + if (type == STEP_TYPE_QUIZ) + { + points = stepDurationInMinutes * QUIZ_POINTS_PER_MINUTE; + } + + var step = new Step(id, type, stepDurationInMinutes, points); + + results += JsonSerializer.Serialize(step, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); + + if (i != csvSteps.Count - 1) + { + results += ","; + } + } + results += "]"; + + return results; + } + + private List ParseCsv(string courseId) + { + var csv = platform.FindCourseSteps(courseId); + + var csvSteps = new List(); + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var id = row[0]; + var type = row[1]; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) + { + continue; + } + + var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); + + csvSteps.Add(csvStep); + } + + return csvSteps; + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj new file mode 100644 index 00000000..f208d303 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CoursesStepsCsv.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CsvStep.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CsvStep.cs new file mode 100644 index 00000000..57d508ee --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CsvStep.cs @@ -0,0 +1,19 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class CsvStep + { + public string StepId { get; } + public string Type { get; } + public double? QuizTotalQuestions { get; } + public double? VideoDuration { get; } + + public CsvStep(string stepId, string type, double? quizTotalQuestions, double? videoDuration) + { + StepId = stepId; + Type = type; + QuizTotalQuestions = quizTotalQuestions; + VideoDuration = videoDuration; + } + } +} \ No newline at end of file diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Platform.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Platform.cs new file mode 100644 index 00000000..e9ef3120 --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Platform.cs @@ -0,0 +1,9 @@ +using System; + +namespace CodelyTv.CoursesStepsCsv +{ + public interface Platform + { + string FindCourseSteps(string courseId); + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Step.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Step.cs new file mode 100644 index 00000000..6026404c --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/Step.cs @@ -0,0 +1,19 @@ + +namespace CodelyTv.CoursesStepsCsv +{ + public sealed class Step + { + public string Id { get; } + public string Type { get; } + public double Duration { get; } + public double Points { get; } + + public Step(string id, string type, double duration, double points) + { + Id = id; + Type = type; + Duration = duration; + Points = points; + } + } +} \ No newline at end of file diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs new file mode 100644 index 00000000..2354645b --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs @@ -0,0 +1,63 @@ +using System; +using Xunit; +using Moq; + +namespace CodelyTv.CoursesStepsCsv.Tests +{ + public class CourseStepsGetControllerShould + { + private readonly Mock platform; + private readonly CourseStepsGetController courseStepsGetController; + public CourseStepsGetControllerShould() + { + platform = new Mock(); + courseStepsGetController = new CourseStepsGetController(platform.Object); + } + + [Fact] + public void ReturnEmptyStepList() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var emptyCsv = ""; + + GivenPlatformReturnsCourseStepCsv(courseId, emptyCsv); + + var actualCourseSteps = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", actualCourseSteps); + } + + [Fact] + public void ReturnExistingCourseSteps() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = String.Join( + Environment.NewLine, + "1,video,,13", + "2,quiz,5,"); + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + var expected = "[{\"id\":\"1\",\"type\":\"video\",\"duration\":14.3,\"points\":1430},{\"id\":\"2\",\"type\":\"quiz\",\"duration\":2.5,\"points\":25}]"; + Assert.Equal(expected, results); + } + + [Fact] + public void IgnoreStepsWithInvalidType() + { + var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19"; + var csv = "1,survey,,13"; + GivenPlatformReturnsCourseStepCsv(courseId, csv); + + var results = courseStepsGetController.Get(courseId); + + Assert.Equal("[]", results); + } + + private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv) + { + platform.Setup(x => x.FindCourseSteps(courseId)).Returns(csv); + } + } +} diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj new file mode 100644 index 00000000..528458bd --- /dev/null +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/test/CoursesStepsCsv.Tests/CoursesStepsCsv.Tests.csproj @@ -0,0 +1,28 @@ + + + + net5.0 + CodelyTv.CoursesStepsCsv.Tests + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + From d174ca5c1b2e9086a0962211043d90da526c0958 Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 14:56:17 +0200 Subject: [PATCH 16/18] [csharp] [courses_steps_csv] [04_split_serialization_phase] Change second loop to store result in Step list DTO --- .../CourseStepsGetController.cs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 8f6f3e74..5554c6d8 100644 --- a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -26,12 +26,10 @@ public string Get(string courseId) { List csvSteps = ParseCsv(courseId); - var results = "["; + var steps = new List(); - for (int i = 0; i < csvSteps.Count; i++) + csvSteps.ForEach(csvStep => { - var csvStep = csvSteps.ElementAt(i); - var id = csvStep.StepId; var type = csvStep.Type; var quizTotalQuestions = csvStep.QuizTotalQuestions; @@ -62,19 +60,13 @@ public string Get(string courseId) var step = new Step(id, type, stepDurationInMinutes, points); - results += JsonSerializer.Serialize(step, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - }); + steps.Add(step); + }); - if (i != csvSteps.Count - 1) - { - results += ","; - } - } - results += "]"; - - return results; + return JsonSerializer.Serialize(steps, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }); } private List ParseCsv(string courseId) From 83f192f7c118b4f8023443b6d9c5157f63a1d476 Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 14:59:28 +0200 Subject: [PATCH 17/18] [csharp] [courses_steps_csv] [04_split_serialization_phase] Extract methods for serialization --- .../CourseStepsGetController.cs | 69 +++++++++++-------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 5554c6d8..9fa97e5d 100644 --- a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -25,7 +25,42 @@ public CourseStepsGetController(Platform platform) public string Get(string courseId) { List csvSteps = ParseCsv(courseId); + List steps = CreateStepsFromPrimitives(csvSteps); + return ToJson(steps); + } + + private List ParseCsv(string courseId) + { + var csv = platform.FindCourseSteps(courseId); + + var csvSteps = new List(); + + var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + + for (int i = 0; i < lines.Length; i++) + { + var row = lines[i].Split(','); + + var id = row[0]; + var type = row[1]; + int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); + int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); + + if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) + { + continue; + } + + var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); + + csvSteps.Add(csvStep); + } + return csvSteps; + } + + private static List CreateStepsFromPrimitives(List csvSteps) + { var steps = new List(); csvSteps.ForEach(csvStep => @@ -62,41 +97,15 @@ public string Get(string courseId) steps.Add(step); }); + return steps; + } + private static string ToJson(List steps) + { return JsonSerializer.Serialize(steps, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); } - - private List ParseCsv(string courseId) - { - var csv = platform.FindCourseSteps(courseId); - - var csvSteps = new List(); - - var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - - for (int i = 0; i < lines.Length; i++) - { - var row = lines[i].Split(','); - - var id = row[0]; - var type = row[1]; - int? quizTotalQuestions = string.IsNullOrEmpty(row[2]) ? null : int.Parse(row[2]); - int? videoDuration = string.IsNullOrEmpty(row[3]) ? null : int.Parse(row[3]); - - if (type != STEP_TYPE_VIDEO && type != STEP_TYPE_QUIZ) - { - continue; - } - - var csvStep = new CsvStep(id, type, quizTotalQuestions, videoDuration); - - csvSteps.Add(csvStep); - } - - return csvSteps; - } } } From 25f413ad92adffa6617377b996294936ad20a370 Mon Sep 17 00:00:00 2001 From: "ismanapa@gmail.com" Date: Mon, 26 Apr 2021 15:30:44 +0200 Subject: [PATCH 18/18] [csharp] [courses_steps_csv] Move FindCourseSteps from ParseCsv to CourseStepsGetController entry point --- .../src/CoursesStepsCsv/CourseStepsGetController.cs | 7 +++---- .../src/CoursesStepsCsv/CourseStepsGetController.cs | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 8f6f3e74..0c0afc40 100644 --- a/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-03_split_parsing_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -24,7 +24,8 @@ public CourseStepsGetController(Platform platform) public string Get(string courseId) { - List csvSteps = ParseCsv(courseId); + var csv = platform.FindCourseSteps(courseId); + List csvSteps = ParseCsv(csv); var results = "["; @@ -77,10 +78,8 @@ public string Get(string courseId) return results; } - private List ParseCsv(string courseId) + private List ParseCsv(string csv) { - var csv = platform.FindCourseSteps(courseId); - var csvSteps = new List(); var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); diff --git a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs index 9fa97e5d..9f6ee88e 100644 --- a/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs +++ b/examples/csharp/csharp-courses_steps_csv-04_split_serialization_phase/src/CoursesStepsCsv/CourseStepsGetController.cs @@ -24,15 +24,14 @@ public CourseStepsGetController(Platform platform) public string Get(string courseId) { - List csvSteps = ParseCsv(courseId); + var csv = platform.FindCourseSteps(courseId); + List csvSteps = ParseCsv(csv); List steps = CreateStepsFromPrimitives(csvSteps); return ToJson(steps); } - private List ParseCsv(string courseId) + private List ParseCsv(string csv) { - var csv = platform.FindCourseSteps(courseId); - var csvSteps = new List(); var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);