mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-10-04 07:09:58 +00:00
Fully implement photos for users
This commit is contained in:
parent
143fc96cf5
commit
9b7686bb91
8 changed files with 679 additions and 6 deletions
|
@ -1,3 +1,4 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
@ -25,14 +26,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
|||
[HttpPost("uploadPhoto")]
|
||||
public async Task<IActionResult> UploadPhoto()
|
||||
{
|
||||
User? user = await this.database.UserFromRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
XmlSerializer serializer = new(typeof(Photo));
|
||||
Photo photo = (Photo)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
Photo? photo = (Photo?)serializer.Deserialize(new StringReader(bodyString));
|
||||
if (photo == null) return this.BadRequest();
|
||||
|
||||
photo.CreatorId = user.UserId;
|
||||
photo.Creator = user;
|
||||
|
||||
foreach (PhotoSubject subject in photo.Subjects)
|
||||
{
|
||||
subject.User = await this.database.Users.FirstOrDefaultAsync(u => u.Username == subject.Username);
|
||||
|
@ -59,10 +65,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
|||
[HttpGet("photos/user/{id:int}")]
|
||||
public async Task<IActionResult> SlotPhotos(int id)
|
||||
{
|
||||
List<Photo> photos = await this.database.Photos.Take(50).ToListAsync();
|
||||
|
||||
List<Photo> photos = await this.database.Photos.Take(10).ToListAsync();
|
||||
string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(id));
|
||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
||||
}
|
||||
|
||||
[HttpGet("photos/by")]
|
||||
public async Task<IActionResult> UserPhotos([FromQuery] string user)
|
||||
{
|
||||
User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user);
|
||||
if (user == null) return this.NotFound();
|
||||
|
||||
List<Photo> photos = await this.database.Photos.Where(p => p.CreatorId == userFromQuery.UserId).Take(10).ToListAsync();
|
||||
string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0));
|
||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue