示例#1
0
        public IActionResult Create()
        {
            var userid    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var profileId = this.profilesService.GetId(userid);

            var viewModel = new CreatePageInputModel();

            viewModel.ReturnId = profileId;

            return(this.View(viewModel));
        }
示例#2
0
        public async Task CreateAsyncWorksCorrectly()
        {
            var input = new CreatePageInputModel
            {
                Name  = "test",
                Tags  = null,
                Image = null,
            };

            await this.pagesService.CreateAsync(1, input, "test");

            Assert.Equal(1, await this.pagesRepository.All().CountAsync());
        }
示例#3
0
        public async Task <IActionResult> Create(CreatePageInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userid    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var profileId = this.profilesService.GetId(userid);

            var pageId = await this.pagesService.CreateAsync(profileId, input, $"{this.webHost.WebRootPath}/img/pages");

            return(this.RedirectToAction(nameof(this.ById), new { id = pageId }));
        }
示例#4
0
        public async Task CreateAsyncWorksCorrectlyWithImageAndTags()
        {
            var imageMock = new Mock <IFormFile>();

            imageMock.Setup(x => x.Length).Returns(1);
            var input = new CreatePageInputModel
            {
                Name = "test",
                Tags = new List <string>()
                {
                    "tag1", "tag2"
                },
                Image = imageMock.Object,
            };

            await this.pagesService.CreateAsync(1, input, "test");

            Assert.Equal(1, await this.pagesRepository.All().CountAsync());
        }
示例#5
0
        public ActionResult Create(CreatePageInputModel pageInputModel)
        {
            if (ModelState.IsValid)
            {
                var page = new Page
                {
                    Title = pageInputModel.Title,
                    Content = pageInputModel.Content,
                    Permalink = this.urlGenerator.GenerateUrl(pageInputModel.Title),
                    Author = this.UserProfile,
                    AuthorId = this.UserProfile.Id,
                };

                this.Data.Pages.Add(page);
                this.Data.SaveChanges();

                return this.RedirectToAction("Index");
            }

            return this.View(pageInputModel);
        }
示例#6
0
        public async Task <int> CreateAsync(int profileId, CreatePageInputModel input, string path)
        {
            var page = new Page()
            {
                Name        = input.Name,
                Description = input.Description,
                Email       = input.Email,
                Phone       = input.Phone,
                ProfileId   = profileId,
            };

            if (input.Image?.Length > 0)
            {
                page.ImageId = await this.imagesService.CreateAsync(input.Image, path);
            }

            await this.pagesRepository.AddAsync(page);

            await this.pagesRepository.SaveChangesAsync();

            if (input.Tags?.Count() > 0)
            {
                foreach (var tag in input.Tags.Distinct())
                {
                    var tagId = await this.tagsService.GetIdAsync(tag);

                    var pageTag = new PageTag()
                    {
                        TagId  = tagId,
                        PageId = page.Id,
                    };

                    await this.pageTagsRepository.AddAsync(pageTag);
                }

                await this.pageTagsRepository.SaveChangesAsync();
            }

            return(page.Id);
        }
示例#7
0
        public async Task CreatePostShouldWork()
        {
            // Arrange
            var mockCitiesService            = new Mock <ICitiesService>();
            var mockMakesService             = new Mock <IMakesService>();
            var mockCategoriesService        = new Mock <ICategoriesService>();
            var mockModelsService            = new Mock <IModelsService>();
            var mockVehicleCategoriesService = new Mock <IVehicleCategoriesService>();
            var mockColorsService            = new Mock <IColorService>();
            var mockCloudinaryService        = new Mock <ICloudinaryService>();
            var mockCommentsService          = new Mock <ICommentsService>();
            var mockPostsService             = new Mock <IPostsService>();

            var mockUserManager = this.GetMockUserManager();

            mockCategoriesService
            .Setup(mc => mc.GetCategoryByName("Cars and jeeps"))
            .Returns(new Category()
            {
                Id = 1, Name = "Cars and jeeps"
            });

            mockCitiesService
            .Setup(mc => mc.GetCityByName("Sofia"))
            .Returns(new City()
            {
                Id = 1, Name = "Sofia"
            });

            mockColorsService
            .Setup(mc => mc.GetColorByName("Blue"))
            .Returns(new Color()
            {
                Id = 1, Name = "Blue"
            });

            mockMakesService
            .Setup(mc => mc.GetMakeById(11))
            .Returns(new Make()
            {
                Id = 11, Name = "BMW"
            });

            mockModelsService
            .Setup(mc => mc.GetModelByName("M5"))
            .Returns(new Model()
            {
                Id = 1, Name = "M5"
            });

            mockVehicleCategoriesService
            .Setup(mc => mc.GetVehicleCategoryByName("Sedan"))
            .Returns(new VehicleCategory()
            {
                Id = 1, Name = "Sedan"
            });

            var files = new List <IFormFile>();

            using (var stream = File.OpenRead("../../../Files/cat.jpg"))
            {
                FormFile file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
                {
                    Headers     = new HeaderDictionary(),
                    ContentType = "image/jpeg",
                };

                files.Add(file);
            }

            mockCloudinaryService
            .Setup(mc => mc.UploadAsync(files))
            .Returns(Task.FromResult(this.GetAll <Image>()));

            var claims = new List <Claim>()
            {
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "5247d66a-84ff-4987-abb5-53b1c2e747c2"),
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "*****@*****.**"),
                new Claim("AspNet.Identity.SecurityStamp", "E7B2QZV5M4OIRM3ZFIVXFVGR3YULFGO7"),
                new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "Admin"),
                new Claim("amr", "pwd"),
            };

            var claimsIdentity = new ClaimsIdentity(claims);

            var principal = new ClaimsPrincipal(claimsIdentity);

            Thread.CurrentPrincipal = principal;

            mockUserManager.Setup(mu => mu.GetUserAsync(principal))
            .Returns(Task.FromResult(new ApplicationUser()
            {
                UserName = "******", Image = new Image()
                {
                    Url = "testUrl"
                }
            }));

            // Act
            var controller = new PostsController(
                mockPostsService.Object,
                mockCitiesService.Object,
                mockCategoriesService.Object,
                mockMakesService.Object,
                mockModelsService.Object,
                mockVehicleCategoriesService.Object,
                mockColorsService.Object,
                mockUserManager.Object,
                mockCloudinaryService.Object,
                mockCommentsService.Object);

            // Assert
            var inputModel = new CreatePageInputModel()
            {
                PostCategory     = "Cars and jeeps",
                Town             = "Sofia",
                Color            = "Blue",
                Make             = 11,
                Model            = "M5",
                VehicleCategory  = "Sedan",
                Condition        = "Used",
                Currency         = "LV",
                EngineType       = "Disel",
                TransmissionType = "Automatic",
                Eurostandard     = 5,
                Description      = "some random description",
                Horsepower       = 155,
                Email            = "*****@*****.**",
                Year             = 2020,
                Mileage          = 152456,
                Modification     = "F10",
                Price            = 52000,
                PhoneNumber      = "0897132123",
                Files            = files,
            };

            // Act
            var result = await controller.Create(inputModel);

            // Assert
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Posts", redirectToActionResult.ControllerName);
            Assert.Equal("Details", redirectToActionResult.ActionName);
        }
        public async Task <IActionResult> Create(CreatePageInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Create());
            }

            var category        = this.categoriesService.GetCategoryByName(inputModel.PostCategory);
            var city            = this.citiesService.GetCityByName(inputModel.Town);
            var color           = this.colorService.GetColorByName(inputModel.Color);
            var make            = this.makesService.GetMakeById(inputModel.Make.Value);
            var model           = this.modelsService.GetModelByName(inputModel.Model);
            var vehicleCategory = this.vehicleCategoriesService.GetVehicleCategoryByName(inputModel.VehicleCategory);

            var userChecker = this.User ?? (ClaimsPrincipal)Thread.CurrentPrincipal;
            var user        = await this.userManager.GetUserAsync(userChecker);

            var condition        = (Condition)Enum.Parse(typeof(Condition), inputModel.Condition, true);
            var currency         = (Currency)Enum.Parse(typeof(Currency), inputModel.Currency, true);
            var engineType       = (EngineType)Enum.Parse(typeof(EngineType), inputModel.EngineType, true);
            var transmissionType = (TransmissionType)Enum.Parse(typeof(TransmissionType), inputModel.TransmissionType, true);
            var eurostandard     = (Eurostandard)inputModel.Eurostandard;

            var post = new Post()
            {
                Category          = category,
                CategoryId        = category.Id,
                City              = city,
                Make              = make,
                MakeId            = make.Id,
                Model             = model,
                ModelId           = model.Id,
                CityId            = city.Id,
                Color             = color,
                ColorId           = color.Id,
                Condition         = condition,
                Currency          = currency,
                EngineType        = engineType,
                Description       = inputModel.Description,
                Horsepower        = inputModel.Horsepower.Value,
                Email             = inputModel.Email,
                Eurostandard      = eurostandard,
                TransmissionType  = transmissionType,
                ManufactureDate   = new DateTime(inputModel.Year.Value, 1, 1),
                Mileage           = inputModel.Mileage.Value,
                Name              = make.Name + " " + model.Name + " " + inputModel.Modification,
                Price             = inputModel.Price.Value,
                VehicleCategory   = vehicleCategory,
                VehicleCategoryId = vehicleCategory.Id,
                PhoneNumber       = inputModel.PhoneNumber,
                User              = user,
                UserId            = user.Id,
            };

            await this.postsService.AddAsync(post);

            var images = await this.cloudinaryService.UploadAsync(inputModel.Files);

            foreach (var image in images)
            {
                var postImage = new PostImage()
                {
                    Image   = image,
                    ImageId = image.Id,
                    Post    = post,
                    PostId  = post.Id,
                };

                await this.postsService.AddImageToPost(post.Id, postImage);
            }

            return(this.RedirectToAction("Details", "Posts", new { id = post.Id }));
        }