protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); IUnityContainer container = new UnityContainer(); var customXmlRW = new DataBase(@"C:\Users\stefan\Desktop\db.xml"); // Register ViewModels var loginViewModel = new LoginViewModel(customXmlRW); container.RegisterInstance <LoginViewModel>(loginViewModel); var showImagesViewModel = new ImagesViewModel(); container.RegisterType <ImagesViewModel>(); var addImageViewModel = new AddImageViewModel(); container.RegisterType <AddImageViewModel>(); var accountDetailsViewModel = new AccountDetailsViewModel(); container.RegisterType <AccountDetailsViewModel>(); var contentViewModel = new ContentViewModel(container, customXmlRW); container.RegisterInstance <ContentViewModel>(contentViewModel); var mainWindowViewModel = new MainWindowViewModel(container); container.RegisterInstance <MainWindowViewModel>(mainWindowViewModel); var mainWindow = container.Resolve <MainWindow>(); mainWindow.DataContext = container.Resolve <MainWindowViewModel>(); Application.Current.MainWindow = mainWindow; Application.Current.MainWindow.Show(); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } var imageForCreation = new ImageForCreationModel { Title = addImageViewModel.Title }; var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } var serializedImageForCreation = JsonConvert.SerializeObject(imageForCreation); var httpClient = await _imageGalleryHttpClient.GetHttpClientAsync(); var response = await httpClient.PostAsync( $"api/images", new StringContent(serializedImageForCreation, System.Text.Encoding.Unicode, "application/json")); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { var httpClient = _clientFactory.CreateClient("Api"); var addImageModel = new AddImageModel { Title = addImageViewModel.Title, FileName = addImageViewModel.Files.First().FileName }; var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using var fileStream = imageFile.OpenReadStream(); using var memoryStream = new MemoryStream(); fileStream.CopyTo(memoryStream); addImageModel.Bytes = memoryStream.ToArray(); } var serializedImage = JsonSerializer.Serialize(addImageModel); var request = new HttpRequestMessage(HttpMethod.Post, "/images") { Content = new StringContent(serializedImage, Encoding.Unicode, MediaTypeNames.Application.Json) }; var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
public bool AddCarImage(AddImageViewModel addImageView) { if (addImageView.File != null && !string.IsNullOrEmpty(addImageView.Description) && !string.IsNullOrEmpty(addImageView.View)) { string path = _imgService.CreateImage(addImageView.File, AppConstant.ImageFolder, _env); if (!string.IsNullOrEmpty(path)) { CarImage carImageToAdd = new CarImage { CarId = addImageView.CarId, CarImagePath = path, Description = addImageView.Description, View = addImageView.View }; try { _ctx.CarImages.Add(carImageToAdd); _ctx.SaveChanges(); return(true); } catch (DbUpdateException ex) { _logger.LogInformation($"{ex.Message}"); } } } return(false); }
public void AddImage(AddImageViewModel model) { Product product = _dbContext.Products.FirstOrDefault(c => c.ProductId == model.ProductId); product.Image = model.ByteImage; _dbContext.SaveChanges(); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForCreation instance var imageForCreation = new ImageForCreation { Title = addImageViewModel.Title }; // take the first (only) file in the Files list var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } await _imageGalleryApiClient.AddImage(imageForCreation).ConfigureAwait(false); return(RedirectToAction("Index")); }
//[ValidateAntiForgeryToken] public async Task <IActionResult> Add(AddImageViewModel model) { if (ModelState.IsValid) { string uniqueFileName = await UploadedFileAsync(model); IImages image = new IImages { Id = model.Id, Link = uniqueFileName, }; var result = await imageService.AddImage(image); if (result.StatusCode == 200) { await SendEmail(); //return Redirect("/Image/GetAll"); return(Redirect("http://vnhcmm0teapp02/imageapp")); } else { return(View(model)); } } return(View(model)); }
public void AddErrorToModelState_IfFileIsNull() { // Arrange var mockedImageGalleryService = new Mock <IImageGalleryService>(); var mockedImageFactory = new Mock <IImageFactory>(); var mockedDateProvider = new Mock <IDateProvider>(); var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.GetAll()).Verifiable(); var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>(); var mockedDirectoryHelper = new Mock <IDirectoryHelper>(); var controller = new PicturesController( mockedImageGalleryService.Object, mockedImageFactory.Object, mockedDateProvider.Object, mockedLakeService.Object, mockedImageGalleryFactory.Object, mockedDirectoryHelper.Object); var model = new AddImageViewModel(); // Act var result = controller.Add(null, model) as ViewResult; // Assert Assert.AreEqual( GlobalMessages.NoFileErrorMessage, result.ViewData.ModelState[""].Errors.First().ErrorMessage); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForCreation instance var imageForCreation = new ImageForCreation() { Title = addImageViewModel.Title }; // take the first (only) file in the Files list var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } // serialize it var serializedImageForCreation = JsonSerializer.Serialize(imageForCreation); var httpClient = _httpClientFactory.CreateClient("APIClient"); var request = new HttpRequestMessage( HttpMethod.Post, $"/api/images"); request.Content = new StringContent( serializedImageForCreation, System.Text.Encoding.Unicode, "application/json"); var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); //if (response.IsSuccessStatusCode) //{ // return RedirectToAction("Index"); //} //else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || // response.StatusCode == System.Net.HttpStatusCode.Forbidden) //{ // return RedirectToAction("ActionDenied", "Authorization"); //} //throw new Exception("Problem using Api"); }
public ActionResult Add() { var model = new AddImageViewModel(); this.LoadNames(model); return(View(model)); }
public async Task <IActionResult> Index(AddImageViewModel vm) { var newVM = new AddImageViewModel { Categories = await _categoryService.Get() }; return(View(newVM)); }
public void ShouldCreateNewInstance() { // Act var instance = new AddImageViewModel(); // Assert Assert.NotNull(instance); Assert.IsType <AddImageViewModel>(instance); }
public ActionResult Add(HttpPostedFileBase file, AddImageViewModel model) { if (file == null) { ModelState.AddModelError("", GlobalMessages.NoFileErrorMessage); } else if (ModelState.IsValid) { var isValidFIle = file.ContentLength > 0 && file.ContentLength <= Constants.ImageMaxSize; if (isValidFIle) { try { var date = this.dateProvider.GetDate(); var lakeName = this.lakeService.GetLakeName(model.SelectedLakeId); var path = Server.MapPath( $"{Constants.ImageGalleriesBaseServerFolder}/{lakeName}/"); directoryHelper.CreateIfNotExist(path); var url = $"{Constants.ImageGalleriesBaseFolder}/{lakeName}/{file.FileName}"; file.SaveAs(Server.MapPath(url)); var image = this.imageFactory.CreateImage(url, date, model.ImageInfo); if (User.IsInRole("Moderator")) { image.IsConfirmed = true; } var gallery = this.imageGalleryService.FindById(model.SelectedImageGalleryId); if (gallery == null) { gallery = this.imageGalleryFactory.CreateImageGallery(model.Name, model.SelectedLakeId); this.imageGalleryService.Add(gallery); } gallery.Images.Add(image); this.imageGalleryService.Save(); } catch (ArgumentException ex) { ModelState.AddModelError("", ex.Message); } } } this.LoadNames(model); model.SelectedImageGalleryId = null; model.Name = null; return(View(model)); }
public ActionResult AddImage() { var viewModel = new AddImageViewModel() { Albums = imageDB.tblAlbums.ToList(), Conditions = imageDB.tblEnvironmentalConditions.ToList(), Features = imageDB.tblFeatures.ToList() }; return(View(viewModel)); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } // create an ImageForCreation instance var imageForCreation = new ImageForCreation() { Title = addImageViewModel.Title }; // take the first (only) file in the Files list var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } // serialize it var serializedImageForCreation = JsonConvert.SerializeObject(imageForCreation); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PostAsync( $"api/images", new StringContent(serializedImageForCreation, System.Text.Encoding.Unicode, "application/json")) .ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("AccessDenied", "Authorization")); } if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public void ShouldBeAbleToSetGetFiles() { // Arrange var instance = new AddImageViewModel(); var files = new List <IFormFile>(); // Act instance.Files = files; // Assert Assert.Same(files, instance.Files); }
public void ShouldGetFilesPropertyAfterCreation() { // Arrange var instance = new AddImageViewModel(); // Act var files = instance.Files; // Assert Assert.NotNull(files); Assert.IsType <List <IFormFile> >(files); }
public async Task <IActionResult> AddImage([FromBody] AddImageViewModel model) { var user = await GetCurrentUserAsync(); var imagePath = ImageHelper.SaveImage(model.ImageData, model.Title); var imageParams = new ImageParams(user.Id, model.Title, model.Description, imagePath); var image = _imageService.CreateOrUpdate(imageParams); var imageModel = _mapper.Map <ImageListModel>(image); return(Ok(imageModel)); }
public ActionResult AddImage(AddImageViewModel model, HttpPostedFileBase image1) { if (image1 != null && (image1.ContentType.Equals("image/png") || image1.ContentType.Equals("image/gif") || image1.ContentType.Equals("image/jpg") || image1.ContentType.Equals("image/jpeg"))) { model.ByteImage = new byte[image1.InputStream.Length]; image1.InputStream.Read(model.ByteImage, 0, image1.ContentLength); _productService.AddImage(model); } return(RedirectToAction("Details", new { productId = model.ProductId })); }
public async Task <IActionResult> Upload(AddImageViewModel vm) { var UploadImage = Request.Form.Files; var result = await _imageService.UploadImage(UploadImage, vm); bool addedImage = false; if (result != null) { addedImage = true; } return(RedirectToAction("Index", addedImage)); }
public ContentViewModel(IUnityContainer container, DataBase db) { _container = container; _dataBase = db; _ImagesViewModel = _container.Resolve <ImagesViewModel>(); _addImageViewModel = _container.Resolve <AddImageViewModel>(); _accountDetailsViewModel = _container.Resolve <AccountDetailsViewModel>(); CurrentContentViewModel = _ImagesViewModel; NavCommand = new Command <string>(OnNav); }
public void ShouldSetGetTitleProperty() { // Arrange var instance = new AddImageViewModel(); // Act instance.Title = "NewImageTitle"; var t = instance.Title; // Assert Assert.IsType <string>(instance.Title); Assert.Same(t, instance.Title); Assert.Equal("NewImageTitle", t); }
public ActionResult AddImage(Guid?productId) { if (productId == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = _productService.GetProductById(productId); AddImageViewModel model = new AddImageViewModel() { ProductId = (Guid)productId }; return(View(model)); }
public async void ShouldGetExceptionWhenAddImagePostMethodFailed() { // Arrange var client = GetMockOfIImageGalleryHttpClient(HttpStatusCode.BadRequest); var controller = new GalleryController(client.Object); var addImageViewModel = new AddImageViewModel(); addImageViewModel.Files.Add(await GetFormFileAsync()); // Act var result = controller.AddImage(addImageViewModel); // Assert var exception = await Assert.ThrowsAsync <Exception>(() => result); Assert.Equal("A problem happend while calling the API: Because this client's handler always fails", exception.Message); }
public async void ShouldGetViewResultWhenAddImagePostMethodInvalidatePostedData() { // Arrange var client = GetMockOfIImageGalleryHttpClient(HttpStatusCode.OK); var controller = new GalleryController(client.Object); controller.ModelState.AddModelError("Title", "Title is required"); var addImageViewModel = new AddImageViewModel(); // Action var response = await controller.AddImage(addImageViewModel); // Assert var viewResult = Assert.IsType <ViewResult>(response); Assert.Null(viewResult.Model); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // create an ImageForCreation instance var imageForCreation = new ImageForCreation { Title = addImageViewModel.Title, Category = addImageViewModel.Category, }; // take the first (only) file in the Files list var imageFile = addImageViewModel.File; if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } // serialize it var serializedImageForCreation = JsonConvert.SerializeObject(imageForCreation); // call the API var httpClient = await _imageGalleryHttpClient.GetClient(); var response = await httpClient.PostAsync( InternalImagesRoute, new StringContent(serializedImageForCreation, Encoding.Unicode, "application/json")) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { return(Ok()); } throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}"); }
public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel) { if (!ModelState.IsValid) { return(View()); } var imageForCreation = new ImageForCreation() { Title = addImageViewModel.Title, Description = addImageViewModel.Description }; var imageFile = addImageViewModel.Files.First(); if (imageFile.Length > 0) { using (var fileStream = imageFile.OpenReadStream()) using (var ms = new MemoryStream()) { fileStream.CopyTo(ms); imageForCreation.Bytes = ms.ToArray(); } } var serializedImageForCreation = JsonSerializer.Serialize(imageForCreation); var httpClient = _httpClientFactory.CreateClient("APIClient"); var request = new HttpRequestMessage( HttpMethod.Post, $"/api/images"); request.Content = new StringContent( serializedImageForCreation, System.Text.Encoding.Unicode, "application/json"); var response = await httpClient.SendAsync( request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false); response.EnsureSuccessStatusCode(); return(RedirectToAction("Index")); }
// GET: Image/Details/5 public async Task <IActionResult> AddToIdeaboard(int?id) { if (id == null) { return(NotFound()); } var user = await GetCurrentUserAsync(); AddImageViewModel model = new AddImageViewModel(_context, user); model.Image = await _context.Image .Include(i => i.Room) .Include(i => i.Style) .SingleOrDefaultAsync(m => m.ImageId == id); return(View(model)); }
public async Task <UploadResult> UploadImage(IFormFileCollection files, AddImageViewModel vm) { var result = new UploadResult(); foreach (var file in files) { if (UploadImageHelper.IsImage(file)) { vm.Name.Replace(' ', '_'); var fileName = $"{Guid.NewGuid()}{vm.Name}"; var fileStream = await UploadImageHelper.UploadImage(file, fileName, file.ContentType, _azureStorageConfig); if (fileStream != null) { //try //{ var client = new WebClient { Credentials = new NetworkCredential(CDNProfile.Username, CDNProfile.Password) }; client.UploadFile( "ftp://[email protected]/www/Picks", WebRequestMethods.Ftp.AppendFile, $"{fileStream.Name}"); var img = new Image() { CategoryId = vm.CategoryId, Name = vm.Name }; await _imageRepo.Add(img); //} //catch (Exception err) //{ // return null; //} } } } return(result); }
public void AddErrorMessageToModel_IfAddingImageFail() { // Arrange var mockedImageGalleryService = new Mock <IImageGalleryService>(); var mockedImageFactory = new Mock <IImageFactory>(); var mockedDateProvider = new Mock <IDateProvider>(); mockedDateProvider.Setup(d => d.GetDate()).Throws(new ArgumentException(message: "Test")); var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.GetAll()).Verifiable(); var mockedImageGalleryFactory = new Mock <IImageGalleryFactory>(); var mockedDirectoryHelper = new Mock <IDirectoryHelper>(); var mockedHttpContext = new Mock <ControllerContext>(); mockedHttpContext.Setup(c => c.HttpContext.Server.MapPath(It.IsAny <string>())).Returns("Test"); mockedHttpContext.Setup(c => c.HttpContext.User.IsInRole(It.IsAny <string>())).Returns(true); var controller = new PicturesController( mockedImageGalleryService.Object, mockedImageFactory.Object, mockedDateProvider.Object, mockedLakeService.Object, mockedImageGalleryFactory.Object, mockedDirectoryHelper.Object); controller.ControllerContext = mockedHttpContext.Object; var model = new AddImageViewModel(); var mockedFile = new MockHttpPostedFileBase(); mockedFile.SetContentLength(Constants.ImageMaxSize); // Act var result = controller.Add(mockedFile, model) as ViewResult; // Assert Assert.AreEqual( "Test", result.ViewData.ModelState[""].Errors.First().ErrorMessage); }