public static ServiceResponseResult AddRentalListing(RentListingModel model, HttpFileCollectionBase files, Login user) { using (var db = new KeysEntities()) { var foundProp = db.OwnerProperty.FirstOrDefault(x => x.PropertyId == model.PropertyId && x.OwnerId == user.Id); if (foundProp == null) { return(new ServiceResponseResult { IsSuccess = false, ErrorMessage = "Can not find property." }); } var newRentalListing = new RentalListing { PropertyId = model.PropertyId, Description = model.Description, Title = model.Title = model.Title, MovingCost = model.MovingCost, TargetRent = model.TargetRent, AvailableDate = model.AvailableDate, Furnishing = model.Furnishing, IdealTenant = model.IdealTenant, OccupantCount = model.OccupantCount, PetsAllowed = model.PetsAllowed, CreatedBy = user.Email, CreatedOn = DateTime.UtcNow, UpdatedBy = user.Email, IsActive = true, RentalStatusId = 1 }; var fileList = MediaService.SaveMediaFiles(files, 5, null).NewObject as List <MediaModel>; if (fileList != null) { fileList.ForEach(x => newRentalListing.RentalListingMedia.Add(new RentalListingMedia { NewFileName = x.NewFileName, OldFileName = x.OldFileName })); } db.RentalListing.Add(newRentalListing); try { db.SaveChanges(); return(new ServiceResponseResult { IsSuccess = true }); } catch (Exception ex) { return(new ServiceResponseResult { IsSuccess = true, ErrorMessage = _error }); } } }
public ActionResult AddRentalListing(RentListingModel model) { var user = User.Identity.Name; if (String.IsNullOrEmpty(user)) { return(Json(new { Success = false })); } var login = AccountService.GetLoginByEmail(user); if (ModelState.IsValid) { var files = Request.Files; var result = RentalService.AddRentalListing(model, files, login); return(Json(new { Success = result.IsSuccess })); } return(Json(new { Success = false })); }
public static ServiceResponseResult <List <MediaModel> > EditRentalListing(RentListingModel model, HttpFileCollectionBase files, Login user) { using (var db = new KeysEntities()) { var foundProp = db.OwnerProperty.FirstOrDefault(x => x.PropertyId == model.PropertyId && x.OwnerId == user.Id); if (foundProp == null) { return(new ServiceResponseResult <List <MediaModel> > { IsSuccess = false, ErrorMessage = "Can not find property." }); } var oldRentalListing = db.RentalListing.Find(model.Id); oldRentalListing.Description = model.Description; oldRentalListing.Title = model.Title; oldRentalListing.MovingCost = model.MovingCost; oldRentalListing.TargetRent = model.TargetRent; oldRentalListing.AvailableDate = model.AvailableDate; oldRentalListing.Furnishing = model.Furnishing; oldRentalListing.IdealTenant = model.IdealTenant; oldRentalListing.OccupantCount = model.OccupantCount; oldRentalListing.PetsAllowed = model.PetsAllowed; oldRentalListing.UpdatedBy = user.Email; oldRentalListing.UpdatedOn = DateTime.UtcNow; if (model.FilesRemoved != null) { model.FilesRemoved.ToList().ForEach(x => { var media = oldRentalListing.RentalListingMedia.FirstOrDefault(y => y.Id == x); if (media != null) { db.RentalListingMedia.Remove(media); MediaService.RemoveMediaFile(media.NewFileName); } }); } var fileList = MediaService.SaveMediaFiles(files, 5, null).NewObject as List <MediaModel>; if (fileList != null) { fileList.ForEach(x => oldRentalListing.RentalListingMedia.Add(new RentalListingMedia { NewFileName = x.NewFileName, OldFileName = x.OldFileName })); } try { db.SaveChanges(); var medias = oldRentalListing.RentalListingMedia .Select(x => MediaService.GenerateViewProperties(new MediaModel { Id = x.Id, NewFileName = x.NewFileName, OldFileName = x.OldFileName })).ToList(); return(new ServiceResponseResult <List <MediaModel> > { IsSuccess = true, Result = medias }); } catch (Exception ex) { return(new ServiceResponseResult <List <MediaModel> > { IsSuccess = true, ErrorMessage = _error }); } } }