ScoreServiceEF.cs 967 Bytes
Newer Older
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
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using NPuzzle.Entity;

namespace NPuzzle.Service
{
    public class ScoreServiceEF : IScoreService
    {
        public void AddScore(Score score)
        {
            using (var context = new NPuzzleDbContext())
            {
                context.Scores.Add(score);
                context.SaveChanges();
            }
        }

        public IList<Score> GetTopScores()
        {
            using (var context = new NPuzzleDbContext())
            {
                return (from s in context.Scores
                    orderby s.Points
                        descending
                    select s).Take(5).ToList();
            }
        }

        public void ClearScores()
        {
            using (var context = new NPuzzleDbContext())
            {
                context.Database.ExecuteSqlCommand("DELETE FROM Scores");
            }
        }
    }
}