DB added and more
This commit is contained in:
19
TOOHUCardAPI/Data/AppDbContext.cs
Normal file
19
TOOHUCardAPI/Data/AppDbContext.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
TOOHUCardAPI/Data/AutomapProfile.cs
Normal file
15
TOOHUCardAPI/Data/AutomapProfile.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
TOOHUCardAPI/Data/Models/EncodedCardGroup.cs
Normal file
12
TOOHUCardAPI/Data/Models/EncodedCardGroup.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
22
TOOHUCardAPI/Data/Models/User.cs
Normal file
22
TOOHUCardAPI/Data/Models/User.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
58
TOOHUCardAPI/Data/Repositories/UserRepository.cs
Normal file
58
TOOHUCardAPI/Data/Repositories/UserRepository.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user