public object CreateUpdateSalaryStructure(int id, [FromBody] SalaryStructureVM 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.SalaryStructure.FirstOrDefault(x => x.Id == id); if (entityUpdate != null) { entityUpdate.Name = model.Name; entityUpdate.Description = model.Description; entityUpdate.IsActive = model.IsActive; _context.SaveChanges(); } List <SalaryStructureDepartmentMapping> RemoveDepartmentList = _context.SalaryStructureDepartmentMapping.Where(y => y.SalaryStructureId == id).ToList(); if (RemoveDepartmentList.Count > 0) { _context.SalaryStructureDepartmentMapping.RemoveRange(RemoveDepartmentList); } if (model.DepartmentIds.Count > 0) { foreach (int DepartmentId in model.DepartmentIds) { SalaryStructureDepartmentMapping objMapping = null; objMapping = new SalaryStructureDepartmentMapping { SalaryStructureId = id, DepartmentId = DepartmentId }; _context.Add(objMapping); } _context.SaveChanges(); } List <SalaryStructurePayrollCategoryMapping> RemovePayrollCategoryList = _context.SalaryStructurePayrollCategoryMapping.Where(y => y.SalaryStructureId == id).ToList(); if (RemovePayrollCategoryList.Count > 0) { _context.SalaryStructurePayrollCategoryMapping.RemoveRange(RemovePayrollCategoryList); } if (model.PayrollCategoryIds.Count > 0) { foreach (int PayrollCategoryId in model.PayrollCategoryIds) { SalaryStructurePayrollCategoryMapping objMapping = null; objMapping = new SalaryStructurePayrollCategoryMapping { SalaryStructureId = id, PayrollCategoryId = PayrollCategoryId }; _context.Add(objMapping); } _context.SaveChanges(); } _ctxTransaction.Commit(); message = "Entry Updated"; errorcode = "0"; } else { SalaryStructure objSalaryStructure = new SalaryStructure(); objSalaryStructure.Name = model.Name; objSalaryStructure.Description = model.Description; objSalaryStructure.IsActive = model.IsActive; _context.SalaryStructure.Add(objSalaryStructure); //await _ctx.SaveChangesAsync(); _context.SaveChanges(); if (model.DepartmentIds.Count > 0) { foreach (int DepartmentId in model.DepartmentIds) { SalaryStructureDepartmentMapping objMapping = null; objMapping = new SalaryStructureDepartmentMapping { SalaryStructureId = objSalaryStructure.Id, DepartmentId = DepartmentId }; _context.Add(objMapping); } _context.SaveChanges(); } if (model.PayrollCategoryIds.Count > 0) { foreach (int PayrollCategoryId in model.PayrollCategoryIds) { SalaryStructurePayrollCategoryMapping objMapping = null; objMapping = new SalaryStructurePayrollCategoryMapping { SalaryStructureId = objSalaryStructure.Id, PayrollCategoryId = PayrollCategoryId }; _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 CreateSalaryStructure([FromBody] SalaryStructureVM model) { object result = null; string message = ""; string errorcode = ""; string excp = ""; if (model == null) { return(BadRequest()); } using (_context) { using (var _ctxTransaction = _context.Database.BeginTransaction()) { try { SalaryStructure objSalaryStructure = new SalaryStructure(); objSalaryStructure.Name = model.Name; objSalaryStructure.Description = model.Description; objSalaryStructure.IsActive = model.IsActive; _context.SalaryStructure.Add(objSalaryStructure); //await _ctx.SaveChangesAsync(); _context.SaveChanges(); if (model.DepartmentIds.Count > 0) { foreach (int DepartmentId in model.DepartmentIds) { SalaryStructureDepartmentMapping objMapping = null; objMapping = new SalaryStructureDepartmentMapping { SalaryStructureId = objSalaryStructure.Id, DepartmentId = DepartmentId }; _context.Add(objMapping); } _context.SaveChanges(); } if (model.PayrollCategoryIds.Count > 0) { foreach (int PayrollCategoryId in model.PayrollCategoryIds) { SalaryStructurePayrollCategoryMapping objMapping = null; objMapping = new SalaryStructurePayrollCategoryMapping { SalaryStructureId = objSalaryStructure.Id, PayrollCategoryId = PayrollCategoryId }; _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); }