コード例 #1
0
        public async Task <IActionResult> CreatePostAsync(Guid userId, [FromForm] CreatePost post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var    tags           = JsonConvert.DeserializeObject <List <SelectTag> >(post.Tags);
            string coverImagePath = null;

            if (post.CoverImage != null)
            {
                var uniqueFileName = GetUniqueFileName(post.CoverImage.FileName);
                var uploads        = Path.Combine(_hostingEnvironment.ContentRootPath, "Storage/Uploads");
                var filePath       = Path.Combine(uploads, uniqueFileName);
                await post.CoverImage.CopyToAsync(new FileStream(filePath, FileMode.Create));

                coverImagePath = uniqueFileName;
            }
            var createdPost = await _repo.CreatePostAsync(
                new Post
            {
                Title          = post.Title,
                Body           = post.Body,
                UserId         = userId,
                CoverImagePath = coverImagePath
            },
                tags
                );

            return(Created(nameof(Route.PostsCreate), createdPost));
        }
コード例 #2
0
        public async Task <IActionResult> Create([FromBody] CreatePostRequest postRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var post = _mapper.Map <Post>(postRequest);

            post.UserId = HttpContext.GetUserId();

            var created = await _postRepository.CreatePostAsync(post);

            if (created)
            {
                var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
                var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", post.Id.ToString());

                return(Created(locationUri, _mapper.Map <PostResponse>(post)));
            }

            return(NotFound());
        }
コード例 #3
0
        public async Task Seed()
        {
            // Clear database
            await EntityFrameworkTestsHelper.ClearAndMarkTestDatabase(_dbContext);

            // Setup users
            var cloudyLogin = new UserLoginInfo("e2e", "cloudy_dude");
            var cloudy      = await _usersRepository.LoadOrCreateUserFromLoginAsync(cloudyLogin.Scheme, cloudyLogin.NameIdentifier, "Cloudy Dude", new DateTimeOffset(2018, 01, 15, 15, 23, 12, TimeSpan.Zero));

            var billyLogin = new UserLoginInfo("e2e", "H4x0rs4evaaaah");
            var billy      = await _usersRepository.LoadOrCreateUserFromLoginAsync(billyLogin.Scheme, billyLogin.NameIdentifier, "Billy", new DateTimeOffset(2000, 01, 01, 00, 00, 00, TimeSpan.Zero));

            var edLogin = new UserLoginInfo("e2e", "153234");
            var ed      = await _usersRepository.LoadOrCreateUserFromLoginAsync(edLogin.Scheme, edLogin.NameIdentifier, "ed", new DateTimeOffset(2018, 03, 14, 19, 54, 48, TimeSpan.Zero));

            // Setup posts
            var gettingReadyToJumpJpg = await CreateFile(new PostFile
            {
                Filename   = "getting_ready_to_jump.jpg",
                MimeType   = "image/jpeg",
                Compressed = false,
            });

            var gettingReadyToJumpJson = await CreateFile(new PostFile
            {
                Filename   = "getting_ready_to_jump.json",
                MimeType   = "application/json",
                Compressed = true,
            });

            var gettingReadyToJumpWav = await CreateFile(new PostFile
            {
                Filename   = "getting_ready_to_jump.wav",
                MimeType   = "audio/wav",
                Compressed = true,
            });

            var gettingReadyToJumpPost = await _postsRepository.CreatePostAsync(
                cloudyLogin,
                "Jumping Lady",
                new[] { "jumping", "lady" },
                new[]
            {
                new Scene
                {
                    Name         = "getting_ready_to_jump",
                    ThumbnailUrn = gettingReadyToJumpJpg.Urn
                }
            },
                new[]
            {
                gettingReadyToJumpJson, gettingReadyToJumpJpg, gettingReadyToJumpWav
            },
                gettingReadyToJumpJpg.Urn,
                new DateTimeOffset(2018, 01, 16, 21, 02, 54, TimeSpan.Zero)
                );

            var gettingReadyToJumpPostViewModel = PostViewModel.From(gettingReadyToJumpPost, false);

            gettingReadyToJumpPostViewModel.Published = true;
            await _postsRepository.UpdatePostAsync(cloudyLogin, gettingReadyToJumpPostViewModel, new DateTimeOffset(2018, 01, 16, 21, 03, 49, TimeSpan.Zero));

            // Setup comments
            await _commentsRepository.CreatePostCommentAsync(billyLogin, gettingReadyToJumpPost.Id, "This scene rocks man", new DateTimeOffset(2018, 01, 16, 21, 02, 54, TimeSpan.Zero));

            await _commentsRepository.CreatePostCommentAsync(cloudyLogin, gettingReadyToJumpPost.Id, "Thanks!", new DateTimeOffset(2018, 01, 16, 21, 04, 24, TimeSpan.Zero));

            await _commentsRepository.CreatePostCommentAsync(edLogin, gettingReadyToJumpPost.Id, "She's not even *jumping*, _yo_.", new DateTimeOffset(2018, 01, 16, 21, 08, 07, TimeSpan.Zero));

            await _commentsRepository.CreatePostCommentAsync(billyLogin, gettingReadyToJumpPost.Id, "Get\na\nlife", new DateTimeOffset(2018, 01, 16, 21, 12, 35, TimeSpan.Zero));

            // Setup votes
            await _votesRepository.VoteAsync(cloudyLogin, gettingReadyToJumpPost.Id, 10);

            await _votesRepository.VoteAsync(billyLogin, gettingReadyToJumpPost.Id, 10);

            await _votesRepository.VoteAsync(edLogin, gettingReadyToJumpPost.Id, -2);
        }
コード例 #4
0
 public async Task <Option <Post> > CreatePostAsync(Post postToCreate)
 {
     return(await postRepository.CreatePostAsync(postToCreate));
 }