/// <summary> /// Update existing borrower or insert a new one /// </summary> /// <param name="model"></param> /// <exception cref="Services.Exceptions.AlreadyExistsException"> /// Thrown if trying to create a new borrower with id that already exists </exception> /// <exception cref="Services.Exceptions.DataAccessException"> /// Thrown when an error occurs in the data access layer.</exception> static public void Upsert(BorrowerViewModel viewModel) { Borrower borrower = Mapper.Map<Borrower>(viewModel); if (viewModel.New) { Borrower existingBorrower = null; if (Borrower.GetBorrower(out existingBorrower, borrower.PersonId)) { if (existingBorrower != null) { throw new AlreadyExistsException("En låntagare med det personnumret finns redan."); } } else { throw new DataAccessException("Oväntat fel när låntagare skulle uppdateras."); } } if(!Borrower.Upsert(borrower)) { throw new DataAccessException("Oväntat fel när låntagare skulle uppdateras."); } if (viewModel.Account.NewPassword != null) { AccountViewModel account = Mapper.Map<AccountViewModel>(viewModel); AccountServices.Upsert(account); } }
public ActionResult Borrower(BorrowerViewModel borrower, string password, string passwordRetry) { var errors = new List<string>(); string err; bool upsertSuccess = false; if (ModelState.IsValid) { try { BorrowerServices.Upsert(borrower); borrower = BorrowerServices.GetBorrower(borrower.PersonId); borrower.New = false; upsertSuccess = true; } catch (AlreadyExistsException e) { errors.Add(e.Message); } catch (DoesNotExistException e) { errors.Add(e.Message); } catch (DataAccessException e) { errors.Add(e.Message); } } err = setBorrowerViewLists(borrower); if (err != null) errors.Add(err); if (errors.Count > 0) ViewBag.error = errors; if (!upsertSuccess) return View(borrower); else return RedirectToAction("Borrower", "Search", new { search = borrower.PersonId }); }
/// <summary> /// Populate selectlists in /edit/borrower. /// </summary> private string setBorrowerViewLists(BorrowerViewModel borrower) { try { var categoryDic = CategoryServices.GetCategoriesAsDictionary(); borrower.Category = new SelectList(categoryDic.OrderBy(x => x.Value), "Key", "Value"); } catch (DataAccessException e) { borrower.Category = new SelectList(new List<SelectListItem>()); return e.Message; } return null; }
public ActionResult Borrower(string PersonId) { var errors = new List<string>(); BorrowerViewModel borrower = new BorrowerViewModel(); borrower.New = true; if (PersonId != null) { try { borrower = BorrowerServices.GetBorrower(PersonId); borrower.New = false; } catch (DoesNotExistException e) { errors.Add(e.Message); } catch (DataAccessException e) { errors.Add(e.Message); } } string err = setBorrowerViewLists(borrower); if (err != null) errors.Add(err); if (errors.Count > 0) ViewBag.error = errors; return View(borrower); }