Backend for the Teso project written in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.2 KiB

3 months ago
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using Teso_API.Models;
namespace Teso_API.Controllers
{
[AllowAnonymous, Route("user_details")]
[ApiController]
public class TesoUserDetailsController : ControllerBase
{
private readonly TESOContext _context;
public TesoUserDetailsController(TESOContext context)
{
_context = context;
}
[Authorize]
[Route("pullInformation"), HttpGet]
public async Task<ActionResult<TesoUser>> GetTesoUserDetail()
{
var accessToken = Request.Headers[HeaderNames.Authorization];
string token = accessToken;
token = token.Substring(6).Trim();
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
string userID = jwtToken.Claims.First(claim => claim.Type == "userGUID").Value;
TesoUserDetail detail = await _context.TesoUserDetails.AsQueryable().Where(usr => usr.UserGUID == userID).FirstOrDefaultAsync();
int friends = await _context.Relationships.AsQueryable().Where(t => t.UserGuid == userID).CountAsync();
UserFinance finance = await _context.UserFinances.AsQueryable().Where(usr => usr.UserGUID == userID).FirstOrDefaultAsync();
if (finance == null)
{
finance = new UserFinance();
finance.Gold = 0;
finance.Silver = 0;
}
TesoUser tesouser = new TesoUser();
tesouser.userGUID = detail.UserGUID;
tesouser.username = detail.Username;
tesouser.firstname = detail.Firstname;
tesouser.lastname = detail.Surname;
tesouser.description = detail.Description;
tesouser.email = detail.Email;
tesouser.phonenumber = detail.Phonenumber.HasValue ? detail.Phonenumber.Value.ToString() : "";
tesouser.address = detail.Address;
tesouser.thumbnail_dp = detail.ThumbnailDp;
tesouser.DateOfBirth = detail.DateOfBirth;
tesouser.country = detail.Country;
tesouser.gender = detail.Gender;
tesouser.gold = finance.Gold.ToString();
tesouser.silver = finance.Silver.ToString();
tesouser.friends = friends.ToString();
return tesouser;
}
[Authorize]
[Route("pullProfile"), HttpPost]
public async Task<ActionResult<ThirdPerson>> GetProfile([FromBody] string tesouser)
{
var accessToken = Request.Headers[HeaderNames.Authorization];
string token = accessToken;
token = token.Substring(6).Trim();
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
string userID = jwtToken.Claims.First(claim => claim.Type == "userGUID").Value;
ThirdPerson thirdPerson = new ThirdPerson();
List<string> friends = await _context.Relationships.AsQueryable().Where(t => t.UserGuid == tesouser).Select(t => t.RelationId).ToListAsync();
thirdPerson.friends = await _context.Relationships.AsQueryable().Where(t => friends.Contains(t.RelationId) && t.UserGuid != tesouser).
Select(u => u.UserGuid).ToListAsync();
bool sentPending = _context.RelationsDetails.Any(f => f.BeneficiaryRequester == userID && f.BeneficiaryRecipient == tesouser && f.Status == "pending");
bool receivedPending = _context.RelationsDetails.Any(f => f.BeneficiaryRequester == tesouser && f.BeneficiaryRecipient == userID && f.Status == "pending");
bool friendship = thirdPerson.friends.Any(t => t.Contains(userID));
if (!sentPending && !receivedPending && !friendship)
{
thirdPerson.relation = "not related";
}
else if (sentPending && !receivedPending && !friendship)
{
thirdPerson.relation = "sent pending";
}
else if (!sentPending && receivedPending && !friendship)
{
thirdPerson.relation = "received pending";
}
else
{
thirdPerson.relation = "friends";
}
thirdPerson.posts = await _context.Posts.AsQueryable().Where(i => i.PublisherId == tesouser).Select(e => new Post
{
Aspect = e.Aspect,
PlaybackID = e.PlaybackID,
PostId = e.PostId,
PublisherId = e.PublisherId,
AssetID = e.AssetID,
Rendition = e.Rendition,
Timestamp = e.Timestamp,
Title = e.Title
}).OrderByDescending(t => t.Timestamp).ToListAsync();
thirdPerson.following = await _context.RelationsDetails.AsQueryable().Where(p => p.Status == "subscribed").Select(s => s.BeneficiaryRecipient).ToListAsync();
return thirdPerson;
}
}
}