Could anyone give some advice, for the best possible way to implement this solution. My program does do a basic insert, but trying to get Items added to the Job Card is my big problem.
Thanks
Controller:
IJobCardBusiness iUser;
ApplicationDbContext _context = new ApplicationDbContext();
private readonly JobCardBusiness UserLogic = new JobCardBusiness();
public JobCardController(){
iUser = new JobCardBusiness();
}
public JobCardController(IJobCardBusiness Cat)
{
iUser = Cat;
}
public ActionResult Index()
{
return View(iUser.GetAllJobCards().ToList());
}
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(JobCardView jobmod)
{ if (ModelState.IsValid) { iUser.InsertJobCards(jobmod);
return RedirectToAction("Index");
}
return View();
}
Business Logic:
public JobCardView DeleteJobCard(int id)
{
JobCardView jView = new JobCardView();
using (var ssd = new JobCardRepository())
{
if (id != 0)
{
JobCard model = ssd.GetById(id);
}
return jView;
}
}
public void PostDeleteJobCard(int id)
{
using (var d = new JobCardRepository())
{
if (id != 0)
{
JobCard j_Id = d.GetById(id);
d.Delete(j_Id);
d.SaveChanges();
}
}
}
public void SaveChanges()
{
using (var repository = new JobCardRepository())
{
repository.SaveChanges();
}
}
public IEnumerable<JobCardView> GetAllJobCards()
{
var jobCards = new List<JobCardView>();
using (var repository = new JobCardRepository())
{
jobCards = repository.GetAll().Select(model => new JobCardView
{
JobNum = model.JobNum,
JobType = model.JobType,
JobDescription = model.JobDescription,
JobStat = (JobCardView.JobStats) model.JobStats,
Address = model.Address,
TechName = model.TechName, Date = model.Date,
Qty = model.Qty, ItemName = model.ItemName, Price = model.Price
}).ToList();
}
return jobCards;
}
public void InsertJobCards(JobCardView jobView)
{
using (var repository = new JobCardRepository())
{
repository.Insert(new JobCard
{
JobDescription = jobView.JobDescription,
JobStats =(JobCard.JobStat) jobView.JobStat,
JobType = jobView.JobType, Address = jobView.Address,
ItemName =jobView.ItemName,
Date = jobView.Date, Price = jobView.Price,
Qty = jobView.Qty, TechName = jobView.TechName,
JobNum = jobView.JobNum
});
}
}
Repository:
private readonly IRepository<JobCard> _clinicRepository;
public JobCardRepository()
{
_datacontext = new ApplicationDbContext();
_clinicRepository = new RepositoryService<JobCard>(_datacontext);
}
public JobCard GetById(int id)
{
return _clinicRepository.GetById(id);
}
public List<JobCard> GetAll()
{
return _clinicRepository.GetAll().ToList();
}
public void Insert(JobCard model)
{
_clinicRepository.Insert(model);
}
public void Update(JobCard model)
{
_clinicRepository.Update(model);
}
public void Delete(JobCard model)
{
_clinicRepository.Delete(model);
}
public IEnumerable<JobCard> Find(Func<JobCard, bool> predicate)
{
return _clinicRepository.Find(predicate).ToList();
}
public void SaveChanges()
{
_datacontext.SaveChanges();
}
public void Dispose()
{
_datacontext.Dispose();
_datacontext = null;
}
Model:
[Key]
public int JobNum { get; set; }
public string JobType { get; set; }
public string JobDescription { get; set; }
public float Price { get; set; }
public string ItemName { get; set; }
public int Qty{ get; set; }
public DateTime Date { get; set; }
public string Address { get; set; }
public JobStat JobStats { get; set; }
public enum JobStat
{
InProgress = 2,
Unassigned = 1,
Completed = 3
}
public string TechName { get; set; }
View Model:
[Key]
public int JobNum { get; set; }
[Display(Name="Job Type")]
public string JobType { get; set; }
[Display(Name = "Job Description")]
public string JobDescription { get; set; }
[Display(Name = "Price")]
public float Price { get; set; }
[Display(Name = "Items")]
public string ItemName { get; set; }
[Display(Name = "Quantity")]
public int Qty { get; set; }
[Display(Name = "Date")]
public DateTime Date { get; set; }
[Display(Name = "Client Address")]
public string Address { get; set; }
[Display(Name = "Job Status")]
public JobStats JobStat { get; set; }
public enum JobStats
{
InProgress = 2,
Unassigned = 1,
Completed = 3
}
[Display(Name = "Technician Assigned")]
public string TechName { get; set; }