Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f96cfbe

Browse files
committed
[csharp] [courses_steps_csv] Implement 01 base
1 parent 22297c6 commit f96cfbe

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

‎examples/csharp/csharp-courses_steps_csv-01_base/src/CoursesStepsCsv/CourseStepsGetController.cs‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text.Json;
5+
using Microsoft.VisualBasic.FileIO;
26

37
namespace CodelyTv.CoursesStepsCsv
48
{
@@ -13,7 +17,66 @@ public CourseStepsGetController(Platform platform)
1317

1418
public string Get(string courseId)
1519
{
16-
return platform.FindCourseSteps(courseId);
20+
var csv = platform.FindCourseSteps(courseId);
21+
22+
var results = "[";
23+
24+
var lines = csv.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
25+
26+
for (int i = 0; i < lines.Length; i++)
27+
{
28+
var row = lines[i].Split(',');
29+
30+
var type = row[1];
31+
var duration = 0.0;
32+
var points = 0.0;
33+
34+
if (type == "video")
35+
{
36+
duration = int.Parse(row[3]) * 1.1;
37+
}
38+
39+
if (type == "quiz")
40+
{
41+
duration = int.Parse(row[2]) * 0.5; // 0.5 = time in minutes per question
42+
}
43+
44+
if (type != "video" && type != "quiz")
45+
{
46+
continue;
47+
}
48+
49+
if (type == "video")
50+
{
51+
points = int.Parse(row[3]) * 1.1 * 100;
52+
}
53+
54+
if (type == "quiz")
55+
{
56+
points = int.Parse(row[2]) * 0.5 * 10;
57+
}
58+
59+
var step = new Step
60+
{
61+
Id = row[0],
62+
Type = row[1],
63+
Duration = duration,
64+
Points = points
65+
};
66+
67+
results += JsonSerializer.Serialize(step, new JsonSerializerOptions
68+
{
69+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
70+
});
71+
72+
if (i != lines.Length - 1)
73+
{
74+
results += ",";
75+
}
76+
}
77+
results += "]";
78+
79+
return results;
1780
}
1881
}
1982
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
namespace CodelyTv.CoursesStepsCsv
3+
{
4+
public sealed class Step
5+
{
6+
public string Id { get; set; }
7+
public string Type { get; set; }
8+
public double Duration { get; set; }
9+
public double Points { get; set; }
10+
}
11+
}

‎examples/csharp/csharp-courses_steps_csv-01_base/test/CoursesStepsCsv.Tests/CourseStepsGetControllerShould.cs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,35 @@ public void ReturnEmptyStepList()
2424

2525
var actualCourseSteps = courseStepsGetController.Get(courseId);
2626

27-
Assert.Equal("", actualCourseSteps);
27+
Assert.Equal("[]", actualCourseSteps);
28+
}
29+
30+
[Fact]
31+
public void ReturnExistingCourseSteps()
32+
{
33+
var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19";
34+
var csv = String.Join(
35+
Environment.NewLine,
36+
"1,video,,13",
37+
"2,quiz,5,");
38+
GivenPlatformReturnsCourseStepCsv(courseId, csv);
39+
40+
var results = courseStepsGetController.Get(courseId);
41+
42+
var expected = "[{\"id\":\"1\",\"type\":\"video\",\"duration\":14.3,\"points\":1430},{\"id\":\"2\",\"type\":\"quiz\",\"duration\":2.5,\"points\":25}]";
43+
Assert.Equal(expected, results);
44+
}
45+
46+
[Fact]
47+
public void IgnoreStepsWithInvalidType()
48+
{
49+
var courseId = "8fe17ce6-1d33-4b6b-a27c-4e0d1f870a19";
50+
var csv = "1,survey,,13";
51+
GivenPlatformReturnsCourseStepCsv(courseId, csv);
52+
53+
var results = courseStepsGetController.Get(courseId);
54+
55+
Assert.Equal("[]", results);
2856
}
2957

3058
private void GivenPlatformReturnsCourseStepCsv(string courseId, string csv)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /