168 lines
6.1 KiB
C#
168 lines
6.1 KiB
C#
using DevExpress.Office.Crypto;
|
|
using DevExpress.Xpo.DB;
|
|
using DevExpress.XtraPrinting;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json.Linq;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Text.Json;
|
|
using TIAMWebApp.Shared.Application.Models;
|
|
using TIAMWebApp.Shared.Application.Models.PageModels;
|
|
using TIAMWebApp.Server.Models;
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TIAM.Database.DataLayers.Users;
|
|
using AyCode.Utils.Helpers;
|
|
using TIAM.Entities.Users;
|
|
using TIAMWebApp.Server.ModelsTIAMWebApp.Shared.Application.Models;
|
|
using TIAMWebApp.Shared.Application.Utility;
|
|
using TIAM.Entities.Auctions;
|
|
|
|
namespace TIAMWebApp.Server.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuctionAPIController : ControllerBase
|
|
{
|
|
private AuctionDal _auctionDal;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
PasswordHasher hasher = new PasswordHasher();
|
|
|
|
|
|
private readonly ILogger<UserAPIController> _logger;
|
|
|
|
public AuctionAPIController(ILogger<UserAPIController> logger, IConfiguration configuration, IWebHostEnvironment webHostEnvironment, AuctionDal auctionDal)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_webHostEnvironment = webHostEnvironment;
|
|
_auctionDal = auctionDal;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route("CreateBid")]
|
|
public async Task<IActionResult> CreateUser([FromBody] AuctionBid SerializedAuctionBidModel)
|
|
{
|
|
Console.WriteLine("CreateBid called");
|
|
//if (string.IsNullOrEmpty(SerializedAuctionBidModel.GetRawText()))
|
|
//{
|
|
// return BadRequest("SerializedAuctionBidModel is required");
|
|
//}
|
|
//else
|
|
//{
|
|
//AuctionBidModel? bid = JObject.Parse(SerializedAuctionBidModel.GetRawText()).ToObject<AuctionBidModel>();
|
|
AuctionBid bid = SerializedAuctionBidModel;
|
|
AuctionBid finalizedBidModel;
|
|
|
|
if(bid != null)
|
|
{
|
|
//add userModel to users array
|
|
//Array.Resize(ref users, users.Length + 1);
|
|
//users[users.Length - 1] = new UserModel(user.Email, user.PhoneNumber, user.Password);
|
|
|
|
var userId = bid.OwnerId;
|
|
var targetProductId = bid.TargetProductId;
|
|
string? email = bid?.Email;
|
|
string? phoneNumber = bid?.PhoneNumber;
|
|
int bidAmount = bid?.BidAmount ?? 0;
|
|
bool isValid = false;
|
|
|
|
if(userId == Guid.Empty || string.IsNullOrEmpty(email) || targetProductId==0 || bidAmount == 0)
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Bid to be created: {userId}, {targetProductId}, {email}, {phoneNumber}, {bidAmount}, {isValid}");
|
|
finalizedBidModel = new AuctionBid(userId, targetProductId, email, phoneNumber, bidAmount);
|
|
await _auctionDal.CreateBidAsync(finalizedBidModel);
|
|
return Ok(finalizedBidModel.Id);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
//}
|
|
}
|
|
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route("GetBids")]
|
|
public Task<List<AuctionBid>> GetBids()
|
|
{
|
|
//var users = await _userDal.Ctx.Users.ToListAsync();//.GetUsersAsync();
|
|
//return users;
|
|
return _auctionDal.GetBids();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route("GetBidsByEmail")]
|
|
public async Task<List<AuctionBid>> GetUserByEmail(string email)
|
|
{
|
|
return await _auctionDal.GetBidsByEmail(email);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route("ValidateBid")]
|
|
public async Task<IActionResult> ValidateBid([FromBody] AuctionBid SerializedAuctionBidModel)
|
|
{
|
|
Console.WriteLine("ValidateBid called");
|
|
//var validateBid = JObject.Parse(SerializedAuctionBidModel.GetRawText()).ToObject<AuctionBidModel>();
|
|
|
|
//check if bid exists
|
|
AuctionBid? dbBid = null;
|
|
|
|
//Console.WriteLine(validateBid?.Id);
|
|
Console.WriteLine(SerializedAuctionBidModel?.Id);
|
|
//if (validateBid != null)
|
|
if (SerializedAuctionBidModel != null)
|
|
{
|
|
//dbBid = await _auctionDal.GetBidById(validateBid.Id);
|
|
dbBid = await _auctionDal.GetBidById(SerializedAuctionBidModel.Id);
|
|
}
|
|
|
|
//check if password is valid
|
|
//bool isValidUser = await _userManager.CheckPasswordAsync(userModel, authenticateUser.Password);
|
|
|
|
//mocking
|
|
if (dbBid is null)
|
|
{
|
|
return Unauthorized("Not found in DB");
|
|
}
|
|
else
|
|
{
|
|
|
|
//if (dbBid.Email == validateBid?.Email)
|
|
if (dbBid.Email == SerializedAuctionBidModel?.Email)
|
|
{
|
|
Console.WriteLine("Bid is valid");
|
|
dbBid.IsValid = true;
|
|
//Update userModel with refreshToken!!
|
|
await _auctionDal.UpdateBidAsync(dbBid);
|
|
return Ok(dbBid.IsValid);
|
|
|
|
}
|
|
else
|
|
{
|
|
return Unauthorized("Emails not matching");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
} |