public bool CreateTextBook(UserInformationModel<User> aCreatingUser, TextBookViewModel aCreateTextBookModel) { if (!ValidTextBook(aCreateTextBookModel)) { return false; } Class myClass = theClassRepo.GetClass(aCreateTextBookModel.UniversityId, aCreateTextBookModel.ClassSubject, aCreateTextBookModel.ClassCourse); if(myClass == null) { theClassRepo.CreateClass(aCreatingUser.Details, aCreateTextBookModel.UniversityId, aCreateTextBookModel.ClassSubject, aCreateTextBookModel.ClassCourse, "No title"); } string myBookImageName = string.Empty; if (aCreateTextBookModel.BookImage != null) { try { myBookImageName = AWSPhotoHelper.TakeImageAndResizeAndUpload(aCreateTextBookModel.BookImage, AWSHelper.GetClient(), SiteConfiguration.TextbookPhotosBucket(), aCreateTextBookModel.BookTitle.GetHashCode().ToString(), TextBookConstants.BOOK_MAX_SIZE); } catch (Exception myException) { throw new PhotoException("Unable to upload the textbook image.", myException); } } theTextBookRepo.CreateTextbook(aCreatingUser.Details, aCreateTextBookModel.UniversityId, aCreateTextBookModel.TextBookCondition, aCreateTextBookModel.BookTitle, aCreateTextBookModel.BookAuthor, myBookImageName, aCreateTextBookModel.ClassSubject, aCreateTextBookModel.ClassCourse, aCreateTextBookModel.Edition == null ? 0 : int.Parse(aCreateTextBookModel.Edition), double.Parse(aCreateTextBookModel.Price), string.IsNullOrEmpty(aCreateTextBookModel.Details) ? null : aCreateTextBookModel.Details, aCreateTextBookModel.ISBN); return true; }
private bool ValidTextBook(TextBookViewModel aCreateTextBoook) { if (string.IsNullOrEmpty(aCreateTextBoook.UniversityId)) { theValidationDictionary.AddError("UniversityId", aCreateTextBoook.UniversityId, "A university must be present."); } if (string.IsNullOrEmpty(aCreateTextBoook.BookTitle)) { theValidationDictionary.AddError("BookTitle", aCreateTextBoook.BookTitle, "You must specify a book title."); } if (string.IsNullOrEmpty(aCreateTextBoook.BookAuthor)) { theValidationDictionary.AddError("BookAuthor", aCreateTextBoook.BookAuthor, "You must specify the book author(s)."); } double myNum; if (!double.TryParse(aCreateTextBoook.Price, out myNum) || myNum < 0.01) { theValidationDictionary.AddError("Price", aCreateTextBoook.Price.ToString(), "The asking price must be above zero."); } int myEdition; if (!string.IsNullOrEmpty(aCreateTextBoook.Edition) && (!int.TryParse(aCreateTextBoook.Edition, out myEdition) || myEdition < 0)) { theValidationDictionary.AddError("Edition", aCreateTextBoook.Edition.ToString(), "If you specify an edition is must be non-zero and non-negative."); } if (!DropDownItemValidation.IsValid(aCreateTextBoook.TextBookCondition)) { theValidationDictionary.AddError("TextBookCondition", aCreateTextBoook.TextBookCondition, "You must specify the condition of the textbook."); } if (aCreateTextBoook.BookImage != null && !PhotoValidation.IsValidImageFile(aCreateTextBoook.BookImage.FileName)) { theValidationDictionary.AddError("BookImage", aCreateTextBoook.BookImage.FileName, PhotoValidation.INVALID_IMAGE); } if (!string.IsNullOrEmpty(aCreateTextBoook.ClassSubject) || !string.IsNullOrEmpty(aCreateTextBoook.ClassCourse)) { if (string.IsNullOrEmpty(aCreateTextBoook.ClassSubject)) { theValidationDictionary.AddError("ClassSubject", string.Empty, "If you want to tag this book to a class you must give the Class Subject. Example: CIS"); } if (string.IsNullOrEmpty(aCreateTextBoook.ClassCourse)) { theValidationDictionary.AddError("ClassCourse", string.Empty, "If you want to tag this book to a class you must give the Class Course Number. Example: 556707"); } } return theValidationDictionary.isValid; }
public bool EditTextBook(UserInformationModel<User> aUserInfo, TextBookViewModel aTextBookViewModel) { if (!ValidTextBook(aTextBookViewModel)) { return false; } TextBook myTextBook = GetTextBook(aTextBookViewModel.TextBookId); myTextBook.BookTitle = aTextBookViewModel.BookTitle; myTextBook.Edition = aTextBookViewModel.Edition == null ? 0 : int.Parse(aTextBookViewModel.Edition); myTextBook.TextBookConditionId = aTextBookViewModel.TextBookCondition; myTextBook.Details = aTextBookViewModel.Details; myTextBook.Price = double.Parse(aTextBookViewModel.Price); myTextBook.ClassSubject = string.IsNullOrEmpty(aTextBookViewModel.ClassSubject) ? null : aTextBookViewModel.ClassSubject; myTextBook.ClassCourse = string.IsNullOrEmpty(aTextBookViewModel.ClassCourse) ? null : aTextBookViewModel.ClassCourse; myTextBook.BookAuthor = aTextBookViewModel.BookAuthor; myTextBook.ISBN = string.IsNullOrEmpty(aTextBookViewModel.ISBN) ? null : aTextBookViewModel.ISBN; theTextBookRepo.UpdateTextBook(myTextBook); if (aTextBookViewModel.BookImage != null) { string myOldTextBookImage = myTextBook.BookPicture; UpdateTextBookPhoto(aTextBookViewModel.TextBookId.ToString(), aTextBookViewModel.BookImage, myTextBook); if (!string.IsNullOrEmpty(myOldTextBookImage)) { AWSPhotoHelper.PhysicallyDeletePhoto(AWSHelper.GetClient(), SiteConfiguration.TextbookPhotosBucket(), myOldTextBookImage); } } return true; }
public ActionResult Create(string universityId, string classSubject, string classCourse) { if (!IsLoggedIn()) { return RedirectToLogin(); } if (!UniversityHelper.IsFromUniversity(GetUserInformatonModel().Details, universityId)) { return SendToResultPage(UOMConstants.NOT_APART_OF_UNIVERSITY); } IDictionary<string, string> myBookConditionTypes = DictionaryHelper.DictionaryWithSelect(); try { myBookConditionTypes = theTextBookService.CreateTextBookConditionsDictionaryEntry(); } catch (Exception myException) { LogError(myException, GET_TEXTBOOK_CONDITIONS_ERROR); ViewData["Message"] = MessageHelper.ErrorMessage(GET_TEXTBOOK_CONDITIONS_ERROR); } try { LoggedInWrapperModel<TextBookViewModel> myLoggedIn = new LoggedInWrapperModel<TextBookViewModel>(GetUserInformatonModel().Details); TextBookViewModel myCreateTextbookModel = new TextBookViewModel() { UniversityId = universityId, TextBookConditions = new SelectList(myBookConditionTypes, "Value", "Key"), ClassSubject = classSubject, ClassCourse = classCourse }; myLoggedIn.Set(myCreateTextbookModel); return View("Create", myLoggedIn); } catch(Exception myException) { LogError(myException, ErrorKeys.ERROR_MESSAGE); return SendToErrorPage(ErrorKeys.ERROR_MESSAGE); } }
public ActionResult Edit(TextBookViewModel aViewModel) { if (!IsLoggedIn()) { return RedirectToLogin(); } try { UserInformationModel<User> myUserInfo = GetUserInformatonModel(); bool myResult = theTextBookService.EditTextBook(myUserInfo, aViewModel); if (myResult) { TempData["Message"] += MessageHelper.SuccessMessage(TEXTBOOK_EDITED); return RedirectToAction("Details", new { id = aViewModel.TextBookId }); } } catch (PhotoException anException) { LogError(anException, anException.Message); TempData["Message"] += MessageHelper.ErrorMessage(TEXTBOOK_PHOTO_UPLOAD_ERROR); } catch (CustomException anException) { LogError(anException, anException.Message); TempData["Message"] += MessageHelper.ErrorMessage(TEXTBOOK_PHOTO_UPLOAD_ERROR); } catch (Exception myException) { LogError(myException, TEXTBOOK_EDIT_ERROR); TempData["Message"] += MessageHelper.ErrorMessage(TEXTBOOK_EDIT_ERROR); theValidation.ForceModleStateExport(); } return RedirectToAction("Edit", new { id = aViewModel.TextBookId }); }
public ActionResult Edit(string universityId, int id) { if (!IsLoggedIn()) { return RedirectToLogin(); } IDictionary<string, string> myBookConditionTypes = DictionaryHelper.DictionaryWithSelect(); try { myBookConditionTypes = theTextBookService.CreateTextBookConditionsDictionaryEntry(); } catch (Exception myException) { LogError(myException, GET_TEXTBOOK_CONDITIONS_ERROR); ViewData["Message"] = MessageHelper.ErrorMessage(GET_TEXTBOOK_CONDITIONS_ERROR); return RedirectToAction("Details", new { id = id }); } try { UserInformationModel<User> myUserInfo = GetUserInformatonModel(); TextBook myTextBook = theTextBookService.GetTextBookForEdit(myUserInfo, id); LoggedInWrapperModel<TextBookViewModel> myLoggedIn = new LoggedInWrapperModel<TextBookViewModel>(GetUserInformatonModel().Details); TextBookViewModel myCreateTextbookModel = new TextBookViewModel(myTextBook) { UniversityId = universityId, TextBookConditions = new SelectList(myBookConditionTypes, "Value", "Key", myTextBook.TextBookConditionId) }; myLoggedIn.Set(myCreateTextbookModel); return View("Edit", myLoggedIn); } catch (PermissionDenied myException) { TempData["Message"] += MessageHelper.WarningMessage(myException.Message); } catch (Exception myException) { LogError(myException, ErrorKeys.ERROR_MESSAGE); TempData["Message"] += MessageHelper.ErrorMessage(TEXTBOOK_GET_ERROR); } return RedirectToAction("Details", new { id = id }); }
public ActionResult Create(TextBookViewModel textbook) { if (!IsLoggedIn()) { return RedirectToLogin(); } UserInformationModel<User> myUserInformation = GetUserInformatonModel(); try { bool myResult = theTextBookService.CreateTextBook(myUserInformation, textbook); if (myResult) { TempData["Message"] += MessageHelper.SuccessMessage(TEXTBOOK_ADDED); return RedirectToAction("List"); } } catch(PhotoException myException) { LogError(myException, TEXTBOOK_IMAGE_UPLOAD_ERROR); TempData["Message"] += MessageHelper.ErrorMessage(TEXTBOOK_IMAGE_UPLOAD_ERROR); } catch (Exception myException) { LogError(myException, ErrorKeys.ERROR_MESSAGE); TempData["Message"] += MessageHelper.ErrorMessage(ErrorKeys.ERROR_MESSAGE); } theValidation.ForceModleStateExport(); return RedirectToAction("Create"); }