-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathScoreController.cs
More file actions
77 lines (60 loc) · 2.6 KB
/
Copy pathScoreController.cs
File metadata and controls
77 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Flashcards.davetn657.Models.DTOs;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using System.Data;
namespace Flashcards.davetn657.Controllers;
public class ScoreController
{
private IConfiguration configuration;
private string? connectionString;
public ScoreController()
{
this.configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
this.connectionString = configuration.GetConnectionString("DatabaseConnection");
}
internal void AddScore(StudyDto session, ScoreDto score)
{
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = @"INSERT INTO SCORES (SessionId, Score)
VALUES (@id, @score)";
tableCmd.Parameters.Add("@id", SqlDbType.Int).Value = session.Id;
tableCmd.Parameters.Add("@score", SqlDbType.Int).Value = score.Score;
tableCmd.ExecuteNonQuery();
connection.Close();
}
}
internal List<ScoreDto> GetScores(int numberOfDays)
{
var pastScores = new List<ScoreDto>();
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = @"SELECT Sessions.StackId, Sessions.SessionName, Scores.Score, Scores.CreateDate
FROM Sessions
LEFT JOIN Scores ON Sessions.SessionId = Scores.SessionId
WHERE CAST(Scores.CreateDate AS DATE) >= DATEADD(day, @numDays, GETDATE())
GROUP BY Sessions.StackId, Sessions.SessionName, Scores.Score, Scores.CreateDate
ORDER BY Scores.CreateDate DESC";
tableCmd.Parameters.Add("@numDays", SqlDbType.Int).Value = -numberOfDays;
var reader = tableCmd.ExecuteReader();
while (reader.Read())
{
var data = new ScoreDto();
data.SessionId = reader.GetInt32("StackId");
data.Name = reader.GetString("SessionName");
data.Score = reader.GetInt32("Score");
data.CreateDate = reader.GetDateTime("CreateDate");
pastScores.Add(data);
}
connection.Close();
}
return pastScores;
}
}