public async Task <IActionResult> SendListToClient([FromBody] SendListViewModel sendListVM) { if (sendListVM.SendList != null) { string[] userId = null; userId = sendListVM.SendList.Split(","); string content = ""; string table; table = " <table cellspacing=\"0\" cellpadding=\"10\" border=\"0\" align=\"left\" >" + " <tbody>" + " <tr>"; //http: //13.75.89.123:8081/generatedCV/UserId_327.pdf for (int i = 0; i < userId.Length; i++) { //Check if the user exist var is_user_exist = _context.Users.Any(u => u.UserId == Int32.Parse(userId[i])); if (!is_user_exist) { return(Json(StatusCode(404))); } /*Create an object to represent a user in the database == userId */ var user = _context.Users.FirstOrDefault(u => u.UserId == Int32.Parse(userId[i])); var dir = _configuration.GetSection("Directory2:ForGeneratedCV").Value; var path = dir + user.UserId + "/" + user.FirstName + "_" + user.LastName + "_CV.pdf"; table += "<tr><td>" + (i + 1) + ". " + user.FirstName + " " + user.LastName + "</td><td>" + "<a href=\"" + path + "\">" + "Link to CV" + " </a></td></tr>"; } //END OF THE TABLE FOR EMPLOYEES, ADD THIS table += "</tr>" + "<tr><td>Best regards.</td></tr>" + "<tr><td>Dev Partners</td></tr>" + "</tbody>" + "</table>"; content = "<p>Good day! Please find below the CV(s) of candidate(s) we are presenting for your consideration:</p>"; content += table; string subject = "List of proposed people for " + sendListVM.CompanyName + " project."; string senderEmail = _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value; //send the content here await _send.SendEmailAsync(sendListVM.ReceiverName, senderEmail, sendListVM.ReceiverEmail, content, subject); } return(Json(Ok())); }
public async Task <BPOClient> Send(BPOClient ClientVM) { var dbContextOptions = new DbContextOptionsBuilder <DataContext>(); dbContextOptions.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")); using (var newContext = new DataContext(dbContextOptions.Options)) //Separate this context's instance from the request { var client = newContext.BPOClient.FirstOrDefault(c => c.Id == ClientVM.Id); if (client == null) { throw new NullReferenceException(); } try { await _mail.SendEmailAsync("Roland", _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value, _configuration.GetSection("AdminEmailCredentials:ReceiverEmail").Value, ClientVM, "New BPO Client"); client.IsSent = 1; newContext.Update(client); } catch (Exception ex) { client.Message = ex.Message + "\n " + client.Message; client.IsSent = 0; } finally { newContext.SaveChanges(); } return(null); } }
[HttpPost("Submit")]//Inform Fritz about this public async Task <IActionResult> SubmitAnswers([FromBody] SubmitAnswerViewModel submitAnswerVM) { //if the user has already taken a specified exam. if (_context.UserTests.Any(ut => (ut.UserId == submitAnswerVM.UserId && ut.TestId == submitAnswerVM.TestId && ut.IsSubmit == 1))) { var data2 = new { emailStatus = 1, isTaken = 1 }; return(Json(data2)); } double score = 0; int negative = 0; if (submitAnswerVM.TestId == 4) { foreach (var userAnswer in submitAnswerVM.Answers) { Question question = _context.Questions.FirstOrDefault(q => q.QuestionId == userAnswer.QuestionId); Choice choice = _context.Choices.FirstOrDefault(c => c.ChoiceId == userAnswer.ChoiceId); if (choice.Value == 1) { score++; } else if (choice.Value == -1) { negative++; } else { continue; } } if (negative > 1) { score = -1; //meaning he failed the test. } } else { foreach (var userAnswer in submitAnswerVM.Answers) { Question question = _context.Questions.FirstOrDefault(q => q.QuestionId == userAnswer.QuestionId); if (userAnswer.ChoiceId == question.CorrectAnswer) { score++; } else { continue; } } } int scoreForDB = Convert.ToInt32(score); //UPDATE THE TABLE HERE var _usertest = _context.UserTests.FirstOrDefault(ut => ut.UserId == submitAnswerVM.UserId && ut.TestId == submitAnswerVM.TestId); _map.Map(submitAnswerVM, _usertest); _usertest.Score = scoreForDB; _usertest.IsTaken = 1; _usertest.IsSubmit = 1; _usertest.DateTaken = DateTime.Now.AddHours(8); _context.Update(_usertest); _context.SaveChanges(); //send a confirmation email to admin try { var admins = _context.Admins; var user = _context.Users.FirstOrDefault(u => u.UserId == submitAnswerVM.UserId); var test = _context.Test.FirstOrDefault(t => t.TestId == submitAnswerVM.TestId); var testtype = _context.TestTypes.FirstOrDefault(tt => tt.TestTypeId == test.TestTypeId); var content = ""; if (submitAnswerVM.TestId == 4) { string passFail = ""; if (score > 6) { passFail = "passed"; } else { passFail = "failed"; } content = user.Email + " has " + passFail + " the exam on " + testtype.Description + " with a score of " + score + " on " + DateTime.Now.AddHours(8); } else { var CountQuestions = _context.Questions.Count(q => q.TestId == submitAnswerVM.TestId); double scorePercentages = (score / CountQuestions) * 100; content = user.Email + " has taken an exam on " + testtype.Description + " with a score of " + scorePercentages + "% on " + DateTime.Now.AddHours(8); } if (admins.Count() > 0) { foreach (var a in admins) { var adminEmail = a.Email; await _send.SendEmailAsync("Admin", _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value, adminEmail, content, "Applicant Online Examination"); } } } catch (Exception) { return(Json(StatusCode(500, "Error in sending email to admins"))); } return(Ok()); }
public async Task <IActionResult> CreateAsync(UserViewModel userVM) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // email validation var is_email_exist = _context.Users.Any(u => u.Email == userVM.Email); if (is_email_exist) { return(StatusCode(409)); } userVM.SubmittedDate = DateTime.Now.AddHours(8); var user = _map.Map <User>(userVM); _context.Add(user); _context.SaveChanges(); // save skill if (userVM.Skills != null) { string[] skillId = null; skillId = userVM.Skills.Split(","); for (int i = 0; i < skillId.Length; i++) { UserSkillViewModel userskillVM = new UserSkillViewModel { UserId = user.UserId, SkillId = int.Parse(skillId[i]) }; _context.Add(_map.Map <UserSkills>(userskillVM)); } _context.SaveChanges(); } // will create folder where it stored the uploaded file. if (userVM.Filename == null || userVM.Filename.Length == 0) { return(Content("file not selected")); } var path = Path.Combine("C:\\UploadFiles", user.UserId.ToString(), userVM.Filename.FileName); Directory.CreateDirectory(Path.GetDirectoryName(path)); using (var stream = new FileStream(path, FileMode.Create)) { userVM.Filename.CopyTo(stream); } var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); // email the user_config.GetSection("AppSettings:Token").Value await _send.SendEmailAsync(userVM.FirstName, _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value, userVM.Email, "Thank you for submitting your profile to Dev Partners. We will review your CV and contact you for current openings.", "Application received."); // email the admin await _send.SendEmailAsync("Admin", _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value, _configuration["AdminEmailCredentials:ReceiverEmail"], "A new CV/profile has been uploaded to the Pooling System on " + userVM.SubmittedDate, "New application received on the Pooling System."); return(StatusCode(201)); } catch (Exception ex) { return(BadRequest(ex)); } }