Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
 private bool IsDbContextNull(LoanDb db)
 {
     if (db == null)
     {
         _logger.LogError(new ArgumentNullException($"{nameof(LoanDb)} instance is required to perform database operations"), $"{nameof(LoanDb)} is null");
         return(true);
     }
     return(false);
 }
Esempio n. 3
0
        public IActionResult UpdatePartial(int id, [FromBody] dynamic loan, [FromServices] LoanDb db)
        {
            int rowsAffected = _loanDao.UpdatePartial(id, loan, db);

            if (rowsAffected <= 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "error occurred while partially updating loan" }));
            }
            return(Ok());
        }
Esempio n. 4
0
        public IActionResult GetById(int id, [FromServices] LoanDb db)
        {
            Loan loan = _loanDao.GetById(id, db);

            if (loan == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, new { error = $"no entity found by id: {id}" }));
            }
            return(Ok(loan));
        }
Esempio n. 5
0
        public IActionResult Delete(int id, [FromServices] LoanDb db)
        {
            int rowsAffected = _loanDao.Delete(id, db);

            if (rowsAffected <= 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "error occurred while deleting loan" }));
            }
            return(Ok());
        }
        public IActionResult UpdateFull(int id, [FromBody] Loan loan, [FromServices] LoanDb db)
        {
            if (loan == null)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, new { error = "cannot update with null entity" }));
            }

            int rowsAffected = _loanDao.UpdateFull(id, loan, db);

            return(Ok(new { rowsaffected = rowsAffected }));
        }
        public IActionResult Save([FromBody] Loan loan, [FromServices] LoanDb db)
        {
            if (loan == null)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, new { error = "cannot store null entity" }));
            }

            int rowId = _loanDao.Save(loan, db);

            return(CreatedAtAction(nameof(LoanController.GetById), nameof(LoanController).RemoveSuffix("Controller"), new { id = rowId }, loan));
        }
        public IActionResult GetById(int id, [FromServices] LoanDb db)
        {
            Loan loan;

            try { loan = _loanDao.GetById(id, db); }
            catch (InvalidOperationException) { return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "more than one loan found for the given id" })); }

            if (loan == null)
            {
                return(NotFound(new { error = $"no loan found by id: {id}" }));
            }
            return(Ok(loan));
        }
        public Loan GetById(int id, LoanDb db)
        {
            if (id <= 0)
            {
                throw new ArgumentException($"id less than or equal to 0");
            }
            if (db == null)
            {
                throw new ArgumentNullException($"{typeof(LoanDb).FullName} parameter shouldn't be null");
            }

            return(db.Loans.SingleOrDefault(l => l.Id == id));
        }
Esempio n. 10
0
        public IActionResult UpdateFull(int id, [FromBody] Loan loan, [FromServices] LoanDb db)
        {
            if (loan == null)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, new { error = "cannot update with null entity" }));
            }

            int rowsAfected = _loanDao.UpdateFull(id, loan, db);

            if (rowsAfected <= 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "error occurred while fully updaing loan" }));
            }
            return(Ok());
        }
Esempio n. 11
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);
            }
        }
Esempio n. 12
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);
            }
        }
Esempio n. 13
0
        public Loan GetById(int id, LoanDb db)
        {
            if (IsDbContextNull(db))
            {
                return(null);
            }

            try
            {
                Loan loan = db.Loans.SingleOrDefault(l => l.Id == id);
                if (loan == null)
                {
                    _logger.LogDebug($"No loan found for id: {id}");
                }
                return(loan);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error occurred while getting loan by id{(_logSensitiveData ? "{" + id + "}" : "")}");
                return(null);
            }
        }
Esempio n. 14
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);
            }
        }
Esempio n. 15
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);
            }
        }
        public IActionResult Delete(int id, [FromServices] LoanDb db)
        {
            int rowsAffected = _loanDao.Delete(id, db);

            return(Ok(new { rowsaffected = rowsAffected }));
        }
Esempio n. 17
0
 public LoanBs()
 {
     objDb = new LoanDb();
 }
Esempio n. 18
0
        public IActionResult GetAll([FromQuery] Page page, [FromQuery] Filter filter, [FromServices] LoanDb db)
        {
            if (page == null || page.PageNo < 1 || page.PageSize < 1)
            {
                return(StatusCode((int)HttpStatusCode.BadRequest, new { error = "invalid page details" }));
            }

            return(Ok(_loanDao.GetAll(page, filter, db)));
        }
        public async Task <IActionResult> SaveWithCollaterals([FromBody] JsonElement loanWithCollaterals, [FromServices] LoanDb db)
        {
            if (loanWithCollaterals.ValueKind != JsonValueKind.Object)
            {
                return(BadRequest(new { error = "invalid request body" }));
            }

            JsonElement loanJson        = loanWithCollaterals.GetProperty("loan");
            JsonElement collateralsJson = loanWithCollaterals.GetProperty("collaterals");

            if (collateralsJson.ValueKind != JsonValueKind.Array)
            {
                return(BadRequest(new { error = "invalid collateral array" }));
            }

            Loan loan = JsonSerializer.Deserialize <Loan>(loanJson.GetRawText(), new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            });

            if (loan == null)
            {
                return(BadRequest(new { error = "invalid loan object" }));
            }

            _logger.LogInformation(collateralsJson.GetRawText());

            HttpResponseMessage response;

            try { response = await _collateralManagement.Save(collateralsJson); }
            catch (HttpRequestException) { return(StatusCode((int)HttpStatusCode.ServiceUnavailable, new { error = "unable to connect with CollateralManagementApi" })); }

            if (response.StatusCode != HttpStatusCode.MultiStatus)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "something went wrong in CollateralManagentApi, status code: " + response.StatusCode }));
            }

            int rowsAffected = _loanDao.Save(loan, db);

            if (rowsAffected <= 0)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "something went wrong while saving the loan details" }));
            }

            JsonElement responseBody = JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement;

            if (!responseBody.TryGetProperty("statuses", out JsonElement collateralSaveStatuses) || collateralSaveStatuses.ValueKind != JsonValueKind.Array)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, new { error = "invalid response from CollateralManagementApi" }));
            }

            _logger.LogInformation(JsonSerializer.Serialize(new { loanSaveStatus = (int)HttpStatusCode.Created, collateralSaveStatuses = collateralSaveStatuses }));

            return(Ok(new { loanSaveStatus = (int)HttpStatusCode.Created, collateralSaveStatuses = collateralSaveStatuses }));
        }
 public IActionResult GetAll([FromQuery] Page page, [FromQuery] Filter filter, [FromServices] LoanDb db)
 {
     return(Ok(_loanDao.GetAll(page, filter, db)));
 }
Esempio n. 21
0
        public List <Loan> GetAll(Page page, Filter filter, LoanDb db)
        {
            if (IsDbContextNull(db))
            {
                return(null);
            }

            if (page == null || page.PageNo < 1 || page.PageSize < 1)
            {
                _logger.LogDebug(new ArgumentException($"Given page{JsonConvert.SerializeObject(page, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })} details are invalid"), "Invalid Page Details");
                return(null);
            }

            IQueryable <Loan> query = db.Loans.AsQueryable();

            if (filter != null)
            {
                if (filter.CustomerId != 0)
                {
                    query = query.Where(l => l.CustomerId == filter.CustomerId);
                }
                if (filter.Type != null)
                {
                    query = query.Where(l => l.Type == filter.Type);
                }
                if (filter.MinPrincipal > 0)
                {
                    query = query.Where(l => l.Principal >= filter.MinPrincipal);
                }
                if (filter.MaxPrincipal > 0)
                {
                    query = query.Where(l => l.Principal <= filter.MaxPrincipal);
                }
                if (filter.MinInterest > 0)
                {
                    query = query.Where(l => l.Interest >= filter.MinInterest);
                }
                if (filter.MaxInterest > 0)
                {
                    query = query.Where(l => l.Interest <= filter.MaxInterest);
                }
                if (filter.MinEmi > 0)
                {
                    query = query.Where(l => l.Emi >= filter.MinEmi);
                }
                if (filter.MaxEmi > 0)
                {
                    query = query.Where(l => l.Emi <= filter.MaxEmi);
                }
                if (filter.SanctionAfter != DateTime.MinValue)
                {
                    query = query.Where(l => l.SanctionDate >= filter.SanctionAfter);
                }
                if (filter.SanctionBefore != DateTime.MinValue)
                {
                    query = query.Where(l => l.SanctionDate <= filter.SanctionBefore);
                }
                if (filter.MaturityAfter != DateTime.MinValue)
                {
                    query = query.Where(l => l.MaturityDate >= filter.MaturityAfter);
                }
                if (filter.MaturityBefore != DateTime.MinValue)
                {
                    query = query.Where(l => l.MaturityDate <= filter.MaturityBefore);
                }
            }

            query = query.Skip((page.PageNo - 1) * page.PageSize).Take(page.PageSize);

            return(query.ToList());
        }
Esempio n. 22
0
        public List <Loan> GetAll(Page page, Filter filter, LoanDb db)
        {
            page ??= new Page()
            {
                PageNo = 1, PageSize = 10
            };
            page.PageSize = page.PageSize <= 0 ? 10 : page.PageSize;

            if (db == null)
            {
                throw new ArgumentNullException($"{typeof(LoanDb).FullName} parameter shouldn't be null");
            }

            IQueryable <Loan> query = db.Loans.AsQueryable();

            if (filter != null)
            {
                if (filter.CustomerId != 0)
                {
                    query = query.Where(l => l.CustomerId == filter.CustomerId);
                }
                if (filter.Type != null)
                {
                    query = query.Where(l => l.Type == filter.Type);
                }
                if (filter.MinPrincipal > 0)
                {
                    query = query.Where(l => l.Principal >= filter.MinPrincipal);
                }
                if (filter.MaxPrincipal > 0)
                {
                    query = query.Where(l => l.Principal <= filter.MaxPrincipal);
                }
                if (filter.MinInterest > 0)
                {
                    query = query.Where(l => l.Interest >= filter.MinInterest);
                }
                if (filter.MaxInterest > 0)
                {
                    query = query.Where(l => l.Interest <= filter.MaxInterest);
                }
                if (filter.MinEmi > 0)
                {
                    query = query.Where(l => l.Emi >= filter.MinEmi);
                }
                if (filter.MaxEmi > 0)
                {
                    query = query.Where(l => l.Emi <= filter.MaxEmi);
                }
                if (filter.SanctionAfter != DateTime.MinValue)
                {
                    query = query.Where(l => l.SanctionDate >= filter.SanctionAfter);
                }
                if (filter.SanctionBefore != DateTime.MinValue)
                {
                    query = query.Where(l => l.SanctionDate <= filter.SanctionBefore);
                }
                if (filter.MaturityAfter != DateTime.MinValue)
                {
                    query = query.Where(l => l.MaturityDate >= filter.MaturityAfter);
                }
                if (filter.MaturityBefore != DateTime.MinValue)
                {
                    query = query.Where(l => l.MaturityDate <= filter.MaturityBefore);
                }
            }

            if ((page.PageNo - 1) * page.PageSize >= query.Count())
            {
                int totalRows = query.Count();
                page.PageNo = totalRows / page.PageSize;
                if (totalRows % page.PageSize != 0 || page.PageNo == 0)
                {
                    page.PageNo++;
                }
            }

            query = query.Skip((page.PageNo - 1) * page.PageSize).Take(page.PageSize);

            return(query.ToList());
        }
 public IActionResult Seed([FromServices] LoanDb db)
 {
     db.Seed();
     return(Ok());
 }
Esempio n. 24
0
 public LoanController()
 {
     _db = new LoanDb();
 }
        public IActionResult UpdatePartial(int id, [FromBody] dynamic loan, [FromServices] LoanDb db)
        {
            int rowsAffected = _loanDao.UpdatePartial(id, loan, db);

            return(Ok(new { rowsaffected = rowsAffected }));
        }
Esempio n. 26
0
 public ScheduleController()
 {
     _db = new LoanDb();
 }