Beispiel #1
0
        public async Task AddAsync(AddRigInputModel input, string userId)
        {
            var rig = new Rig
            {
                Name        = input.Name,
                Description = input.Description,
                OwnerId     = userId,
            };

            var rigImgUrl = await this.cloudinaryService.UploadAsync(input.Image, input.Image.FileName);

            rig.Image = new Image
            {
                OwnerId = userId,
                Url     = rigImgUrl,
            };

            for (int i = 0; i < input.Materials.Count(); i++)
            {
                rig.Materials.Add(new Material
                {
                    Rig            = rig,
                    Description    = input.Materials[i].Description,
                    MaterialNumber = i + 1,
                });
            }

            for (int i = 0; i < input.Steps.Count(); i++)
            {
                var stepImgUrl = await this.cloudinaryService.UploadAsync(input.Steps[i].Image, input.Steps[i].Image.FileName);

                rig.Steps.Add(new Step
                {
                    Rig         = rig,
                    Description = input.Steps[i].Description,
                    StepNumber  = i + 1,
                    Image       = new Image
                    {
                        OwnerId = userId,
                        Url     = stepImgUrl,
                    },
                });
            }

            await this.rigsRepository.AddAsync(rig);

            await this.rigsRepository.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task <IActionResult> Add(AddRigInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            try
            {
                await this.rigsService.AddAsync(input, userId);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            return(this.RedirectToAction(nameof(this.All)));
        }
Beispiel #3
0
        public IActionResult Add()
        {
            var viewModel = new AddRigInputModel();

            return(this.View(viewModel));
        }
Beispiel #4
0
        public async Task AddAsyncShouldWorkCorrect()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            using var db = new ApplicationDbContext(options);
            var rigsRepository = new EfDeletableEntityRepository <Rig>(db);

            var clodinaryMock = new Mock <ICloudinaryService>();

            var service = new RigsService(clodinaryMock.Object, rigsRepository);

            // Arrange
            var fileMock = new Mock <IFormFile>();

            // Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.jpg";
            var ms       = new MemoryStream();

            using var writer = new StreamWriter(ms);
            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            var file = fileMock.Object;

            var input = new AddRigInputModel
            {
                Name        = "Test Rig",
                Description = "This is a test rig",
                Image       = file,
                Steps       = new List <AddStepInputModel>
                {
                    new AddStepInputModel
                    {
                        Description = "test step 1",
                        Image       = file,
                    },
                    new AddStepInputModel
                    {
                        Description = "test step 2",
                        Image       = file,
                    },
                },
                Materials = new List <AddMaterialInputModel>
                {
                    new AddMaterialInputModel {
                        Description = "test material 1"
                    },
                    new AddMaterialInputModel {
                        Description = "test material 2"
                    },
                },
            };

            Assert.Equal(0, service.GetCount());

            await service.AddAsync(input, Guid.NewGuid().ToString());

            await rigsRepository.SaveChangesAsync();

            Assert.Equal(1, service.GetCount());
        }