using LotteryWebApp.Common; using LotteryWebApp.Extensions; using LotteryWebApp.Languages; using LotteryWebApp.Models; using LotteryWebApp.Service; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Globalization; namespace LotteryWebApp.Controllers { [AutoValidateAntiforgeryToken] public class ProfileController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); IConfiguration configuration; private readonly IWebHostEnvironment webHostEnvironment; APIFunctions api = new APIFunctions(); public ProfileController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) { configuration = _configuration; webHostEnvironment = hostEnvironment; } public String GetParameter(String key) { return configuration.GetSection(key).Value; } public IActionResult Index() { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } HttpContext.Session.SetComplexData("navigator", Constants.PROFILE_NAVIGATOR); ProfileViewModel model = new ProfileViewModel(); try { model.channel = GetParameter(Constants.CHANNEL); // load msisdn String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); Profile profile = HttpContext.Session.GetComplexData("profile"); if (profile == null) { // load profile UserGetProfileRequest request = new UserGetProfileRequest(); request.users = msisdn; request.token = token; Profile profileGet = api.UserLoadProfileApi(configuration, request); if (profileGet.status == Code.SESSION_EXPIRED) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } model.profile = profileGet; HttpContext.Session.SetComplexData("profile", profileGet); } else { model.profile = profile; } UserStatus userStatus = HttpContext.Session.GetComplexData("userStatus"); if (profile == null) { // load profile UserStatusRequest userStatusRequest = new UserStatusRequest(); userStatusRequest.users = msisdn; userStatusRequest.token = token; UserStatus userStatusResponse = api.GetUserStatusApi(configuration, userStatusRequest); if (userStatusResponse.status == Code.SESSION_EXPIRED) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } model.userStatus = userStatusResponse; HttpContext.Session.SetComplexData("userStatus", userStatusResponse); } else { model.userStatus = userStatus; } } catch (Exception ex) { log.Error(ex); } return View(model); } public IActionResult ChangePassword(string code) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } ProfileChangePassword_ViewModel model = new ProfileChangePassword_ViewModel(); model.code = code; return View(model); } [ValidateAntiForgeryToken] public JsonResult ChangePassword_Action(string oldpass, string newpass, string confirmpass) { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); if (newpass != null && newpass != "" && newpass == confirmpass) { // update RegisterRequest registerRequest = new RegisterRequest(); registerRequest.Msisdn = msisdn; registerRequest.Users = msisdn; registerRequest.pass = oldpass; registerRequest.Passnew = newpass; registerRequest.token = token; RegisterResponse updateProfile = api.UserChangePasswordApi(configuration, registerRequest); return Json(new { code = int.Parse(updateProfile.status), message = GetLangFromCode(updateProfile.status), }); } else { return Json(new { code = Code.FAILURE, message = Lang.new_pass_error }); } } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } public IActionResult ProfileInfo(String code) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } ProfileProfileInfo_ViewModel model = new ProfileProfileInfo_ViewModel(); try { model.code = code; Profile profile = HttpContext.Session.GetComplexData("profile"); model.profile = profile; } catch (Exception ex) { log.Error(ex); } return View(model); } [ValidateAntiForgeryToken] public JsonResult ProfileInfoUpdate_Action(string fullname, string birthday) { try { String msisdn = HttpContext.Session.GetComplexData("msisdn"); String token = HttpContext.Session.GetComplexData("token"); Profile profile = HttpContext.Session.GetComplexData("profile"); String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy"); // update UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest(); userUpdateProfileRequest.users = msisdn; userUpdateProfileRequest.fullName = fullname; userUpdateProfileRequest.birthday = birthdayFormat; userUpdateProfileRequest.picture = profile.realPicture; userUpdateProfileRequest.token = token; UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest); if (updateProfile.status == Code.SUCCESS) { // update profile profile.fullName = fullname; profile.birthday = birthdayFormat; HttpContext.Session.SetComplexData("profile", profile); } return Json(new { code = int.Parse(updateProfile.status), message = GetLangFromCode(updateProfile.status) }); } catch (Exception ex) { log.Error(ex); } return Json(new { code = Code.FAILURE, message = Lang.error_happened }); } public IActionResult HowToPlay(string termType) { if (!CheckAuthToken()) { return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login"); } ProfileHowToPlay_ViewModel model = new ProfileHowToPlay_ViewModel(); model.termType = termType != null ? termType : Constants.GameGroup.Singapore; return View(model); } } }