From b2428b6ba86f2fcc18a7a971c35d723ec7088fc4 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 6 Oct 2021 17:05:57 -0400 Subject: [PATCH] Store location in seperate table --- DatabaseMigrations/1.sql | 17 ++++++++++++++++ .../Controllers/MessageController.cs | 2 +- .../Controllers/UserController.cs | 12 +++++++++-- ProjectLighthouse/Database.cs | 20 +++++++++++++++++-- ProjectLighthouse/Types/Location.cs | 7 ++++--- ProjectLighthouse/Types/User.cs | 19 +++++++++++------- 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 DatabaseMigrations/1.sql diff --git a/DatabaseMigrations/1.sql b/DatabaseMigrations/1.sql new file mode 100644 index 00000000..1ab66fbc --- /dev/null +++ b/DatabaseMigrations/1.sql @@ -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; + diff --git a/ProjectLighthouse/Controllers/MessageController.cs b/ProjectLighthouse/Controllers/MessageController.cs index 91df7dcd..59407576 100644 --- a/ProjectLighthouse/Controllers/MessageController.cs +++ b/ProjectLighthouse/Controllers/MessageController.cs @@ -13,7 +13,7 @@ namespace ProjectLighthouse.Controllers { public async Task 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()}"); + return Ok($"You are logged in as user {user.Username} (id {user.UserId})"); } [HttpGet("announce")] diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index 00d10f36..48c1fec1 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -13,12 +13,20 @@ namespace ProjectLighthouse.Controllers { public class UserController : ControllerBase { [HttpGet("user/{username}")] public async Task 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(); return this.Ok(user.Serialize()); } + [HttpPost("user/{username}")] + public async Task CreateUser(string username) { + await new Database().CreateUser(username); + return await GetUser(username); + } + [HttpPost("updateUser")] public async Task UpdateUser() { 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(); } } diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 6995d260..bdab87bc 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -12,10 +12,26 @@ namespace ProjectLighthouse { public async Task CreateUser(string username) { await this.Database.ExecuteSqlRawAsync( - "INSERT INTO Users (Username, Biography) VALUES ({0}, {1})", - username, ""); + "INSERT INTO Locations (X, Y) VALUES ({0}, {1})", + 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 Users { get; set; } + public DbSet Locations { get; set; } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Location.cs b/ProjectLighthouse/Types/Location.cs index beba4ad5..9d50d346 100644 --- a/ProjectLighthouse/Types/Location.cs +++ b/ProjectLighthouse/Types/Location.cs @@ -5,12 +5,13 @@ namespace ProjectLighthouse.Types { /// The location of a slot on a planet. /// public class Location { - public int X; - public int Y; + public int Id { get; set; } + public int X { get; set; } + public int Y { get; set; } public string Serialize() { return LbpSerializer.StringElement("x", this.X) + - LbpSerializer.StringElement("x", this.Y); + LbpSerializer.StringElement("y", this.Y); } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index c1396fa8..cd7af5ca 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using ProjectLighthouse.Serialization; namespace ProjectLighthouse.Types { @@ -21,16 +22,20 @@ namespace ProjectLighthouse.Types { public int PhotosByMeCount { get; set; } public int PhotosWithMeCount { get; set; } public bool CommentsEnabled { get; set; } - + + public int LocationId { get; set; } + + private Location location; /// /// The location of the profile card on the user's earth /// -// [NotMapped] - public Location Location = new() { - X = 0, - Y = 0 - }; - + public Location Location { + get { + if(location != null) return this.location; + return location = new Database().Locations.First(l => l.Id == LocationId); + } + } + public int FavouriteSlotCount { get; set; } public int FavouriteUserCount { get; set; } public int lolcatftwCount { get; set; }