Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
9.2 kB
2
Indexable
Never
using Microsoft.AspNetCore.Components.Forms;

namespace DLudo_API.Controllers.Client
{
    [Route("api/Client/[controller]")]
    public class LoginController : Controller
    {
        private readonly IGameUserService _userService;
        private readonly IJwtService _jwtService;
        private readonly IStatisticService _statisticService;
        private readonly IAdminParameterService _adminParameterService;
        private readonly ITransactionService _transactionService;
        private readonly IS3Service _s3Service;

        public LoginController(IGameUserService userService, IJwtService jwtService, IStatisticService statisticService, IAdminParameterService adminParameterService, ITransactionService transactionService, IS3Service s3Service)
        {
            _userService = userService;
            _jwtService = jwtService;
            _statisticService = statisticService;
            _adminParameterService = adminParameterService;
            _transactionService = transactionService;
            _s3Service = s3Service;
        }

        [HttpPost("SignUpUser")]
        public async Task<ActionResult<BaseOutput>> SignUpUser([FromBody] SignUpUser_Input inputData)
        {
            var outputData = new BaseOutput();

            await _userService.SignUpUser(inputData.username, inputData.password, inputData.referralCode);

            return Ok(outputData);
        }

        [HttpPost("SignUpUserFiles")]
        public async Task<ActionResult<BaseOutput>> SignUpUserFiles([FromForm] string username, [FromForm] string password, [FromForm] string referralCode, IFormFile aadharCard_Front, IFormFile aadharCard_Back, IFormFile panCard, IFormFile photo)
        {
            var outputData = new BaseOutput();

            /*if (aadharCard_Front == null || aadharCard_Front.Length == 0)
                throw new Exception("Aadhar Card Front is required.");

            if (aadharCard_Back == null || aadharCard_Back.Length == 0)
                throw new Exception("Aadhar Card Back is required.");

            if (panCard == null || panCard.Length == 0)
                throw new Exception("Pan Card is required.");

            if (photo == null || photo.Length == 0)
                throw new Exception("Photo is required.");*/

            var user = await _userService.SignUpUser(username, password, referralCode);

            if (user == null || string.IsNullOrWhiteSpace(user.Id))
                throw new Exception("Something went wrong!!!");

            List<Task> tasks = new List<Task>();

            if (aadharCard_Front != null && aadharCard_Front.Length > 0)
                tasks.Add(_s3Service.UploadAadharFront(user.Id, aadharCard_Front));

            if (aadharCard_Back != null && aadharCard_Back.Length > 0)
                tasks.Add(_s3Service.UploadAadharBack(user.Id, aadharCard_Back));

            if (panCard != null && panCard.Length > 0)
                tasks.Add(_s3Service.UploadPan(user.Id, panCard));

            if (photo != null && photo.Length > 0)
                tasks.Add(_s3Service.UploadPhoto(user.Id, photo));

            foreach (var task in tasks)
            {
                await task.WaitAsync(new TimeSpan(0, 0, 8));
            }

            return Ok(outputData);
        }

        [HttpPost("VerifyUser")]
        public async Task<ActionResult<VerifyOtp_Output>> VerifyUser([FromBody] client.VerifyUser_Input inputData)
        {
            var outputData = new VerifyOtp_Output();

            var user = await _userService.VerifyUser(inputData.username, inputData.password);

            if (user != null)
            {
                if (!string.IsNullOrWhiteSpace(user.Id) && !string.IsNullOrWhiteSpace(user.Token))
                {
                    outputData.userId = user.Id;
                    outputData.token = _jwtService.GenerateJwtToken(user.Id, user.Token);

                    try
                    {
                        await _statisticService.UpdateStatistic(user.Id, 0);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                else
                {
                    throw new Exception("Something went wrong. Please try again later.");
                }
            }
            else
            {
                throw new Exception("Invalid Username or Password. Please try again.");
            }

            return Ok(outputData);
        }

        [HttpPost("GenerateOtp")]
        public async Task<ActionResult<BaseOutput>> GenerateOtp([FromBody] GenerateOtp_Input inputData)
        {
            var outputData = new BaseOutput();

            var otp = await _userService.GenerateOtp(inputData.mobile);

            if (string.IsNullOrEmpty(otp))
                throw new Exception("Could not generate OTP. Please try again later");

            return Ok(outputData);
        }

        [HttpPost("ValidateReferral")]
        public async Task<ActionResult<BaseOutput>> ValidateReferral([FromBody] ValidateReferral_Input inputData)
        {
            var outputData = new BaseOutput();

            var user = await _userService.ValidateReferral(inputData.referralCode);

            if (user == null)
            {
                throw new Exception("Invalid Referral Code.");
            }

            return Ok(outputData);
        }

        [HttpPost("VerifyOtp")]
        public async Task<ActionResult<VerifyOtp_Output>> VerifyOtp([FromBody] VerifyOtp_Input inputData)
        {
            var outputData = new VerifyOtp_Output();

            var hasToken = await _userService.HasToken(inputData.mobile);
            var user = await _userService.VerifyOtp(inputData.mobile, inputData.otp);

            if (user != null)
            {
                if (!string.IsNullOrWhiteSpace(user.Id) && !string.IsNullOrWhiteSpace(user.Token))
                {
                    outputData.userId = user.Id;
                    outputData.token = _jwtService.GenerateJwtToken(user.Id, user.Token);

                    try
                    {
                        await _statisticService.UpdateStatistic(user.Id, 0);

                        if (!hasToken)
                        {
                            await InitBonus(user.Id, inputData.referralCode);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                else
                {
                    throw new Exception("Something went wrong. Please try again later.");
                }
            }
            else
            {
                throw new Exception("Invalid OTP. Please try again.");
            }

            return Ok(outputData);
        }

        [HttpPost("GenerateGuestUser")]
        public async Task<ActionResult<GenerateGuestUser_Output>> GenerateGuestUser()
        {
            var outputData = new GenerateGuestUser_Output();

            var user = await _userService.GenerateGuestUser();

            if (user != null)
            {
                if (!string.IsNullOrWhiteSpace(user.Id) && !string.IsNullOrWhiteSpace(user.Token))
                {
                    outputData.userId = user.Id;
                    outputData.token = _jwtService.GenerateJwtToken(user.Id, user.Token);

                    try
                    {
                        await _statisticService.UpdateStatistic(user.Id, 0);
                        await InitBonus(user.Id, "");
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                else
                {
                    throw new Exception("Something went wrong. Please try again later.");
                }
            }
            else
            {
                throw new Exception("Couldn't Create Guest User. Please try again later.");
            }

            return Ok(outputData);
        }

        async Task CreditBonus(string userId, string description, string key, Dictionary<string, string> Parameters)
        {
            double bonus = 0;

            if (Parameters.ContainsKey(key) && double.TryParse(Parameters[key], out bonus))
            {
                await _transactionService.CreditBalance(userId, 0, 0, bonus, description, "bonus", 0);
            }
        }

        async Task InitBonus(string userId, string referralCode)
        {
            var Parameters = await _adminParameterService.GetAdminParameters();

            await CreditBonus(userId, "Welcome Bonus", "welcome_bonus", Parameters);

            var referrer = await _userService.ValidateReferral(referralCode);

            if (referrer != null && !string.IsNullOrWhiteSpace(referrer.Id))
            {
                await CreditBonus(referrer.Id, "Referral Bonus", "referrer_bonus", Parameters);
                await CreditBonus(userId, "Referral Bonus", "referee_bonus", Parameters);
            }
        }
    }
}
Leave a Comment