/// <summary> /// Get Method | Route: /BusinessPartner | View of all business partner and input for create a new one /// </summary> /// <returns></returns> public ActionResult Index() { //Take all entities from db and project them into view model var allPartners = _bussinessPartner.All().Select(i => new BusinessPartnerDetailsView { Email = i.Email, Id = i.Id, Name = i.Name, PhoneNumber = i.PhoneNumber, }).OrderByDescending(i => i.Id).ToList(); var addbox = new BusinessPartnerBindingModel(); var model = new BusinessPartnerGlobalView() { AddBusinessPartner = addbox, PartnerDetails = allPartners }; return(View(model)); }
public ActionResult Add(EmplyeeBindingModel emp) { //check if model is valid, if not -> redirect to Index with error message if (!ModelState.IsValid) { TempData["class"] = "alert-danger"; TempData["Response"] = "Unable to add new Employee "; return(RedirectToAction("Index")); } //Check if an entity with current name is existing if (_employee.All().Any(i => i.Name == emp.Name)) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! Unvalid Input Username should be unique!"; return(RedirectToAction("Index")); } var employee = new Employee() { Name = emp.Name, Position = emp.Position, }; //check if business partner field is empty if (emp.BusinessPartner != null) { //take business partner from db by given name var empBusnessPartner = _businessPartner.All().FirstOrDefault(i => i.Name == emp.BusinessPartner); //check if exists if (empBusnessPartner == null) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! That Business Partner does not exist ! Tip : Check your input twice"; return(RedirectToAction("Index")); } employee.BusinessPartner = empBusnessPartner; employee.BusinessPartnerId = empBusnessPartner.Id; } //Check if supervisor field is empty if (emp.SupervisorName != null) { //Check if supervisor and Name is equal var supervisor = _employee.All().FirstOrDefault(i => i.Name == emp.SupervisorName); if (emp.SupervisorName == emp.Name) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! You can not select the same Supervisor as the current user"; return(RedirectToAction("Index")); } //Check if supervisor is an existing employee if (supervisor == null) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! Invalid Supervisor's name ! It should be an existing Employee"; return(RedirectToAction("Index")); } if (_employee.checkIfCanBeSupervisor(employee, supervisor)) { TempData["class"] = "alert-danger"; TempData["Response"] = "Unable to add new Employee "; return(RedirectToAction("Index")); } employee.Supervisor = supervisor; employee.SupervisorId = supervisor.Id; } _employee.Add(employee); return(RedirectToRoute("Employee")); }