562bc87822
* #74: Add UserID to the Song model * #74: Added functionality to retrieve user id from the token * #74: Songs will now contain the User Id when uploading * Updated Readme and script to add migrations * #74: Some cleanup
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Icarus.Models;
|
|
|
|
namespace Icarus.Database.Contexts;
|
|
|
|
public class UserContext : DbContext
|
|
{
|
|
public DbSet<User> Users { get; set; }
|
|
|
|
|
|
#region Constructors
|
|
public UserContext(DbContextOptions<UserContext> options) : base(options) { }
|
|
[ActivatorUtilitiesConstructor]
|
|
public UserContext(string connString) : base(new DbContextOptionsBuilder<UserContext>()
|
|
.UseMySQL(connString).Options)
|
|
{
|
|
}
|
|
#endregion
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<User>()
|
|
.ToTable("User");
|
|
modelBuilder.Entity<User>()
|
|
.Property(u => u.LastLogin).IsRequired(false);
|
|
modelBuilder.Entity<User>()
|
|
.Property(u => u.DateCreated).HasDefaultValue(DateTime.Now);
|
|
}
|
|
|
|
|
|
public User RetrieveRecord(User user)
|
|
{
|
|
return Users.FirstOrDefault(usr => usr.UserID == user.UserID);
|
|
}
|
|
|
|
public bool DoesRecordExist(User user)
|
|
{
|
|
return Users.FirstOrDefault(usr => usr.UserID == user.UserID) != null ? true : false;
|
|
}
|
|
}
|