private async Task SaveImages(IFormCollection formData, NewSampleBidingModel newSample)
        {
            foreach (var image in formData.Files)
            {
                var isImage = CheckIfFileIsAnImage(image);
                if (!isImage)
                {
                    logger.LogError("the file is not an image! on create new sample.");
                    throw new Exception("Allowed image format is image/jpg(jpeg).");
                }

                try
                {
                    string path       = Path.Combine(this.env.ContentRootPath + "\\wwwroot\\Images");
                    var    newImgName = Guid.NewGuid().ToString() + (image.FileName.Substring(image.FileName.LastIndexOf('.')));
                    using (var img = new FileStream(Path.Combine(path, newImgName), FileMode.Create))
                    {
                        await image.CopyToAsync(img);

                        newSample.ImgUrls.Add(newImgName);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Exemple #2
0
        public void CreateNewSample_CalledWithRelevantData_ShouldCreateSample()
        {
            // Arrange
            var db = this.GetDatabase();

            var newSampleData = new NewSampleBidingModel()
            {
                Name               = "New Sample",
                Description        = "Test Test Test",
                Tags               = "tag1",
                Groups             = "group1",
                NutrientAgarPlates = "nutrient1",
                ImgUrls            = new List <string>()
                {
                    "some-image.jpg",
                    "another-image.jpg",
                    "test-image.jpg"
                }
            };

            User logedInUser = new User()
            {
                Name  = "Test User",
                Email = "*****@*****.**"
            };

            var tag1 = new Tag()
            {
                Name = "tag1"
            };
            var group = new Group()
            {
                Name = "group1"
            };
            var nutrint = new NutrientAgarPlate()
            {
                Name = "nutrient1"
            };

            db.Tags.AddRange(tag1);
            db.Groups.Add(group);
            db.NutrientAgarPlates.Add(nutrint);
            db.SaveChanges();

            var service = new SampleService(db, null);

            // Act
            service.CreateNewSample(newSampleData, logedInUser);
            var cretedNewSample = db.Samples.First();

            // Assert
            Assert.NotNull(cretedNewSample);
            Assert.IsType <Sample>(cretedNewSample);
            var sampleName = cretedNewSample.Name;

            Assert.Equal("New Sample", sampleName);
        }
        public async Task <IActionResult> CreateNewSample(
            [FromForm] string name,
            [FromForm] string description,
            [FromForm] string groups,
            [FromForm] string tags,
            [FromForm] string nutrientAgarPlates,
            [FromForm] IFormCollection formData)
        {
            if (name == null &&
                description == null &&
                tags == null &&
                groups == null &&
                formData == null &&
                nutrientAgarPlates == null)
            {
                logger.LogError("description, tags or ... are not provided to create to sample");
                return(BadRequest("Please field up at least one image! All fields are requered!"));
            }

            var user = this.accountService
                       .RetrieveCurrentUser(this.Request.Headers["Authorization"]);

            var newSample = new NewSampleBidingModel()
            {
                Name               = name,
                Description        = description,
                Groups             = groups,
                Tags               = tags,
                NutrientAgarPlates = nutrientAgarPlates,
                ImgUrls            = new List <string>()
            };

            try
            {
                await SaveImages(formData, newSample);
            }
            catch (Exception ex)
            {
                logger.LogError($"error on save image when try to create new sample with name:{name}");
                return(BadRequest(ex));
            }

            try
            {
                this.service.CreateNewSample(newSample, user);
            }
            catch (Exception ex)
            {
                logger.LogError($"error on create new sample with name:{name}");
                return(BadRequest(ex));
            }

            return(Ok());
        }
        public void CreateNewSample(NewSampleBidingModel newSampleData, User currentUser)
        {
            List <SampleGroup>             sampleGroups             = CrateSampleGroupsByGroupName(newSampleData.Groups.Split(','));
            List <SampleTag>               sampleTags               = CreateSampleTagsByTagName(newSampleData.Tags.Split(','));
            List <SampleImage>             newImgs                  = CreateNewImagesByGivenUrls(newSampleData.ImgUrls);
            List <SampleNutrientAgarPlate> sampleNutrientAgarPlates =
                CreateNewSampleNutrientAgarPlatesByNutrientAgarPlates(newSampleData.NutrientAgarPlates.Split(','));

            Sample newSample = new Sample()
            {
                Name                     = newSampleData.Name,
                Description              = newSampleData.Description,
                CreatedOn                = DateTime.Now,
                CreatedBy                = $"{currentUser.Name}({currentUser.Email})",
                SampleGroups             = sampleGroups,
                SampleTags               = sampleTags,
                SampleNutrientAgarPlates = sampleNutrientAgarPlates,
                Images                   = newImgs
            };

            this.Context.Samples.Add(newSample);
            this.Context.SaveChanges();
        }