public int Delete(int id, LoanDb db) { if (IsDbContextNull(db)) { return(0); } Loan trackedLoan = null; try { trackedLoan = db.Loans.SingleOrDefault(c => c.Id == id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while getting loan by id{(_logSensitiveData ? "{" + id + "}" : "")} for delete"); } if (trackedLoan == null) { _logger.LogDebug($"No loan found for given id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } try { db.Loans.Remove(trackedLoan); return(db.SaveChanges()); } catch (Exception e) { _logger.LogError(e, $"Error occurred while deleting loan by id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } }
public int Save(Loan loan, LoanDb db) { if (IsDbContextNull(db) || IsLoanNull(loan)) { return(0); } db.Loans.Add(loan); try { db.SaveChanges(); return(loan.Id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while saving customer{(_logSensitiveData ? JsonConvert.SerializeObject(loan) : "")}"); return(0); } }
public int UpdatePartial(int id, dynamic loan, LoanDb db) { if (IsDbContextNull(db)) { return(0); } Loan trackedLoan = null; try { trackedLoan = db.Loans.SingleOrDefault(c => c.Id == id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while getting loan by id{(_logSensitiveData ? "{" + id + "}" : "")} for partial update"); } if (trackedLoan == null) { _logger.LogDebug($"No loan found for given id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } if (loan.GetType().GetProperty("Id") != null) { loan.Id = id; //cannot update the id } try { db.Entry(trackedLoan).CurrentValues.SetValues(loan); return(db.SaveChanges()); } catch (Exception e) { _logger.LogError(e, $"Error occurred while partially updating loan{(_logSensitiveData ? JsonConvert.SerializeObject(loan, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }) : " ")}"); return(0); } }
public int UpdateFull(int id, Loan loan, LoanDb db) { if (IsDbContextNull(db) || IsLoanNull(loan)) { return(0); } Loan trackedLoan = null; try { trackedLoan = db.Loans.SingleOrDefault(c => c.Id == id); } catch (Exception e) { _logger.LogError(e, $"Error occurred while getting loan by id{(_logSensitiveData ? "{" + id + "}" : "")} for full update"); return(0); } if (trackedLoan == null) { _logger.LogDebug($"No loan found for given id{(_logSensitiveData ? "{" + id + "}" : "")}"); return(0); } loan.Id = id; //avoid updating the id try { db.Entry(trackedLoan).CurrentValues.SetValues(loan); return(db.SaveChanges()); } catch (Exception e) { _logger.LogError(e, $"Error occurred while fully updating loan{(_logSensitiveData ? JsonConvert.SerializeObject(loan) : "")}"); return(0); } }
public int Save(Loan loan, LoanDb db) { if (loan == null || db == null) { throw new ArgumentNullException($"{(loan == null ? "loan is null. " : "")}{(db == null ? "db is null. " : "")}"); } //if (IsDbContextNull(db) || IsLoanNull(loan)) // return 0; db.Loans.Add(loan); try { db.SaveChanges(); return(loan.Id); } catch (Exception e) { if (_logger != null) { _logger.LogError(e, $"Error occurred while saving customer{(_logSensitiveData ? JsonConvert.SerializeObject(loan) : "")}"); } return(0); } }