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.
76 lines
3.1 KiB
76 lines
3.1 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.Data.SqlClient;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
using Microsoft.Net.Http.Headers;
|
||
|
using Teso_API.Models;
|
||
|
|
||
|
namespace Teso_API.Controllers
|
||
|
{
|
||
|
[AllowAnonymous, Route("adverts")]
|
||
|
[ApiController]
|
||
|
public class CampaignsController : ControllerBase
|
||
|
{
|
||
|
private readonly TESOContext _context;
|
||
|
|
||
|
public CampaignsController(TESOContext context)
|
||
|
{
|
||
|
_context = context;
|
||
|
}
|
||
|
|
||
|
[Authorize]
|
||
|
[Route("businesscampaigns"), HttpPost]
|
||
|
public async Task<IEnumerable<Campaign>> PullCampaign([FromBody] string searchKey)
|
||
|
{
|
||
|
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;
|
||
|
|
||
|
var param = new SqlParameter[]
|
||
|
{
|
||
|
new SqlParameter
|
||
|
{
|
||
|
ParameterName = "@user",
|
||
|
SqlValue = userID,
|
||
|
SqlDbType = System.Data.SqlDbType.VarChar,
|
||
|
Direction = System.Data.ParameterDirection.Input
|
||
|
},
|
||
|
new SqlParameter
|
||
|
{
|
||
|
ParameterName = "@status",
|
||
|
SqlValue = "ended",
|
||
|
SqlDbType = System.Data.SqlDbType.VarChar,
|
||
|
Direction = System.Data.ParameterDirection.Input
|
||
|
},
|
||
|
new SqlParameter
|
||
|
{
|
||
|
ParameterName = "@searchKey",
|
||
|
SqlValue = searchKey,
|
||
|
SqlDbType = System.Data.SqlDbType.VarChar,
|
||
|
Direction = System.Data.ParameterDirection.Input
|
||
|
},
|
||
|
};
|
||
|
List<Campaign> campaigns;
|
||
|
|
||
|
campaigns = await _context.Campaigns.FromSqlRaw("Select C.title,C.campaignID,C.description,C.rewards,'active' as Status,p.productImage as targetProduct," +
|
||
|
"b.businessName as businessID,C.startDate, C.endDate from Campaigns C Inner Join Products p on C.targetProduct = p.productID " +
|
||
|
"Inner Join TesoBusinessDetail b on b.businessID = C.businessID where campaignID not in (Select campaignID from CampAds ca Inner " +
|
||
|
"Join Posts pts on pts.postID = ca.postID where pts.publisherID = @user and (ca.approved = 1 or ca.approved = NULL)) and " +
|
||
|
"C.status != @status and C.startDate < GETDATE() and (b.businessName Like '%' + @searchKey + '%' or C.title Like '%' + @searchKey + '%') " +
|
||
|
"group by C.title,C.campaignID,C.description,C.rewards,p.productImage,b.businessName,C.startDate,C.endDate", param).ToListAsync();
|
||
|
|
||
|
return campaigns;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|