Store location in seperate table

This commit is contained in:
jvyden 2021-10-06 17:05:57 -04:00
commit b2428b6ba8
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
6 changed files with 62 additions and 15 deletions

17
DatabaseMigrations/1.sql Normal file
View file

@ -0,0 +1,17 @@
create table ProfileCardLocations
(
UserId int not null,
LocationX int not null,
LocationY int not null
);
create unique index ProfileCardLocations_UserId_uindex
on Locations (Id);
alter table Locations
add constraint ProfileCardLocations_pk
primary key (Id);
alter table Users
add LocationId int null;

View file

@ -13,7 +13,7 @@ namespace ProjectLighthouse.Controllers {
public async Task<IActionResult> Eula() { public async Task<IActionResult> Eula() {
User user = await new Database().Users.FirstOrDefaultAsync(u => u.Username == "jvyden"); 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()}"); return Ok($"You are logged in as user {user.Username} (id {user.UserId})");
} }
[HttpGet("announce")] [HttpGet("announce")]

View file

@ -13,12 +13,20 @@ namespace ProjectLighthouse.Controllers {
public class UserController : ControllerBase { public class UserController : ControllerBase {
[HttpGet("user/{username}")] [HttpGet("user/{username}")]
public async Task<IActionResult> GetUser(string username) { public async Task<IActionResult> GetUser(string username) {
User user = await new Database().Users.FirstOrDefaultAsync(u => u.Username == username); await using Database database = new();
User user = await database.Users.FirstOrDefaultAsync(u => u.Username == username);
if(user == null) return this.NotFound(); if(user == null) return this.NotFound();
return this.Ok(user.Serialize()); return this.Ok(user.Serialize());
} }
[HttpPost("user/{username}")]
public async Task<IActionResult> CreateUser(string username) {
await new Database().CreateUser(username);
return await GetUser(username);
}
[HttpPost("updateUser")] [HttpPost("updateUser")]
public async Task<IActionResult> UpdateUser() { public async Task<IActionResult> UpdateUser() {
await using Database database = new(); await using Database database = new();
@ -52,7 +60,7 @@ namespace ProjectLighthouse.Controllers {
} }
} }
await database.SaveChangesAsync(); if(database.ChangeTracker.HasChanges()) await database.SaveChangesAsync();
return this.Ok(); return this.Ok();
} }
} }

View file

@ -12,10 +12,26 @@ namespace ProjectLighthouse {
public async Task CreateUser(string username) { public async Task CreateUser(string username) {
await this.Database.ExecuteSqlRawAsync( await this.Database.ExecuteSqlRawAsync(
"INSERT INTO Users (Username, Biography) VALUES ({0}, {1})", "INSERT INTO Locations (X, Y) VALUES ({0}, {1})",
username, ""); 0, 0);
Location l = new() {
X = 0,
Y = 0
};
this.Locations.Add(l);
await this.SaveChangesAsync();
int locationId = l.Id;
await this.Database.ExecuteSqlRawAsync(
"INSERT INTO Users (Username, Biography, Pins, LocationId) VALUES ({0}, {1}, {2}, {3})",
username, "No biography provided.", "", locationId);
} }
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<Location> Locations { get; set; }
} }
} }

View file

@ -5,12 +5,13 @@ namespace ProjectLighthouse.Types {
/// The location of a slot on a planet. /// The location of a slot on a planet.
/// </summary> /// </summary>
public class Location { public class Location {
public int X; public int Id { get; set; }
public int Y; public int X { get; set; }
public int Y { get; set; }
public string Serialize() { public string Serialize() {
return LbpSerializer.StringElement("x", this.X) + return LbpSerializer.StringElement("x", this.X) +
LbpSerializer.StringElement("x", this.Y); LbpSerializer.StringElement("y", this.Y);
} }
} }
} }

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using ProjectLighthouse.Serialization; using ProjectLighthouse.Serialization;
namespace ProjectLighthouse.Types { namespace ProjectLighthouse.Types {
@ -21,16 +22,20 @@ namespace ProjectLighthouse.Types {
public int PhotosByMeCount { get; set; } public int PhotosByMeCount { get; set; }
public int PhotosWithMeCount { get; set; } public int PhotosWithMeCount { get; set; }
public bool CommentsEnabled { get; set; } public bool CommentsEnabled { get; set; }
public int LocationId { get; set; }
private Location location;
/// <summary> /// <summary>
/// The location of the profile card on the user's earth /// The location of the profile card on the user's earth
/// </summary> /// </summary>
// [NotMapped] public Location Location {
public Location Location = new() { get {
X = 0, if(location != null) return this.location;
Y = 0 return location = new Database().Locations.First(l => l.Id == LocationId);
}; }
}
public int FavouriteSlotCount { get; set; } public int FavouriteSlotCount { get; set; }
public int FavouriteUserCount { get; set; } public int FavouriteUserCount { get; set; }
public int lolcatftwCount { get; set; } public int lolcatftwCount { get; set; }