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.

135 lines
7.7 KiB

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.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using Teso_API.Models;
namespace Teso_API.Controllers
{
[AllowAnonymous, Route("tesobusiness")]
[ApiController]
public class TesoBusinessController : ControllerBase
{
private readonly TESOContext _context;
public TesoBusinessController(TESOContext context)
{
_context = context;
}
[Authorize]
[Route("available"), HttpGet]
public async Task<ActionResult<IEnumerable<TesoBusinessDetail>>> GetTesoBusinessDetails()
{
List<string> coupons = await _context.CouponsHeads.AsQueryable().Where(b => b.State == "active" && b.Expiration > DateTime.Now && b.Quantity > 0).Select(b => b.BusinessId).ToListAsync();
List<TesoBusinessDetail> businessDetails = new List<TesoBusinessDetail>();
businessDetails = await _context.TesoBusinessDetails.FromSqlRaw("Select b.businessID,b.handle,b.businessName,b.businessTIN,b.businessDescription,c.categoryName as businessCategory,b.businessAddress," +
"b.businessContact,b.businessLogo,b.dateOfEst,b.businessEmail,b.businessLAT,b.businessLNG from TesoBusinessDetail b Inner Join BusinessCategory c on c.categoryCode = b.businessCategory")
.AsQueryable().Where(b => coupons.Contains(b.BusinessId) && b.BusinessLat != null && b.BusinessLng != null).ToListAsync();
return businessDetails;
}
[Route("lookupBusiness"), HttpPost]
public async Task<ActionResult<IEnumerable<TesoBusinessDetail>>> GetTesoBusinessDetail([FromBody] string businessName)
{
return await _context.TesoBusinessDetails.FromSqlRaw("Select b.businessID,b.handle,b.businessName,b.businessTIN,b.businessDescription,c.categoryName as businessCategory,b.businessAddress," +
"b.businessContact,b.businessLogo,b.dateOfEst,b.businessEmail,b.businessLAT,b.businessLNG from TesoBusinessDetail b Inner Join BusinessCategory c on c.categoryCode = b.businessCategory")
.AsQueryable().Where(b => b.BusinessName.ToLower().Trim().Contains(businessName.Trim().ToLower()))
.ToListAsync();
}
[Authorize]
[Route("profile"), HttpPost]
public async Task<ActionResult<BusinessProfile>> GetProfile([FromBody] string businessID)
{
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;
BusinessProfile profile = new BusinessProfile();
profile.products = await _context.Products.AsQueryable().Where(b => b.BusinessId == businessID).ToListAsync();
var param = new SqlParameter[] {
new SqlParameter()
{
ParameterName = "@businessID",
SqlDbType = System.Data.SqlDbType.VarChar,
Direction = System.Data.ParameterDirection.Input,
Value = businessID
}
};
profile.subscribers = await (from b in _context.TesoBusinessDetails.AsQueryable()
join s in _context.RelationsDetails on b.BusinessId equals s.BeneficiaryRecipient into subscribers
from sub in subscribers.DefaultIfEmpty()
join u in _context.TesoUserDetails on sub.BeneficiaryRequester equals u.UserGUID into users
from U in users.DefaultIfEmpty()
join e in _context.UserFinances on U.UserGUID equals e.UserGUID into finances
from fnc in finances.DefaultIfEmpty()
where b.BusinessId == businessID
select new TesoUser()
{
username = U.Username,
address = U.Address,
country = U.Country,
thumbnail_dp = U.ThumbnailDp,
DateOfBirth = U.DateOfBirth,
description = U.Description,
email = U.Email,
firstname = U.Firstname,
friends = "0",
gender = U.Gender,
gold = fnc.UserGUID != null ? fnc.Gold.ToString() : "0",
silver = fnc.UserGUID != null ? fnc.Silver.ToString() : "0",
lastname = U.Surname,
phonenumber = U.Phonenumber.ToString(),
userGUID = U.UserGUID
}
).OrderBy(p => p.firstname).ToListAsync();
if (profile.subscribers.AsQueryable().Select(t => t.userGUID).ToList().Contains(userID))
{
profile.subscribed = true;
}
else
{
profile.subscribed = false;
}
profile.coupons = await (from b in _context.TesoBusinessDetails.AsQueryable()
join c in _context.CouponsHeads on b.BusinessId equals c.BusinessId into coupons
from coupon in coupons.DefaultIfEmpty()
join p in _context.Products on coupon.TargetProduct equals p.ProductId
join con in _context.CouponConditions on coupon.CouponId equals con.CouponId into conditions
from cc in conditions.DefaultIfEmpty()
where b.BusinessId == businessID
select new CouponDetails()
{
BusinessId = b.BusinessId,
CouponId = coupon.CouponId,
condition = cc.Condition,
countID = coupon.CouponId+coupon.BusinessId,
Type = coupon.Type,
Expiration = coupon.Expiration,
Issuer = b,
lowerLimit = coupon.LowerLimit,
ProductCost = p.UnitPrice.ToString(),
Quantity = coupon.Quantity,
State = coupon.State,
Target = p,
upperLimit = coupon.UpperLimit,
Worth = coupon.LowerLimit
}).ToListAsync();
return profile;
}
}
}