public async Task <ImageInputModel> SaveImageAsync(IFormFile formFile) { var isValid = new ImageAttribute().IsValid(formFile); if (formFile == null || !isValid) { return(null); } var guid = Guid.NewGuid().ToString(); var extension = await Image.DetectFormatAsync(formFile.OpenReadStream()); var source = $"/img/{guid}.{extension.Name}"; var filePath = $"{this.environment.ContentRootPath}/wwwroot{source}"; using var stream = new FileStream(filePath, FileMode.Create); await formFile.CopyToAsync(stream); var imageInputModel = new ImageInputModel { Source = source, }; return(imageInputModel); }
public async Task CreatePageHeaderAsyncShouldAddNewPageHeaderAndReturnExpectedId() { // Arrange var expectedId = 8; byte[] img = await File.ReadAllBytesAsync($@"C:\Users\nikolaviktor3132\Pictures\test.png"); var imgInputModel = new ImageInputModel { CloudUrl = "sdfsdfsdf", ContentType = "sadasd", CreatedOn = DateTime.Now, ImageByte = img, ImageTitle = "weqeqweq", ImagePath = "dsfsdfsdfs" }; var pageHeaderInputModel = new PageHeaderInputModel { Image = imgInputModel, SubTitle = "dfdfd", MainTitle = "dsds", }; // Act var actualPageHeader = this.pageHeadersRepository.AllWithDeleted().FirstOrDefault(ph => ph.Id == expectedId); Assert.True(actualPageHeader == null); await this.pageHeaderService.CreatePageHeaderAsync(pageHeaderInputModel); actualPageHeader = this.pageHeadersRepository.AllWithDeleted().FirstOrDefault(ph => ph.Id == expectedId); // Assert Assert.True(actualPageHeader != null); }
public async Task <ImageInputModel> SaveImageAsync(IFormFile formFile) { var isValid = new ImageAttribute().IsValid(formFile); if (formFile == null || !isValid) { return(null); } var guid = Guid.NewGuid().ToString(); var extension = await Image.DetectFormatAsync(formFile.OpenReadStream()); var fileName = $"{guid}.{extension.Name}"; var cloudinary = new Cloudinary(this.account); var uploadParams = new ImageUploadParams() { File = new FileDescription(fileName, formFile.OpenReadStream()), }; var uploadResult = await cloudinary.UploadAsync(uploadParams); var imageInput = new ImageInputModel() { Source = uploadResult.SecureUrl.AbsoluteUri, }; return(imageInput); }
public async Task TestDeleteImageAsync() { var projectInputModel = new ProjectInputModel { Name = "test", Description = "Lorem", HeadImage = this.file, }; var projectId = await this.projectsService.CreateProjectAsync(projectInputModel); var imageInputModel = new ImageInputModel { ProjectId = projectId, Description = "Lorem", ImageFile = this.file, }; var id = await this.projectsGalleryService.AddImageToGalleryAsync(imageInputModel); await this.projectsGalleryService.DeleteImageAsync(id); var currentGallery = await this.projectsGalleryService.GetGalleryAsync(projectId); Assert.Equal(0, currentGallery.Count); }
public async Task AddAsyncAddsTheEntityToTheRepository( string boulderName, string boulderDescription, string gradeId, string gymId, string imageSource, string authorId) { AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var repositoryMock = new Mock <IDeletableEntityRepository <Boulder> >(); var testData = new List <Boulder>(); var boulderInput = new BoulderInputModel() { Name = boulderName, Description = boulderDescription, GradeId = gradeId, GymId = gymId, }; var imageInput = new ImageInputModel() { Source = imageSource, }; var saved = false; repositoryMock.Setup(x => x.AddAsync(It.IsAny <Boulder>())) .Callback((Boulder boulder) => { testData.Add(boulder); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var bouldersService = new BouldersService(repositoryMock.Object, AutoMapperConfig.MapperInstance); // Act await bouldersService.AddAsync(boulderInput, authorId, imageInput); // Assert var boulder = testData[0]; Assert.True(saved); Assert.Equal(authorId, boulder.AuthorId); Assert.Equal(boulderName, boulder.Name); Assert.Equal(boulderDescription, boulder.Description); Assert.Equal(gradeId, boulder.GradeId); Assert.Equal(gymId, boulder.GymId); Assert.Equal(imageInput.Source, boulder.Image.Source); }
public async Task <IActionResult> Create(ImageInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.imagesService.AddAsync(input); return(this.RedirectToAction(nameof(this.AllPerSchool))); }
public async Task AddAsync(CityInputModel cityInput, ImageInputModel imageInput) { this.NullCheck(cityInput, nameof(cityInput)); var city = this.mapper.Map <City>(cityInput); city.Image = this.mapper.Map <Image>(imageInput); await this.citiesRepository.AddAsync(city); await this.citiesRepository.SaveChangesAsync(); }
public async Task AddAsync(CategoryInputModel categoryInput, ImageInputModel imageInput) { this.NullCheck(categoryInput, nameof(categoryInput)); var category = this.mapper.Map <Category>(categoryInput); category.Image = this.mapper.Map <Image>(imageInput); await this.categoriesRepository.AddAsync(category); await this.categoriesRepository.SaveChangesAsync(); }
public async Task <IActionResult> AddImage(ImageInputModel input) { if (!this.ModelState.IsValid || input.ImageFile == null) { this.ViewData["id"] = input.ProjectId; return(this.View()); } await this.projectsGalleryService.AddImageToGalleryAsync(input); return(this.RedirectToAction("Gallery", new { id = input.ProjectId })); }
public async Task AddAsync(GymInputModel gymInput, ImageInputModel imageInput) { this.NullCheck(gymInput, nameof(gymInput)); var country = this.mapper.Map <Gym>(gymInput); country.Image = this.mapper.Map <Image>(imageInput); await this.gymsRepository.AddAsync(country); await this.gymsRepository.SaveChangesAsync(); }
public IActionResult Create() { var viewModel = new ImageInputModel(); var userName = this.User.Identity.Name; var currentPrincipal = this.dbContext.Principals .FirstOrDefault(x => x.User.UserName == userName); var schoolId = currentPrincipal.NurserySchoolId; viewModel.GroupsItems = this.groupsService.GetAllAsKeyValuePairsPerSchool(schoolId); return(this.View(viewModel)); }
public async Task AddAsync(PostInputModel postInput, ImageInputModel imageInput, string userId) { this.NullCheck(postInput, nameof(postInput)); this.NullCheck(userId, nameof(userId)); var post = this.mapper.Map <Post>(postInput); post.Image = this.mapper.Map <Image>(imageInput); post.ApplicationUserId = userId; await this.postsRepository.AddAsync(post); await this.postsRepository.SaveChangesAsync(); }
public async Task AddAsync(BoulderInputModel boulderInput, string authorId, ImageInputModel imageInput) { this.NullCheck(boulderInput, nameof(boulderInput)); this.NullCheck(authorId, nameof(authorId)); this.NullCheck(imageInput, nameof(imageInput)); var image = this.mapper.Map <Image>(imageInput); var boulder = this.mapper.Map <Boulder>(boulderInput); boulder.AuthorId = authorId; boulder.Image = image; await this.bouldersRepository.AddAsync(boulder); await this.bouldersRepository.SaveChangesAsync(); }
public async Task AddUserImageAsyncShouldAddImageToUser() { // Arrange byte[] img = await File.ReadAllBytesAsync($@"C:\Users\nikolaviktor3132\Pictures\test.png"); var imgInputModel = new ImageInputModel { CloudUrl = "sdfsdfsdf", ContentType = "sadasd", CreatedOn = DateTime.Now, ImageByte = img, ImageTitle = "weqeqweq", ImagePath = "dsfsdfsdfs" }; // Act var result = await this.userService.AddUserImageAsync(imgInputModel, "*****@*****.**"); var actualUser = this.usersRepository.All().SingleOrDefault(u => u.UserName == "*****@*****.**"); // Assert Assert.True(actualUser.Image != null); }
public Models.PredictionDto GetPrediction(byte[] bytes) { var imageData = new ImageInputModel() { Image = bytes }; var predictionEngine = _predictionEnginePool.GetPredictionEngine(); var prediction = predictionEngine.Predict(imageData); var scores = GetSortedLabelValues(predictionEngine.OutputSchema, "Score", prediction.Score); var result = new Models.PredictionDto { CategoryScores = scores, PredictedLabel = prediction.PredictedLabel, }; return(result); }
public async Task AddAsync(ImageInputModel input) { var groupId = this.groupsRepository.AllAsNoTracking() .Where(x => x.Id == int.Parse(input.Group)) .Select(x => x.Id) .FirstOrDefault(); var image = new Image { Url = input.Url, Extension = input.Extension, NurseryGroupId = groupId, }; await this.imagesRepository.AddAsync(image); await this.imagesRepository.SaveChangesAsync(); }
public async Task <IActionResult> UploadPost([FromForm] ImageInputModel image) { var user = this.GetUserId(); var uploadResult = await mediator.Send(new UploadPostCommand(image.File, image.Description, user)); if (uploadResult.success) { return(Ok(new UploadResultModel() { ImageId = uploadResult.id, IsSuccess = true })); } return(BadRequest(new UploadResultModel() { IsSuccess = false, ErrorCode = uploadResult.errorCode })); }
public string UploudPicture(ImageInputModel image, string folderName) { UploadResult uploadResult; using (var ms = new MemoryStream(image.ImageByte)) { ImageUploadParams uploadParams = new ImageUploadParams { Folder = folderName, File = new FileDescription(image.ImageTitle, ms), PublicId = Guid.NewGuid().ToString(), Invalidate = true, }; uploadResult = this.cloudinaryUtility.Upload(uploadParams); } return(uploadResult?.SecureUri.AbsoluteUri); }
public async Task EditAsync(string id, CategoryInputModel categoryInput, ImageInputModel imageInput) { this.NullCheck(id, nameof(id)); this.NullCheck(categoryInput, nameof(categoryInput)); var category = this.categoriesRepository .All() .FirstOrDefault(x => x.Id == id); if (imageInput != null) { category.Image = this.mapper.Map <Image>(imageInput); } category.Name = categoryInput.Name; category.Description = categoryInput.Description; await this.categoriesRepository.SaveChangesAsync(); }
public async Task EditAsync(string id, PostInputModel postInput, ImageInputModel imageInput) { this.NullCheck(id, nameof(id)); this.NullCheck(postInput, nameof(postInput)); var post = this.postsRepository .All() .FirstOrDefault(x => x.Id == id); if (imageInput != null) { post.Image = this.mapper.Map <Image>(imageInput); } post.Title = postInput.Title; post.Text = postInput.Text; post.CategoryId = postInput.CategoryId; await this.postsRepository.SaveChangesAsync(); }
public async Task <bool> AddUserImageAsync(ImageInputModel imageInputModel, string username) { var currentUser = await this.users.All().SingleOrDefaultAsync(u => u.UserName == username); var cloudUrl = this.cloudinaryService.UploudPicture(imageInputModel, GlobalConstants.UsersFolderName); var imgForDb = new Image { ContentType = imageInputModel.ContentType, ImagePath = imageInputModel.ImagePath, ImageTitle = imageInputModel.ImageTitle, CloudUrl = cloudUrl, }; currentUser.Image = imgForDb; int addedImgs = await this.users.SaveChangesAsync(); return(addedImgs == 1); }
public async Task EditAsync(string id, GymInputModel gymInput, ImageInputModel imageInput) { this.NullCheck(id, nameof(id)); this.NullCheck(gymInput, nameof(gymInput)); var gym = this.gymsRepository .All() .FirstOrDefault(x => x.Id == id); if (imageInput != null) { gym.Image = this.mapper.Map <Image>(imageInput); } gym.Name = gymInput.Name; gym.Description = gymInput.Description; gym.CityId = gymInput.CityId; await this.gymsRepository.SaveChangesAsync(); }
public async Task <int> AddImageToGalleryAsync(ImageInputModel input) { var currentProject = await this.projectsService.GetProjectByIdAsync(input.ProjectId); var projectName = currentProject.Name; var imageId = await this.filesService.UploadToFileSystemAsync(input.ImageFile, "images\\projectImages\\" + projectName, "ImageTo" + projectName + "Gallery"); var newImageToProject = new GalleryProject { FileId = imageId, Description = input.Description, ProjectId = input.ProjectId, }; await this.dbImageToProject.AddAsync(newImageToProject); await this.dbImageToProject.SaveChangesAsync(); return(newImageToProject.Id); }
public async Task EditAsync(string id, BoulderInputModel boulderInput, ImageInputModel imageInput) { this.NullCheck(id, nameof(id)); this.NullCheck(boulderInput, nameof(boulderInput)); var boulder = this.bouldersRepository .All() .FirstOrDefault(x => x.Id == id); if (imageInput != null) { boulder.Image = this.mapper.Map <Image>(imageInput); } boulder.Name = boulderInput.Name; boulder.Description = boulderInput.Description; boulder.GradeId = boulderInput.GradeId; boulder.GymId = boulderInput.GymId; await this.bouldersRepository.SaveChangesAsync(); }
public async Task <IActionResult> MyPatients(ImageInputModel photo) { if (!this.ModelState.IsValid) { return(this.View()); } var photoName = Guid.NewGuid().ToString(); var photoUrl = await this.cloudinaryService.UploadImageAsync(photo.Image, photoName); var user = await this.userManager.GetUserAsync(this.HttpContext.User); var donorPatient = await this.donorsPatientsService.GetDonorsPatientsByDonorsUserIdAsync(user.Id); donorPatient.Patient.NeededBloodBanks--; donorPatient.ImageId = photoUrl.PublicId; await this.donorsPatientsService.AddImageAsync(donorPatient, photoUrl?.SecureUri.AbsoluteUri); return(this.Redirect("/")); }
public async Task TestAddImageToGallery() { var projectInputModel = new ProjectInputModel { Name = "test", Description = "Lorem", HeadImage = this.file, }; var projectId = await this.projectsService.CreateProjectAsync(projectInputModel); var imageInputModel = new ImageInputModel { ProjectId = projectId, Description = "Lorem", ImageFile = this.file, }; var id = await this.projectsGalleryService.AddImageToGalleryAsync(imageInputModel); Assert.NotEqual(0, id); }
public async Task EditAsyncEditsThePorpertiesAndSavesTheChanges( string name, string description, string gradeId, string gymId, string newName, string newDescription, string newGradeId, string newGymId, bool imageNull, string imageSource, string newImageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = true; var boulder = new Boulder() { Name = name, Description = description, GradeId = gradeId, GymId = gymId, Image = new Image() { Source = imageSource, }, }; var bouldersList = new List <Boulder>() { new Boulder(), new Boulder(), new Boulder(), boulder, new Boulder(), new Boulder(), }; var repositoryMock = new Mock <IDeletableEntityRepository <Boulder> >(); repositoryMock.Setup(x => x.All()) .Returns(bouldersList.AsQueryable()); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var boulderEditModel = new BoulderInputModel() { Name = newName, Description = newDescription, GymId = newGymId, GradeId = newGradeId, }; var imageEditModel = new ImageInputModel() { Source = newImageSource, }; if (imageNull) { imageEditModel = null; } var bouldersService = new BouldersService(repositoryMock.Object, AutoMapperConfig.MapperInstance); // Act await bouldersService.EditAsync(boulder.Id, boulderEditModel, imageEditModel); // Assert Assert.True(saved); Assert.Equal(newName, boulder.Name); Assert.Equal(newDescription, boulder.Description); Assert.Equal(newGradeId, boulder.GradeId); Assert.Equal(newGymId, boulder.GymId); var actualImage = boulder.Image; if (imageNull) { Assert.Equal(imageSource, actualImage.Source); } else { Assert.Equal(newImageSource, actualImage.Source); } }
public async Task AddMapsTheInputModelsSetsUserIdAndAddsToTheRepository( string categoryId, string text, string title, bool nullImage, string imageSource, string userId) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var postsList = new List <Post>(); var repositoryMock = new Mock <IDeletableEntityRepository <Post> >(); repositoryMock.Setup(x => x.AddAsync(It.IsAny <Post>())) .Callback((Post post) => { postsList.Add(post); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var postsService = new PostsService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var postInputModel = new PostInputModel() { CategoryId = categoryId, Text = text, Title = title, }; var imageInputModel = new ImageInputModel() { Source = imageSource, }; if (nullImage) { imageInputModel = null; } // Act await postsService.AddAsync(postInputModel, imageInputModel, userId); // Assert var actualPost = postsList[0]; Assert.True(saved); Assert.Equal(categoryId, actualPost.CategoryId); Assert.Equal(text, actualPost.Text); Assert.Equal(title, actualPost.Title); Assert.Equal(userId, actualPost.ApplicationUserId); var actualImage = actualPost.Image; if (nullImage) { Assert.Null(actualImage); } else { Assert.Equal(imageSource, actualImage.Source); } }
public async Task EditAsyncEditsThePropertiesAndSavesTheChanges( string userId, string title, string text, string categoryId, string newTitle, string newText, string newCategoryId, bool imageNull, string imageSource, string newImageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var post = new Post() { ApplicationUserId = userId, Title = title, Text = text, CategoryId = categoryId, Image = new Image() { Source = imageSource, } }; var postsList = new List <Post>() { new Post(), post, new Post(), new Post(), new Post(), }; var repositoryMock = new Mock <IDeletableEntityRepository <Post> >(); repositoryMock.Setup(x => x.All()) .Returns(postsList.AsQueryable()); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var postsService = new PostsService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var postEditModel = new PostInputModel() { Title = newTitle, Text = newText, CategoryId = newCategoryId, }; var imageEditModel = new ImageInputModel() { Source = newImageSource, }; if (imageNull) { imageEditModel = null; } // Act await postsService.EditAsync(post.Id, postEditModel, imageEditModel); // Assert Assert.True(saved); Assert.Equal(userId, post.ApplicationUserId); Assert.Equal(newTitle, post.Title); Assert.Equal(newText, post.Text); Assert.Equal(newCategoryId, post.CategoryId); var actualImage = post.Image; if (imageNull) { Assert.Equal(imageSource, actualImage.Source); } else { Assert.Equal(newImageSource, actualImage.Source); } }
public async Task AddAsyncMapsTheInputModelAndAddsItToTheRepository( string name, string description, bool nullImage, string imageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = false; var categoriesList = new List <Category>(); var repositoryMock = new Mock <IDeletableEntityRepository <Category> >(); repositoryMock.Setup(x => x.AddAsync(It.IsAny <Category>())) .Callback((Category category) => { categoriesList.Add(category); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var categoriesService = new CategoriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var categoryInput = new CategoryInputModel() { Name = name, Description = description, }; var imageInput = new ImageInputModel() { Source = imageSource, }; if (nullImage) { imageInput = null; } // Act await categoriesService.AddAsync(categoryInput, imageInput); // Assert var actualCategory = categoriesList[0]; Assert.True(saved); Assert.Equal(name, actualCategory.Name); Assert.Equal(description, actualCategory.Description); var actualImage = actualCategory.Image; if (nullImage) { Assert.Null(actualCategory.Image); } else { Assert.Equal(imageSource, actualImage.Source); } }