Commit 56478c35 authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

6. prednaska

parent 7086895b
......@@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Text;
using NPuzzle.Core;
using NPuzzleCore.Entity;
using NPuzzleCore.Service;
using NPuzzle.Entity;
using NPuzzle.Service;
namespace NPuzzle.ConsoleUI
{
......@@ -11,7 +11,8 @@ namespace NPuzzle.ConsoleUI
{
private readonly Field _field;
private readonly IScoreService _scoreService = new ScoreServiceFile();
//private readonly IScoreService _scoreService = new ScoreServiceFile();
private readonly IScoreService _scoreService = new ScoreServiceEF();
public ConsoleUI(Field field)
{
......
using System;
using NPuzzle.Core;
using NPuzzleCore.Core;
namespace NPuzzle
{
......
......@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace NPuzzleCore.Core
namespace NPuzzle.Core
{
public struct Coordinate
{
......
......@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPuzzleCore.Core;
namespace NPuzzle.Core
{
......@@ -29,7 +28,7 @@ namespace NPuzzle.Core
ColumnCount = columnCount;
_tiles = new Tile[rowCount, columnCount];
Initialize();
Shuffle();
//Shuffle();
startTime = DateTime.Now;
}
......
......@@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Text;
namespace NPuzzleCore.Entity
namespace NPuzzle.Entity
{
[Serializable]
public class Score
{
public int Id { get; set; }
public string Player { get; set; }
public int Points { get; set; }
......
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using NPuzzle.Service;
namespace NPuzzleCore.Migrations
{
[DbContext(typeof(NPuzzleDbContext))]
[Migration("20210325150516_Migration1")]
partial class Migration1
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.4")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("NPuzzle.Entity.Score", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("PlayedAt")
.HasColumnType("datetime2");
b.Property<string>("Player")
.HasColumnType("nvarchar(max)");
b.Property<int>("Points")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Scores");
});
#pragma warning restore 612, 618
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace NPuzzleCore.Migrations
{
public partial class Migration1 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Scores",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Player = table.Column<string>(type: "nvarchar(max)", nullable: true),
Points = table.Column<int>(type: "int", nullable: false),
PlayedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Scores", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Scores");
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using NPuzzle.Service;
namespace NPuzzleCore.Migrations
{
[DbContext(typeof(NPuzzleDbContext))]
partial class NPuzzleDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.4")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("NPuzzle.Entity.Score", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<DateTime>("PlayedAt")
.HasColumnType("datetime2");
b.Property<string>("Player")
.HasColumnType("nvarchar(max)");
b.Property<int>("Points")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Scores");
});
#pragma warning restore 612, 618
}
}
}
......@@ -4,4 +4,12 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Text;
using NPuzzleCore.Entity;
using NPuzzle.Entity;
namespace NPuzzleCore.Service
namespace NPuzzle.Service
{
public interface IScoreService
{
......
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using NPuzzle.Entity;
namespace NPuzzle.Service
{
public class NPuzzleDbContext : DbContext
{
public DbSet<Score> Scores { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=NPuzzle;Trusted_Connection=True;");
}
}
}
using System;
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(3).ToList();
}
}
public void ResetScore()
{
using (var context = new NPuzzleDbContext())
{
context.Database.ExecuteSqlRaw("DELETE FROM Scores");
}
}
}
}
\ No newline at end of file
using NPuzzleCore.Entity;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NPuzzle.Entity;
namespace NPuzzleCore.Service
namespace NPuzzle.Service
{
public class ScoreServiceFile : IScoreService
{
......@@ -14,20 +14,20 @@ namespace NPuzzleCore.Service
private IList<Score> _scores = new List<Score>();
void IScoreService.AddScore(Score score)
public void AddScore(Score score)
{
_scores.Add(score);
SaveScores();
}
IList<Score> IScoreService.GetTopScores()
public IList<Score> GetTopScores()
{
LoadScores();
return _scores.OrderByDescending(s => s.Points).Take(3).ToList();
//return (from s in _scores orderby s.Points descending select s).Take(3).ToList();
//return _scores.OrderByDescending(s => s.Points).Take(3).ToList();
return (from s in _scores orderby s.Points descending select s).Take(3).ToList();
}
void IScoreService.ResetScore()
public void ResetScore()
{
_scores.Clear();
File.Delete(FileName);
......
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NPuzzleCore.Service;
using System;
using NPuzzleCore.Entity;
using NPuzzle.Entity;
using NPuzzle.Service;
namespace NPuzzleTest
namespace NPuzzle.Test
{
[TestClass]
public class ScoreServiceTest
......@@ -26,9 +26,9 @@ namespace NPuzzleTest
{
var service = CreateService();
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Peter", Points = 200, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jozo", Points = 50, PlayedAt = DateTime.Now });
service.AddScore(new Score {Player = "Jaro", Points = 100, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Peter", Points = 200, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Jozo", Points = 50, PlayedAt = DateTime.Now});
Assert.AreEqual(3, service.GetTopScores().Count);
......@@ -47,11 +47,11 @@ namespace NPuzzleTest
{
var service = CreateService();
service.AddScore(new Score { Player = "Zuzka", Points = 20, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Anca", Points = 10, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Peter", Points = 200, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jozo", Points = 50, PlayedAt = DateTime.Now });
service.AddScore(new Score {Player = "Zuzka", Points = 20, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Jaro", Points = 100, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Anca", Points = 10, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Peter", Points = 200, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Jozo", Points = 50, PlayedAt = DateTime.Now});
Assert.AreEqual(3, service.GetTopScores().Count);
......@@ -70,16 +70,19 @@ namespace NPuzzleTest
{
var service = CreateService();
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score { Player = "Jaro", Points = 100, PlayedAt = DateTime.Now });
service.AddScore(new Score {Player = "Jaro", Points = 100, PlayedAt = DateTime.Now});
service.AddScore(new Score {Player = "Jaro", Points = 100, PlayedAt = DateTime.Now});
service.ResetScore();
Assert.AreEqual(0, service.GetTopScores().Count);
}
private IScoreService CreateService()
{
return new ScoreServiceFile();
//var service = new ScoreServiceFile();
var service = new ScoreServiceEF();
service.ResetScore();
return service;
}
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment