mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-06 08:02:27 +00:00
Allow comment fetching and receiving
This commit is contained in:
parent
897f709288
commit
44194a05ac
5 changed files with 104 additions and 4 deletions
21
DatabaseMigrations/3.sql
Normal file
21
DatabaseMigrations/3.sql
Normal 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;
|
||||||
|
|
48
ProjectLighthouse/Controllers/CommentController.cs
Normal file
48
ProjectLighthouse/Controllers/CommentController.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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",
|
||||||
|
|
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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) +
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue