From 44194a05ac31d80f97f919b45b3eab09ce4e4289 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 7 Oct 2021 00:59:55 -0400 Subject: [PATCH] Allow comment fetching and receiving --- DatabaseMigrations/3.sql | 21 ++++++++ .../Controllers/CommentController.cs | 48 +++++++++++++++++++ .../Controllers/LoginController.cs | 2 +- ProjectLighthouse/Database.cs | 1 + ProjectLighthouse/Types/Comment.cs | 36 ++++++++++++-- 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 DatabaseMigrations/3.sql create mode 100644 ProjectLighthouse/Controllers/CommentController.cs diff --git a/DatabaseMigrations/3.sql b/DatabaseMigrations/3.sql new file mode 100644 index 00000000..26d25f31 --- /dev/null +++ b/DatabaseMigrations/3.sql @@ -0,0 +1,21 @@ +create table Comments +( + CommentId int, + PosterUserId int not null, + TargetUserId int not null, + Timestamp bigint not null, + ThumbsUp int default 0 not null, + ThumbsDown int default 0 not null, + Message longtext not null +); + +create unique index Comments_CommentId_uindex + on Comments (CommentId); + +alter table Comments + add constraint Comments_pk + primary key (CommentId); + +alter table Comments + modify CommentId int auto_increment; + diff --git a/ProjectLighthouse/Controllers/CommentController.cs b/ProjectLighthouse/Controllers/CommentController.cs new file mode 100644 index 00000000..8698fcb4 --- /dev/null +++ b/ProjectLighthouse/Controllers/CommentController.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Xml.Serialization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using ProjectLighthouse.Serialization; +using ProjectLighthouse.Types; + +namespace ProjectLighthouse.Controllers { + [ApiController] + [Route("LITTLEBIGPLANETPS3_XML/")] + [Produces("text/xml")] + public class CommentController : ControllerBase { + [HttpGet("userComments/{username}")] + public async Task GetComments(string username) { + // the following is downright retarded, but its 12:48am and i do not give a shit + // ↓ ok... ↓ why does this need to be wrapped ↓ again???? whyyyy + List comments = (await new Database().Comments.ToListAsync()).Where(c => c.TargetUsername == username).ToList(); + + string outputXml = comments.Aggregate(string.Empty, (current, comment) => current + comment.Serialize()); + return this.Ok(LbpSerializer.StringElement("comments", outputXml)); + } + + [HttpPost("postUserComment/{username}")] + public async Task PostComment(string username) { + Request.Body.Position = 0; + string bodyString = await new StreamReader(Request.Body).ReadToEndAsync(); + + XmlSerializer serializer = new(typeof(Comment)); + Comment comment = (Comment)serializer.Deserialize(new StringReader(bodyString)); + + await using Database database = new(); + User poster = await database.Users.FirstOrDefaultAsync(u => u.Username == "jvyden"); + User target = await database.Users.FirstOrDefaultAsync(u => u.Username == username); + + comment.PosterUserId = poster.UserId; + comment.TargetUserId = target.UserId; + + database.Comments.Add(comment); + await database.SaveChangesAsync(); + + return this.Ok(); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Controllers/LoginController.cs b/ProjectLighthouse/Controllers/LoginController.cs index 49d5ddde..d5cae68f 100644 --- a/ProjectLighthouse/Controllers/LoginController.cs +++ b/ProjectLighthouse/Controllers/LoginController.cs @@ -15,7 +15,7 @@ namespace ProjectLighthouse.Controllers { this.BadRequest(); }; - string titleId = titleValues[0]; +// string titleId = titleValues[0]; return this.Ok(new LoginResult { AuthTicket = "d2c6bbec59162a1e786ed24ad95f2b73", diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 0fc6bb3b..5e4396ca 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -34,5 +34,6 @@ namespace ProjectLighthouse { public DbSet Users { get; set; } public DbSet Locations { get; set; } public DbSet Slots { get; set; } + public DbSet Comments { get; set; } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Comment.cs b/ProjectLighthouse/Types/Comment.cs index 55f1f9b0..1faf6dda 100644 --- a/ProjectLighthouse/Types/Comment.cs +++ b/ProjectLighthouse/Types/Comment.cs @@ -1,19 +1,49 @@ using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Xml.Serialization; using ProjectLighthouse.Serialization; namespace ProjectLighthouse.Types { + [XmlRoot("comment"), XmlType("comment")] public class Comment { - [Key] public int CommentId { get; set; } + [Key] + [XmlAttribute("id")] + public int CommentId { get; set; } + + public int PosterUserId { get; set; } + + public int TargetUserId { get; set; } + + private string posterUsername; + +// [XmlAttribute("username")] + public string PosterUsername { + get { + if(this.posterUsername != null) return this.posterUsername; + return this.posterUsername = new Database().Users.First(u => u.UserId == PosterUserId).Username; + } + } + + private string targetUsername; + + public string TargetUsername { + get { + if(this.targetUsername != null) return this.targetUsername; + + return this.targetUsername = new Database().Users.First(u => u.UserId == TargetUserId).Username; + } + } - public string Username { get; set; } public long Timestamp { get; set; } + + [XmlElement("message")] public string Message { get; set; } public int ThumbsUp { get; set; } public int ThumbsDown { get; set; } private string serialize() { return LbpSerializer.StringElement("id", CommentId) + - LbpSerializer.StringElement("npHandle", Username) + + LbpSerializer.StringElement("npHandle", this.PosterUsername) + LbpSerializer.StringElement("timestamp", Timestamp) + LbpSerializer.StringElement("message", Message) + LbpSerializer.StringElement("thumbsup", ThumbsUp) +