Allow comment fetching and receiving

This commit is contained in:
jvyden 2021-10-07 00:59:55 -04:00
parent 897f709288
commit 44194a05ac
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 104 additions and 4 deletions

21
DatabaseMigrations/3.sql Normal file
View file

@ -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;

View file

@ -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<IActionResult> 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<Comment> 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<IActionResult> 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();
}
}
}

View file

@ -15,7 +15,7 @@ namespace ProjectLighthouse.Controllers {
this.BadRequest(); this.BadRequest();
}; };
string titleId = titleValues[0]; // string titleId = titleValues[0];
return this.Ok(new LoginResult { return this.Ok(new LoginResult {
AuthTicket = "d2c6bbec59162a1e786ed24ad95f2b73", AuthTicket = "d2c6bbec59162a1e786ed24ad95f2b73",

View file

@ -34,5 +34,6 @@ namespace ProjectLighthouse {
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<Location> Locations { get; set; } public DbSet<Location> Locations { get; set; }
public DbSet<Slot> Slots { get; set; } public DbSet<Slot> Slots { get; set; }
public DbSet<Comment> Comments { get; set; }
} }
} }

View file

@ -1,19 +1,49 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Xml.Serialization;
using ProjectLighthouse.Serialization; using ProjectLighthouse.Serialization;
namespace ProjectLighthouse.Types { namespace ProjectLighthouse.Types {
[XmlRoot("comment"), XmlType("comment")]
public class 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; } public long Timestamp { get; set; }
[XmlElement("message")]
public string Message { get; set; } public string Message { get; set; }
public int ThumbsUp { get; set; } public int ThumbsUp { get; set; }
public int ThumbsDown { get; set; } public int ThumbsDown { get; set; }
private string serialize() { private string serialize() {
return LbpSerializer.StringElement("id", CommentId) + return LbpSerializer.StringElement("id", CommentId) +
LbpSerializer.StringElement("npHandle", Username) + LbpSerializer.StringElement("npHandle", this.PosterUsername) +
LbpSerializer.StringElement("timestamp", Timestamp) + LbpSerializer.StringElement("timestamp", Timestamp) +
LbpSerializer.StringElement("message", Message) + LbpSerializer.StringElement("message", Message) +
LbpSerializer.StringElement("thumbsup", ThumbsUp) + LbpSerializer.StringElement("thumbsup", ThumbsUp) +