public IActionResult AddNewDepartment([FromBody] DepartmentEmployeeDetailsDto newDepartment, [FromHeader(Name = "id_token")] string googleToken) { try { if (googleAuth.ProcessRequestAtGoogle(googleToken)) { var DepartAddStatus = empRepository.AddDepartment(newDepartment); return(Ok(Json(DepartAddStatus))); } else { return(BadRequest()); } } catch (Exception exp) { return(NotFound(@"Can't add the given Department")); } }
/* * This Method will return all Employees in a given department * with depId */ public DepartmentEmployeeDetailsDto DepartmentDetail(int depId) { DepartmentEmployeeDetailsDto departDetails = new DepartmentEmployeeDetailsDto(); try { var deptDetail = Context.DepartmentDetails .FirstOrDefault(d => d.DepartmentId == depId); var empDeptDetail = Context.Employees .Include(e => e.DepartmentDetails) .Where(e => e.DepartmentDetails.DepartmentId == deptDetail.DepartmentId); departDetails.DepartmentName = deptDetail.DepartmentName; departDetails.DepartmentDisc = deptDetail.DepartmentDiscription; departDetails.DepartHeadName = deptDetail.DepartmentHeadName; ICollection <EmployeeDto> emplList = new List <EmployeeDto>(); foreach (Employee emp in empDeptDetail) { EmployeeDto deptEmployee = new EmployeeDto(); deptEmployee.EmployeeId = emp.EmployeeId; deptEmployee.FirstName = emp.FirstName; deptEmployee.LastName = emp.LastName; deptEmployee.EmailId = emp.EmailID; emplList.Add(deptEmployee); } departDetails.EmployeeList = emplList; } catch (SqlException expSql) { throw expSql; } catch (NullReferenceException expNull) { throw expNull; } return(departDetails); }
/* * This method to add new team to the Db with given Details */ public int AddDepartment(DepartmentEmployeeDetailsDto toAddDepartment) { DepartmentDetails departmentData = new DepartmentDetails(); try { departmentData.DepartmentHeadName = toAddDepartment.DepartHeadName; departmentData.DepartmentDiscription = toAddDepartment.DepartmentDisc; departmentData.DepartmentName = toAddDepartment.DepartmentName; Context.DepartmentDetails.Add(departmentData); Context.SaveChanges(); } catch (SqlException sqlExp) { throw sqlExp; } catch (NullReferenceException expNull) { throw expNull; } return(departmentData.DepartmentId); }