public object GetLeaveStructureById(int id)
        {
            object result = null;

            LeaveStructure objLeaveStructure = new LeaveStructure();
            List <int?>    DepartmentIds     = new List <int?>();
            List <int?>    LeaveTypeIds      = new List <int?>();

            try
            {
                using (_context)
                {
                    objLeaveStructure = _context.LeaveStructure.FirstOrDefault(x => x.Id == id);
                    DepartmentIds     = _context.LeaveStructureDepartmentMapping.Where(y => y.LeaveStructureId == id).Select(z => z.DepartmentId).ToList();
                    LeaveTypeIds      = _context.LeaveStructureLeaveTypeMapping.Where(y => y.LeaveStructureId == id).Select(z => z.LeaveTypeId).ToList();
                    result            = new
                    {
                        objLeaveStructure,
                        DepartmentIds,
                        LeaveTypeIds,
                        error = "0",
                        msg   = "Success"
                    };
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                result = new
                {
                    objLeaveStructure,
                    DepartmentIds,
                    LeaveTypeIds,
                    error = "1",
                    msg   = "Error"
                };
            }
            return(result);
        }
        public object CreateUpdateLeaveStructure(int id, [FromBody] LeaveStructureVM model)
        {
            object result    = null;
            string message   = "";
            string errorcode = "";
            string excp      = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_context)
            {
                using (var _ctxTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        if (id != 0)
                        {
                            var entityUpdate = _context.LeaveStructure.FirstOrDefault(x => x.Id == id);

                            if (entityUpdate != null)
                            {
                                entityUpdate.LeaveStructureName = model.LeaveStructureName;
                                entityUpdate.MaxLeaveCount      = model.MaxLeaveCount;
                                entityUpdate.IsCarryForward     = model.IsCarryForward;
                                entityUpdate.Status             = model.Status;
                                entityUpdate.IsAllowLeave       = model.IsAllowLeave;
                                entityUpdate.IsDefault          = model.IsDefault;
                                _context.SaveChanges();
                            }

                            List <LeaveStructureDepartmentMapping> RemoveDepartmentList = _context.LeaveStructureDepartmentMapping.Where(y => y.LeaveStructureId == id).ToList();
                            if (RemoveDepartmentList.Count > 0)
                            {
                                _context.LeaveStructureDepartmentMapping.RemoveRange(RemoveDepartmentList);
                            }
                            if (model.DepartmentIds.Count > 0)
                            {
                                foreach (int DepartmentId in model.DepartmentIds)
                                {
                                    LeaveStructureDepartmentMapping objMapping = null;
                                    objMapping = new LeaveStructureDepartmentMapping {
                                        LeaveStructureId = id, DepartmentId = DepartmentId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }
                            List <LeaveStructureLeaveTypeMapping> RemoveLeaveTypeList = _context.LeaveStructureLeaveTypeMapping.Where(y => y.LeaveStructureId == id).ToList();
                            if (RemoveLeaveTypeList.Count > 0)
                            {
                                _context.LeaveStructureLeaveTypeMapping.RemoveRange(RemoveLeaveTypeList);
                            }
                            if (model.LeaveTypeIds.Count > 0)
                            {
                                foreach (int LeaveTypeId in model.LeaveTypeIds)
                                {
                                    LeaveStructureLeaveTypeMapping objMapping = null;
                                    objMapping = new LeaveStructureLeaveTypeMapping {
                                        LeaveStructureId = id, LeaveTypeId = LeaveTypeId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }


                            _ctxTransaction.Commit();
                            message   = "Entry Updated";
                            errorcode = "0";
                        }
                        else
                        {
                            LeaveStructure objLeaveStructure = new LeaveStructure();
                            objLeaveStructure.LeaveStructureName = model.LeaveStructureName;
                            objLeaveStructure.MaxLeaveCount      = model.MaxLeaveCount;
                            objLeaveStructure.IsCarryForward     = model.IsCarryForward;
                            objLeaveStructure.Status             = model.Status;
                            objLeaveStructure.IsAllowLeave       = model.IsAllowLeave;
                            objLeaveStructure.IsDefault          = model.IsDefault;
                            _context.LeaveStructure.Add(objLeaveStructure);
                            //await _ctx.SaveChangesAsync();
                            _context.SaveChanges();

                            if (model.DepartmentIds.Count > 0)
                            {
                                foreach (int DepartmentId in model.DepartmentIds)
                                {
                                    LeaveStructureDepartmentMapping objMapping = null;
                                    objMapping = new LeaveStructureDepartmentMapping {
                                        LeaveStructureId = objLeaveStructure.Id, DepartmentId = DepartmentId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }

                            if (model.LeaveTypeIds.Count > 0)
                            {
                                foreach (int LeaveTypeId in model.LeaveTypeIds)
                                {
                                    LeaveStructureLeaveTypeMapping objMapping = null;
                                    objMapping = new LeaveStructureLeaveTypeMapping {
                                        LeaveStructureId = objLeaveStructure.Id, LeaveTypeId = LeaveTypeId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }

                            _ctxTransaction.Commit();
                            message   = "Saved Successfully";
                            errorcode = "0";
                        }
                    }
                    catch (Exception e)
                    {
                        _ctxTransaction.Rollback();
                        e.ToString();
                        message   = "Saved Error";
                        errorcode = "1";
                        excp      = e.ToString();
                    }

                    result = new
                    {
                        error = errorcode,
                        msg   = message,
                        excp  = excp
                    };
                }
            }
            return(result);
        }
        public object CreateLeaveStructure([FromBody] LeaveStructureVM model)
        {
            object result    = null;
            string message   = "";
            string errorcode = "";
            string excp      = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_context)
            {
                using (var _ctxTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        LeaveStructure objLeaveStructure = new LeaveStructure();
                        objLeaveStructure.LeaveStructureName = model.LeaveStructureName;
                        objLeaveStructure.MaxLeaveCount      = model.MaxLeaveCount;
                        objLeaveStructure.IsCarryForward     = model.IsCarryForward;
                        objLeaveStructure.Status             = model.Status;
                        objLeaveStructure.IsAllowLeave       = model.IsAllowLeave;
                        objLeaveStructure.IsDefault          = model.IsDefault;
                        _context.LeaveStructure.Add(objLeaveStructure);
                        //await _ctx.SaveChangesAsync();
                        _context.SaveChanges();

                        if (model.DepartmentIds.Count > 0)
                        {
                            foreach (int DepartmentId in model.DepartmentIds)
                            {
                                LeaveStructureDepartmentMapping objMapping = null;
                                objMapping = new LeaveStructureDepartmentMapping {
                                    LeaveStructureId = objLeaveStructure.Id, DepartmentId = DepartmentId
                                };
                                _context.Add(objMapping);
                            }
                            _context.SaveChanges();
                        }

                        if (model.LeaveTypeIds.Count > 0)
                        {
                            foreach (int LeaveTypeId in model.LeaveTypeIds)
                            {
                                LeaveStructureLeaveTypeMapping objMapping = null;
                                objMapping = new LeaveStructureLeaveTypeMapping {
                                    LeaveStructureId = objLeaveStructure.Id, LeaveTypeId = LeaveTypeId
                                };
                                _context.Add(objMapping);
                            }
                            _context.SaveChanges();
                        }

                        _ctxTransaction.Commit();
                        message   = "Saved Successfully";
                        errorcode = "0";
                    }
                    catch (Exception e)
                    {
                        _ctxTransaction.Rollback();
                        e.ToString();
                        message   = "Saved Error";
                        errorcode = "1";
                        excp      = e.ToString();
                    }

                    result = new
                    {
                        error = errorcode,
                        msg   = message,
                        excp  = excp
                    };
                }
            }
            return(result);
        }