commit 031261bc526b3380218eaa73c402735416a8c826
parent 5dddcedaeecebbcbe2478c8b0914999fd6456148
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date: Wed, 14 Aug 2019 16:12:31 +0200
Provided extra abstraction around a published system, and added published system tracking functionality to the overview view model
Diffstat:
4 files changed, 124 insertions(+), 14 deletions(-)
diff --git a/StarSim/StarSimGui/ViewModels/OverviewViewModel.cs b/StarSim/StarSimGui/ViewModels/OverviewViewModel.cs
@@ -1,11 +1,6 @@
-using System.IO;
-using Avalonia;
-using Avalonia.Media;
-using Avalonia.Media.Imaging;
-using Avalonia.Platform;
-using Avalonia.Skia;
+using System.Linq;
+using DynamicData.Binding;
using ReactiveUI;
-using StarSimGui.Source;
using StarSimLib.Contexts;
using StarSimLib.Models;
@@ -27,6 +22,11 @@ namespace StarSimGui.ViewModels
private User currentUser;
/// <summary>
+ /// The backing field for the <see cref="PublishedSystems"/> property.
+ /// </summary>
+ private IObservableCollection<PublishedSystem> publishedSystems;
+
+ /// <summary>
/// Initialises a new instance of the <see cref="OverviewViewModel"/> class.
/// </summary>
public OverviewViewModel() : this(null)
@@ -40,6 +40,8 @@ namespace StarSimGui.ViewModels
public OverviewViewModel(SimulatorContext context)
{
dbContext = context;
+
+ publishedSystems = new ObservableCollectionExtended<PublishedSystem>();
}
/// <summary>
@@ -59,12 +61,33 @@ namespace StarSimGui.ViewModels
}
/// <summary>
+ /// The <see cref="PublishedSystem"/> instance that the current user has published.
+ /// </summary>
+ public IObservableCollection<PublishedSystem> PublishedSystems
+ {
+ get
+ {
+ return publishedSystems;
+ }
+ set
+ {
+ publishedSystems = value;
+ this.RaisePropertyChanged();
+ }
+ }
+
+ /// <summary>
/// Handles the <see cref="UserLoginViewModel.LoggedIn"/> event.
/// </summary>
/// <param name="newUser">The user which logged in.</param>
public void HandleLogin(User newUser)
{
CurrentUser = newUser;
+
+ IQueryable<PublishedSystem> usersPublishedSystems =
+ dbContext.PublishedSystems.Where(system => system.Publisher.Id == CurrentUser.Id);
+
+ PublishedSystems.Load(usersPublishedSystems);
}
/// <summary>
@@ -73,6 +96,8 @@ namespace StarSimGui.ViewModels
public void HandleLogout()
{
CurrentUser = null;
+
+ PublishedSystems.Clear();
}
}
}
\ No newline at end of file
diff --git a/StarSim/StarSimLib/Contexts/SimulatorContext.cs b/StarSim/StarSimLib/Contexts/SimulatorContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using System;
+using Microsoft.EntityFrameworkCore;
using StarSimLib.Cryptography;
using StarSimLib.Models;
@@ -41,6 +42,11 @@ namespace StarSimLib.Contexts
public DbSet<BodyToSystemJoin> BodyToSystemJoins { get; set; }
/// <summary>
+ /// Holds the <see cref="PublishedSystem"/> entities in the database. Also allows querying the database via LINQ.
+ /// </summary>
+ public DbSet<PublishedSystem> PublishedSystems { get; set; }
+
+ /// <summary>
/// Holds the <see cref="Models.System"/> entities in the database. Also allows querying the database via LINQ.
/// </summary>
public DbSet<Models.System> Systems { get; set; }
@@ -86,6 +92,12 @@ namespace StarSimLib.Contexts
new BodyToSystemJoin(3, 3, 1)
);
+ modelBuilder.Entity<PublishedSystem>().HasIndex(system => new { system.Id, system.PublisherId, system.SystemId }).IsUnique();
+
+ modelBuilder.Entity<PublishedSystem>().HasData(
+ new PublishedSystem(1, 1, 1, DateTime.Now)
+ );
+
modelBuilder.Entity<User>().HasIndex(user => user.Id).IsUnique();
modelBuilder.Entity<User>().Property(user => user.Email).HasDefaultValue("john.doe@gmail.com");
diff --git a/StarSim/StarSimLib/Models/PublishedSystem.cs b/StarSim/StarSimLib/Models/PublishedSystem.cs
@@ -0,0 +1,72 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace StarSimLib.Models
+{
+ /// <summary>
+ /// Represents a published system in the database.
+ /// </summary>
+ public class PublishedSystem
+ {
+ /// <summary>
+ /// Initialises a new instance of the <see cref="PublishedSystem"/> class.
+ /// </summary>
+ /// <param name="id">The unique id of this instance.</param>
+ /// <param name="publisherId">The unique id of the publisher of this instance.</param>
+ /// <param name="systemId">The unique id of the system that was published in this instance.</param>
+ /// <param name="publishDate">The date this instance was published on.</param>
+ public PublishedSystem(ulong id, ulong publisherId, ulong systemId, DateTime publishDate)
+ {
+ Id = id;
+ PublisherId = publisherId;
+ SystemId = systemId;
+ PublishDate = publishDate;
+ }
+
+ /// <summary>
+ /// The unique primary key for this instance.
+ /// </summary>
+ [Key, Required(ErrorMessage = "Published system must have unique id.")]
+ public ulong Id { get; set; }
+
+ /// <summary>
+ /// The date on which this instance was published.
+ /// </summary>
+ [Required(ErrorMessage = "Published system must have a publish date.")]
+ public DateTime PublishDate { get; set; }
+
+ /// <summary>
+ /// The <see cref="User"/> who created this instance.
+ /// </summary>
+ [ForeignKey(nameof(PublisherId))]
+ [Required(ErrorMessage = "Published system must have creator.")]
+ public User Publisher { get; set; }
+
+ /// <summary>
+ /// The Id of the <see cref="User"/> who created this instance.
+ /// </summary>
+ [Required(ErrorMessage = "Published system must have id of creator.")]
+ public ulong PublisherId { get; set; }
+
+ /// <summary>
+ /// The <see cref="Models.System"/> instance that was published..
+ /// </summary>
+ [ForeignKey(nameof(SystemId))]
+ [Required(ErrorMessage = "Published system must have published system.")]
+ public System System { get; set; }
+
+ /// <summary>
+ /// The Id of the <see cref="Models.System"/> instance which was published..
+ /// </summary>
+ [Required(ErrorMessage = "Published system must have id of published system.")]
+ public ulong SystemId { get; set; }
+
+ /// <summary>
+ /// A timestamp updated whenever the entity is handled by the database. Functions as a concurrency token to prevent
+ /// multiple access to the same field.
+ /// </summary>
+ [Timestamp]
+ public byte[] Timestamp { get; set; }
+ }
+}
+\ No newline at end of file
diff --git a/StarSim/StarSimLib/Models/User.cs b/StarSim/StarSimLib/Models/User.cs
@@ -117,12 +117,6 @@ namespace StarSimLib.Models
}
/// <summary>
- /// The <see cref="System"/> entities that this <see cref="User"/> has created.
- /// </summary>
- [InverseProperty(nameof(System.Creator))]
- public virtual ICollection<System> CreatedSystems { get; set; } = new List<System>();
-
- /// <summary>
/// The email address of this instance.
/// </summary>
[MinLength(0, ErrorMessage = "User's email can not be less than 0 characters long.")]
@@ -155,6 +149,12 @@ namespace StarSimLib.Models
public UserPrivileges Privileges { get; set; }
/// <summary>
+ /// The <see cref="PublishedSystems"/> entities that this <see cref="User"/> has published.
+ /// </summary>
+ [InverseProperty(nameof(PublishedSystem.Publisher))]
+ public virtual ICollection<PublishedSystem> PublishedSystems { get; set; } = new List<PublishedSystem>();
+
+ /// <summary>
/// A timestamp updated whenever the entity is handled by the database. Functions as a concurrency token to prevent
/// multiple access to the same field.
/// </summary>