public IHttpActionResult QRLogin(QRRequest request) { try { if (string.IsNullOrEmpty(request.QrCode)) { return(BadRequest("QR code is required")); } var qrCodeID = request.QrCode.Split('?'); string[] stringSeparators = new string[] { "Num" }; var splitCode = qrCodeID[1].Split(stringSeparators, StringSplitOptions.None); var qrId = splitCode[0].Split('=')[1]; var model = new QRLoginUserModel { Id = qrId }; var apiresult = new CommonModels(). Post <QRLoginUserModel>("Personnel_InfoClass", model); apiresult.Wait(); if (apiresult.Result.IsSuccessStatusCode) { var readTask = apiresult.Result.Content.ReadAsAsync <QRLoginUserModel>(); readTask.Wait(); var info = readTask.Result; if (info.IsActive) { string token = CreateToken(info.FirstName + "." + info.LastName); Logger.Log(info.FirstName + "." + info.LastName + " Logged in"); return(Ok(new UserDetails() { Firstname = info.FirstName, Lastname = info.LastName, Username = info.FirstName + "." + info.LastName, IsAdmin = false, Token = token, TokenValidity = DateTime.UtcNow.AddDays(Convert.ToInt16(ConfigurationManager.AppSettings["jwtValidity"])), IsActive = true })); } else { Logger.Log(info.FirstName + "." + info.LastName + " is inactive"); return(BadRequest("User is inactive")); } } else { return(BadRequest("Saleforce server unable to validate")); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> Post([FromBody] QRRequest request) { _logger.LogInformation($"GET QR with id {request.QRCode}"); var qr = await _context.QR .Where(q => q.QRId == request.QRCode) .FirstOrDefaultAsync(); // Check QR exists if (qr == default(QR)) { return(NotFound(new Response() { Message = $"No QR code {request.QRCode}", Status = "404" })); // Check QR is not expired } else if (qr.Expiry < DateTime.Now) { return(BadRequest(new Response() { Message = $"QR code {request.QRCode} has expired", Status = "400" })); } _logger.LogInformation($"GET Users {request.UserId}"); var user = await _context.AppUser .Where(use => use.Id == request.UserId.ToString()) .FirstOrDefaultAsync(); // Check User exists if (user == default(AppUser)) { return(NotFound(new Response() { Message = $"No user exists with id {request.UserId}", Status = "404" })); } _logger.LogInformation($"GET History for QR code {request.QRCode} and User with id {request.UserId}"); var history = await _context.History .Where(hist => hist.UserId == request.UserId.ToString() && hist.ContentId == qr.ContentId) .FirstOrDefaultAsync(); // Check user has not already visited content if (history != default(History)) { return(BadRequest(new Response() { Message = $"User with id {request.UserId} has already been to this location", Status = "400" })); } // Add new history history = new History() { ContentId = qr.ContentId, Date = DateTime.Now, UserId = request.UserId.ToString() }; _context.History.Add(history); // Update CityUser var cityId = await _context.Content .Where(con => con.ContentId == qr.ContentId) .Select(con => con.CityId) .FirstOrDefaultAsync(); var cityUser = await _context.CityUser .Where(cuse => cuse.CityId == cityId) .FirstOrDefaultAsync(); if (cityUser == default(CityUser)) { _context.CityUser.Add(new CityUser() { CityId = cityId, UserId = request.UserId.ToString(), Count = 1 }); } else { cityUser.Count++; } // Update UserRewards var newRewards = await _context.Reward .Where(rew => rew.CityId == cityId && rew.CountThreshold == cityUser.Count) .ToListAsync(); foreach (Reward reward in newRewards) { _context.UserReward.Add(new UserReward() { UserId = request.UserId.ToString(), RewardId = reward.RewardId }); } await _context.SaveChangesAsync(); return(Ok(new Response() { Message = $"QR code {request.QRCode} has been verified", Status = "200" })); }