private SaveSharePostResponse CreateSharePost(SharePostVM vm, Guid userId, IEnumerable <HttpPostedFileBase> unsavedPictures) { var isPictureValid = ValidatePictrues(vm.PostId, unsavedPictures); if (!isPictureValid) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } var sharePost = new SharePost() { UserId = userId }; var address = new Address(); if (!MapSharePostVMToSharePost(vm, sharePost, true) || !MapSharePostVMToAddress(vm, address)) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } SharePostUnitOfWork.SharePosts.Add(sharePost); address.PostId = sharePost.PostId; SharePostUnitOfWork.Addresses.Add(address); SavePictures(unsavedPictures, sharePost.PostId); SharePostUnitOfWork.Complete(); var encryptedPostId = Encryptor.GetEncryptedString(sharePost.PostId.ToString()); return(new SaveSharePostResponse(true, encryptedPostId)); }
public bool SetSharePostVMForEdit(SharePostVM vm, string userEmail) { Guid userId = base.GetUserIdFromUserEmail(userEmail); if (userId == Guid.Empty) { return(false); } var sharePost = GetSharePostForUserId(vm.PostId, userId, true); if (sharePost == null) { return(false); } PopulateCategories(vm); Mapper.SharePostToSharePostVM(sharePost, vm); if (sharePost.Pictures != null) { vm.SavedPictures = Mapper.PicturesToPictureVMs(sharePost.Pictures, new List <PictureVM>()); } return(true); }
public SharePostVM PopulateCategories(SharePostVM vm) { vm.SharePostCategories.Places = SharePostUnitOfWork.Places.GetAll(); vm.SharePostCategories.SydneySuburbs = Mapper.SuburbToSuburbVM(SharePostUnitOfWork.Suburbs.WhereBy(m => m.PlaceId == (int)PlaceId.Sydney), new List <SuburbVM>()); vm.SharePostCategories.MelbourneSuburbs = Mapper.SuburbToSuburbVM(SharePostUnitOfWork.Suburbs.WhereBy(m => m.PlaceId == (int)PlaceId.Melbourne), new List <SuburbVM>()); vm.SharePostCategories.BrisbaneSuburbs = Mapper.SuburbToSuburbVM(SharePostUnitOfWork.Suburbs.WhereBy(m => m.PlaceId == (int)PlaceId.Brisbane), new List <SuburbVM>()); vm.SharePostCategories.AdelaideSuburbs = Mapper.SuburbToSuburbVM(SharePostUnitOfWork.Suburbs.WhereBy(m => m.PlaceId == (int)PlaceId.Adelaide), new List <SuburbVM>()); vm.SharePostCategories.PerthSuburbs = Mapper.SuburbToSuburbVM(SharePostUnitOfWork.Suburbs.WhereBy(m => m.PlaceId == (int)PlaceId.Perth), new List <SuburbVM>()); vm.SharePostCategories.Genders = SharePostUnitOfWork.Genders.GetAll(); vm.SharePostCategories.ShareTypes = SharePostUnitOfWork.ShareTypes.GetAll(); return(vm); }
private void UrlDecodeSharePostVM(SharePostVM vm) { //later placeId will be replaced with placeName //then needs to be url decode? vm.Suburb = HttpUtility.UrlDecode(vm.Suburb); vm.DateAvailableFrom = HttpUtility.UrlDecode(vm.DateAvailableFrom); vm.ShareType = HttpUtility.UrlDecode(vm.ShareType); vm.Title = HttpUtility.UrlDecode(vm.Title); vm.Description = HttpUtility.UrlDecode(vm.Description); vm.Gender = HttpUtility.UrlDecode(vm.Gender); vm.PostId = HttpUtility.UrlDecode(vm.PostId); }
public ActionResult EditSharePost(string currentPostId, string returnUrl = null) { var sharePostVM = new SharePostVM(); var valid = SharePostService.SetSharePostVMForEdit(sharePostVM, GetEmailFromAuthCookie()); if (!valid) { return(View("Error")); } return(View("SharePost", sharePostVM)); }
public static SharePostVM SharePostToSharePostVM(SharePost sharePost, SharePostVM vm) { vm.DateAvailableFrom = sharePost.AvailableFrom.ToShortDateString(); vm.Description = sharePost.Description; vm.Gender = sharePost.Gender.Name; vm.PlaceId = sharePost.PlaceId; vm.PostId = Encryptor.GetEncryptedString(sharePost.PostId.ToString()); vm.ShareType = sharePost.ShareType.Name; vm.Price = sharePost.Price; vm.Suburb = sharePost.Address.Suburb.Name; vm.Title = sharePost.Title; return(vm); }
private bool MapSharePostVMToAddress(SharePostVM vm, Address address) { if (SharePostUnitOfWork.Places.FindById(vm.PlaceId) == null) { return(false); } var suburb = SharePostUnitOfWork.Suburbs.SingleOrDefault(m => (m.PlaceId == vm.PlaceId) && (m.Name == vm.Suburb)); if (suburb == null) { return(false); } address.PlaceId = vm.PlaceId; address.SuburbId = suburb.SuburbId; return(true); }
public static SharePost SharePostVMToSharePost(SharePostVM vm, SharePost sharePost, byte shareTypeId, byte genderId, bool isCreation) { sharePost.AvailableFrom = GetDateTimeFrom(vm.DateAvailableFrom); if (isCreation) { sharePost.DateCreated = DateTime.Now; } sharePost.Description = vm.Description; sharePost.GenderId = genderId; sharePost.Price = vm.Price.Value; sharePost.ShareTypeId = shareTypeId; sharePost.Title = vm.Title; sharePost.PlaceId = vm.PlaceId; return(null); }
public SaveSharePostResponse SaveSharePost(SharePostVM vm, IEnumerable <HttpPostedFileBase> unsavedPictures, string userEmail) { Guid userId = base.GetUserIdFromUserEmail(userEmail); if (userId == Guid.Empty) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } UrlDecodeSharePostVM(vm); if (vm.PostId == null) { return(CreateSharePost(vm, userId, unsavedPictures)); } else { return(UpdateSharePost(vm, userId, unsavedPictures)); } }
private bool MapSharePostVMToSharePost(SharePostVM vm, SharePost sharePost, bool isCreation) { var shareType = SharePostUnitOfWork.ShareTypes.SingleOrDefault(m => m.Name == vm.ShareType); if (shareType == null) { return(false); } var gender = SharePostUnitOfWork.Genders.SingleOrDefault(m => m.Name == vm.Gender); if (gender == null) { return(false); } Mapper.SharePostVMToSharePost(vm, sharePost, shareType.ShareTypeId, gender.GenderId, isCreation); return(true); }
public ActionResult SharePost(SharePostVM vm, IEnumerable <HttpPostedFileBase> unsavedPictures, string returnUrl = null) { if (!ModelState.IsValid) { Response.StatusCode = 400; return(Json(GetErrorList(ModelState))); } var response = SharePostService.SaveSharePost(vm, unsavedPictures, GetEmailFromAuthCookie()); if (!response.Success) { var errorList = new List <ErrorVM> { new ErrorVM { Key = "general-error", ErrorMsg = response.ErrorCode.ToDescription() } }; Response.StatusCode = 400; return(Json(errorList)); } return(Json(new { placeId = 1 })); }
private SaveSharePostResponse UpdateSharePost(SharePostVM vm, Guid userId, IEnumerable <HttpPostedFileBase> unsavedPictures) { var sharePost = GetSharePostForUserId(vm.PostId, userId, false); if (sharePost == null) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } var address = SharePostUnitOfWork.Addresses.SingleOrDefault(m => m.PostId == sharePost.PostId); if (address == null) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } if (!MapSharePostVMToSharePost(vm, sharePost, false) || !MapSharePostVMToAddress(vm, address)) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } try { UpdateSavedPictures(vm.SavedPictures, sharePost.PostId); } catch (Exception e) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } SharePostUnitOfWork.Complete(); var isPictureValid = ValidatePictrues(vm.PostId, unsavedPictures); if (!isPictureValid) { return(new SaveSharePostResponse(false, ErrorCode.BADREQUEST)); } SavePictures(unsavedPictures, sharePost.PostId); SharePostUnitOfWork.Complete(); return(null); }