Implement SQL for users, implement retrieving user from database

This commit is contained in:
jvyden 2021-10-06 13:38:27 -04:00
parent f179e0df8d
commit 8a3393b0fb
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
8 changed files with 67 additions and 10 deletions

View file

@ -5,6 +5,7 @@
<explicitIncludes>
<Path>.gitignore</Path>
<Path>.idea</Path>
<Path>DatabaseMigrations</Path>
<Path>ProjectLighthouse.sln.DotSettings</Path>
</explicitIncludes>
<explicitExcludes>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

30
DatabaseMigrations/0.sql Normal file
View file

@ -0,0 +1,30 @@
create table Users
(
UserId int auto_increment,
Username tinytext not null,
IconHash text null,
Game int default 0 not null,
Lists int default 0 not null,
HeartCount int default 0 not null,
YayHash text null,
BooHash text null,
Biography text null,
ReviewCount int default 0 not null,
CommentCount int default 0 not null,
PhotosByMeCount int default 0 not null,
PhotosWithMeCount int default 0 not null,
CommentsEnabled tinyint(1) default 1 not null,
FavouriteSlotCount int default 0 not null,
FavouriteUserCount int default 0 not null,
lolcatftwCount int default 0 not null,
Pins text not null,
StaffChallengeGoldCount int default 0 not null,
StaffChallengeSilverCount int default 0 not null,
StaffChallengeBronzeCount int default 0 not null,
UsedSlots int default 0 not null,
constraint users_user_id_uindex
unique (UserId)
);
alter table Users
add primary key (UserId);

View file

@ -1,4 +1,8 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ProjectLighthouse.Types;
namespace ProjectLighthouse.Controllers {
[ApiController]
@ -6,8 +10,10 @@ namespace ProjectLighthouse.Controllers {
[Produces("text/plain")]
public class MessageController : ControllerBase {
[HttpGet("eula")]
public IActionResult Eula() {
return Ok("eula test");
public async Task<IActionResult> Eula() {
User user = await new Database().Users.FirstOrDefaultAsync(u => u.Username == "jvyden");
return Ok($"You are logged in as user {user.Username} (id {user.UserId})\n{user.Serialize()}");
}
[HttpGet("announce")]

View file

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ProjectLighthouse.Types;
@ -8,5 +9,13 @@ namespace ProjectLighthouse {
ServerSettings.DbConnectionString,
MySqlServerVersion.LatestSupportedServerVersion
);
public async Task CreateUser(string username) {
await this.Database.ExecuteSqlRawAsync(
"INSERT INTO Users (Username, Biography) VALUES ({0}, {1})",
username, "");
}
public DbSet<User> Users { get; set; }
}
}

View file

@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using ProjectLighthouse.Serialization;
namespace ProjectLighthouse.Types {
[Keyless]
public class ClientsConnected {
public bool Lbp1 { get; set; }
public bool Lbp2 { get; set; }

View file

@ -5,11 +5,6 @@ namespace ProjectLighthouse.Types {
/// The location of a slot on a planet.
/// </summary>
public class Location {
public Location(int x, int y) {
this.X = x;
this.Y = y;
}
public int X;
public int Y;

View file

@ -1,7 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;
using ProjectLighthouse.Serialization;
namespace ProjectLighthouse.Types {
public class User {
public int UserId { get; set; }
public string Username { get; set; }
public string IconHash { get; set; }
public int Game { get; set; }
@ -19,11 +21,16 @@ namespace ProjectLighthouse.Types {
public int PhotosByMeCount { get; set; }
public int PhotosWithMeCount { get; set; }
public bool CommentsEnabled { get; set; }
/// <summary>
/// The location of the profile card on the user's earth
/// </summary>
public Location Location { get; set; }
// [NotMapped]
public Location Location = new() {
X = 0,
Y = 0
};
public int FavouriteSlotCount { get; set; }
public int FavouriteUserCount { get; set; }
public int lolcatftwCount { get; set; }
@ -31,7 +38,8 @@ namespace ProjectLighthouse.Types {
public int StaffChallengeGoldCount { get; set; }
public int StaffChallengeSilverCount { get; set; }
public int StaffChallengeBronzeCount { get; set; }
public ClientsConnected ClientsConnected { get; set; }
// [NotMapped]
public ClientsConnected ClientsConnected = new();
#region Slots