public virtual ActionResult Create(LendViewModel lendVM) { if (ModelState.IsValid) { lendVM.CreatedAt = DateTime.Now; //get currently logged in user var dbUser = General.GetCurrentUser(_userRepository); var model = _mapper.Map <LendViewModel, Lend>(lendVM); model.UserId = dbUser.Id; _lendRepository.InsertLend(model); //update Availability of LendItem var lenditem = _lendItemRepository.GetLendItemByID(lendVM.LendItemId); lenditem.Avialable--; _lendItemRepository.UpdateLendItem(lenditem); return(RedirectToAction("Index")); } return(View(lendVM)); }
public virtual ActionResult Edit(LendItemViewModel lendItemVM, HttpPostedFileBase uploadfile) { if (ModelState.IsValid) { //get data from DB var model = _lendItemRepository.GetLendItemByID(lendItemVM.Id); //convert vm into Model for comparing / updating values var updatedlineitemmodel = _mapper.Map <LendItemViewModel, LendItem>(lendItemVM); model.Id = updatedlineitemmodel.Id; model.Name = updatedlineitemmodel.Name; model.Description = updatedlineitemmodel.Description; model.CustomerId = updatedlineitemmodel.CustomerId; model.Condition = updatedlineitemmodel.Condition; if (updatedlineitemmodel.Quantity < model.Quantity && (model.Quantity - model.Avialable) > updatedlineitemmodel.Quantity) { ModelState.AddModelError("availability", "es sind bereits Ausleihungen getätigt worden für diesen Gegenstand"); return(View(lendItemVM)); } else { model.Quantity = updatedlineitemmodel.Quantity; } //process Attached Images if (uploadfile != null) { lendItemVM.ItemImageViewModels = ImageInsert.InsertImages(uploadfile).AsEnumerable(); //update lenditem - file ID (only for one image) var firstOrDefault = lendItemVM.ItemImageViewModels.FirstOrDefault(); if (firstOrDefault != null) { lendItemVM.FileId = firstOrDefault.Id; } model.FileId = lendItemVM.FileId; } _lendItemRepository.UpdateLendItem(model); return(RedirectToAction("Index")); } return(View(lendItemVM)); }
public virtual ActionResult Create(LendReturnViewModel lendReturnVM, HttpPostedFileBase uploadfile) { //todo refactoring and improvement§ if (ModelState.IsValid) { lendReturnVM.UserId = General.GetCurrentUser(_userRepository).Id; lendReturnVM.CreatedBy = General.GetCurrentUser(_userRepository); lendReturnVM.CreatedAt = DateTime.Now; lendReturnVM.FileId = null; //process Return Attached Images if (uploadfile != null) { lendReturnVM.ReturnImageViewModels = ImageInsert.InsertImages(uploadfile).AsEnumerable(); //create Return model var firstOrDefault = lendReturnVM.ReturnImageViewModels.FirstOrDefault(); if (firstOrDefault != null) { lendReturnVM.FileId = firstOrDefault.Id; } } var model = _mapper.Map <LendReturnViewModel, LendReturn>(lendReturnVM); _lendReturnRepository.InsertReturn(model); //get Lend and update it var lendmodel = _lendRepository.GetLendByID(model.LendId); lendmodel.LendReturnId = model.Id; lendmodel.To = DateTime.Now; _lendRepository.UpdateLend(lendmodel); //get LendItem and update availability if (model.SetComplete != null && model.SetComplete.Value) { var lenditemmodel = _lendItemRepository.GetLendItemByID(lendmodel.LendItemId); lenditemmodel.Avialable++; _lendItemRepository.UpdateLendItem(lenditemmodel); } return(RedirectToAction("..\\Lends\\Index")); } return(View(lendReturnVM)); }