DB added and more

This commit is contained in:
2021-10-28 17:03:15 -04:00
parent b56ca46363
commit 28fbc4598e
20 changed files with 453 additions and 21 deletions

View File

@@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using TOOHUCardAPI.Data.Models;
namespace TOOHUCardAPI.Data
{
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public AppDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
}

View File

@@ -0,0 +1,15 @@
using AutoMapper;
using TOOHUCardAPI.Data.Models;
using TOOHUCardAPI.DTO;
namespace TOOHUCardAPI.Data
{
public class AutomapProfile : Profile
{
public AutomapProfile()
{
CreateMap<User, PlayerDataGetResponseObjectPlayer>();
CreateMap<User, User>();
}
}
}

View File

@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
namespace TOOHUCardAPI.Data.Models
{
public class EncodedCardGroup
{
[Key]
public int Id { get; set; }
public int CardGroupNumber { get; set; }
public string EncodedString { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace TOOHUCardAPI.Data.Models
{
public class User
{
[Key]
public string SteamId { get; set; }
public int MaxWave { get; set; }
public int MaxTeamWave { get; set; }
public int PetLevel { get; set; }
public DateTime EndTime { get; set; }
public int KeyTotal { get; set; }
public DateTime KeySaveDate { get; set; }
public bool Vip { get; set; }
public int Point { get; set; }
public string LevelList { get; set; }
public List<EncodedCardGroup> EncodedCardGroups { get; set; }
}
}

View File

@@ -0,0 +1,58 @@
using System;
using System.Linq;
using AutoMapper;
using TOOHUCardAPI.Data.Models;
namespace TOOHUCardAPI.Data.Repositories
{
public class UserRepository
{
private readonly AppDbContext _appDbContext;
private readonly IMapper _mapper;
public UserRepository(AppDbContext appDbContext, IMapper mapper)
{
_appDbContext = appDbContext;
_mapper = mapper;
}
public User GetUser(string steamId)
{
return _appDbContext.Users.Where((user => user.SteamId.Equals(steamId))).FirstOrDefault();
}
public User CreateUser(string steamId)
{
User user = new User()
{
SteamId = steamId,
Vip = true,
EndTime = DateTime.MaxValue,
LevelList = string.Empty,
PetLevel = 1
};
_appDbContext.Users.Add(user);
_appDbContext.SaveChanges();
return user;
}
public User UpdateUser(User user)
{
var trackedUser = _appDbContext.Users.FirstOrDefault(u => u.SteamId == user.SteamId);
if (trackedUser == default)
{
return trackedUser;
}
_appDbContext.Update(user);
_appDbContext.SaveChanges();
return user;
}
public User GetOrCreateUser(string steamId)
{
User user = GetUser(steamId) ?? CreateUser(steamId);
return user;
}
}
}