public IActionResult UpdateSalary([FromBody] WebSalaryInfo updateSalaryInfoRequest) { var dbSalaryInfo = _dbContext.Salaries.FirstOrDefault(u => u.Id == updateSalaryInfoRequest.Id); if (dbSalaryInfo == null) { return(BadRequest($"Salary {updateSalaryInfoRequest.Id} does not exist")); } if (updateSalaryInfoRequest.Rate > 0) { dbSalaryInfo.Rate = updateSalaryInfoRequest.Rate; } if (updateSalaryInfoRequest.Salary > 0) { dbSalaryInfo.Salary = updateSalaryInfoRequest.Salary; } if (!string.IsNullOrEmpty(updateSalaryInfoRequest.BankAccount)) { dbSalaryInfo.BankAccount = updateSalaryInfoRequest.BankAccount; } if (!string.IsNullOrEmpty(updateSalaryInfoRequest.Type)) { if (!Enum.TryParse(updateSalaryInfoRequest.Type, out SalaryType salaryType)) { return(BadRequest($"Salary type {updateSalaryInfoRequest.Type} does not exist")); } dbSalaryInfo.Type = (int)salaryType; } if (!string.IsNullOrEmpty(updateSalaryInfoRequest.PaymentType)) { if (!Enum.TryParse(updateSalaryInfoRequest.PaymentType, out PaymentType paymentType)) { return(BadRequest($"Salary type {updateSalaryInfoRequest.PaymentType} does not exist")); } dbSalaryInfo.Type = (int)paymentType; } using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Salaries.Update(dbSalaryInfo); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbSalaryInfo.SalaryInfoToWebSalaryInfo())); }
/// <summary> /// Coverts SalaryInfo to WebSalaryInfo /// </summary> public static WebSalaryInfo SalaryInfoToWebSalaryInfo(this SalaryInfo salaryInfo) { var webSalaryInfo = new WebSalaryInfo { Id = salaryInfo.Id, Salary = salaryInfo.Salary, BankAccount = salaryInfo.BankAccount, PaymentType = salaryInfo.PaymentTypeName, Rate = salaryInfo.Rate, Type = salaryInfo.TypeName }; return(webSalaryInfo); }