using Directory.Services.AccountManagement;
using HumanResources.JobsContext;
using HumanResources.Models;
using HumanResources.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
Public Class Repository : Controller
{
[HttpPost]
public ActionResult PostIndexInternal(IFormFile file, Job job)
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "PPS");
UserPrincipal up = UserPrincipal.FindByIdentity(pc, User.Identity.Name);
string first = up.GivenName;
string last = up.Surname;
string full = first + " " + last;
if (file != null)
{
if (file.Length > 0)
{
//Get File Name
var fileName = Path.GetFileName(file.FileName);
//Get File Extension
var fileExtension = Path.GetExtension(fileName);
//Create New File Name based on AD Credentials
var newFileName = String.Concat(user, fileExtension);
//Creating new object on the database
var fileObject = new InternalJob()
{
IdJob = job.Id,
Title = job.Title,
EmployeeID = employeeID,
FileName = newFileName,
EmployeeName = user,
CurrentJobTitle = currentPosition,
DateApplied = DateTime.Now,
Active = true
};
using (var target = new MemoryStream())
{
file.CopyTo(target);
fileObject.FileData = target.ToArray();
}
_hrContext.InternalJobs.Add(fileObject);
_hrContext.SaveChanges();
return RedirectToAction(nameof(JobsApplied));
}
}
else if (file == null)
{
string msg = "Please do not forget to select and upload the Resume";
return Json(msg);
}
return RedirectToAction(nameof(JobsApplied));
}
}
//This would be in the HomeController
[HttpPost]
public IActionResult Example(IFormFile file, Job job)
{
Repository repository = new Repository(_context);
return repository.PostIndexInternal(IFormFile file, Job job);
}
Everytime I use UserPrincipal in the Controller folder this actually works. But if I were to move the UserPrincipal in the Repository class I get the null reference error. The idea is to use a Repository class to store the code and then run a cleaner code in the controller.Any Ideas would actually help. I am stuck in this part using the UserPrincipal in the Repository class. Repository Class is located in the Models directory. Technically everything else works fine but every time I have tried doing this with userprincipal in Repository class it throws the null reference error.