public async Task <IActionResult> Create([FromBody] EnglishVideoCreateViewModel englishVideoCreateViewModel)
        {
            var englishVideoCreateModel = _mapper.Map <EnglishVideoCreateModel>(englishVideoCreateViewModel);

            await _videoService.CreateAsync(englishVideoCreateModel);

            return(Ok());
        }
コード例 #2
0
        internal async Task GivenCreateAsyncWhenExpectedExceptionIsThrownThenHandlesGracefully()
        {
            // Arrange

            // Act
            var exception = await Assert.ThrowsAsync <NotImplementedException>(
                () => videoService.CreateAsync(It.IsAny <Video>()));

            // Assert
            exception.Should().NotBeNull().And.BeOfType <NotImplementedException>();
        }
コード例 #3
0
        public async Task <IActionResult> Create([FromForm] VideoDetail model)
        {
            var result = await _videoService.CreateAsync(model);

            if (result.IsSuccess)
            {
                return(Ok(result));
            }

            return(BadRequest(result));
        }
コード例 #4
0
        public void GivenCreateAsyncWhenExpectedExceptionIsThrownThenHandlesGracefully()
        {
            // Arrange

            // Act
            var exception = Assert.ThrowsAsync <NotImplementedException>(
                () => videoService.CreateAsync(It.IsAny <Video>()));

            // Assert
            Assert.That(exception, Is.Not.Null);
            Assert.That(exception, Is.TypeOf <NotImplementedException>());
        }
コード例 #5
0
        public void GivenCreateAsyncWhenExpectedExceptionIsThrownThenHandlesGracefully()
        {
            // Arrange

            // Act
            var exception = Assert.ThrowsAsync <NotImplementedException>(
                () => videoService.CreateAsync(It.IsAny <Video>()));

            // Assert
            exception.ShouldNotBeNull();
            exception.ShouldBeOfType <NotImplementedException>();
        }
コード例 #6
0
        public async Task <IActionResult> CreateAsync([FromBody] Video video)
        {
            try
            {
                await videoService.CreateAsync(video);

                return(Created($"api/v1/videos/{ video?.Id }", video));
            }
            catch
            {
                return(BadRequest());
            }
        }
コード例 #7
0
ファイル: VideoController.cs プロジェクト: SofiaXu/VOTServer
        public async Task <JsonResponse> CreateVideoInformation([FromForm] CreateVideoInformationRequest request)
        {
            _ = ModelState.IsValid;
            await service.CreateAsync(new VideoTagViewModel
            {
                Info       = request.Info,
                UploaderId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                Title      = request.Title,
                Tags       = request.Tags.Select(x => new TagViewModel {
                    Id = x
                })
            });

            return(new JsonResponse {
                StatusCode = 200, Message = "OK"
            });
        }
コード例 #8
0
        public async Task Create_ShouldAddVideoAndReturnVideoId()
        {
            var video = new Video
            {
                Id           = "51",
                Title        = "title4e",
                ThumbnailUrl = "url.com",
                Views        = 14,
                AuthorId     = "1",
                CategoryId   = "1"
            };
            var videoId = await videoService.CreateAsync(video);

            Assert.NotNull(videoId);
            Assert.AreEqual(video.Id, videoId);
            Assert.AreSame(video, videoData.Last());
        }
コード例 #9
0
        public async Task <IActionResult> Upload([FromForm] VideoUploadBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetFirstError()));
            }

            var video    = Mapper.Map <Video>(model);
            var category = await categoryService.GetCategoryByNameAsync(model.CategoryName);

            if (category == null)
            {
                return(BadRequest());
            }
            video.CategoryId = category.Id;
            video.AuthorId   = User.GetId();

            var videoId = await videoService.CreateAsync(video);

            return(Json(videoId));
        }
コード例 #10
0
ファイル: PostService.cs プロジェクト: mdockal/Hikkaba
        public async Task <TPrimaryKey> CreateAsync(IFormFileCollection attachments, PostDto dto)
        {
            var isPostingAllowed = await _banService.IsPostingAllowedAsync(dto.ThreadId, dto.UserIpAddress);

            if (!isPostingAllowed.Item1)
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden, isPostingAllowed.Item2);
            }
            else if (attachments == null)
            {
                return(await CreateAsync(dto, (post) =>
                {
                    post.Thread = Context.Threads.FirstOrDefault(thread => thread.Id == dto.ThreadId);
                }));
            }
            else
            {
                var postId = await CreateAsync(dto, (post) =>
                {
                    post.Thread = Context.Threads.FirstOrDefault(thread => thread.Id == dto.ThreadId);
                });

                try
                {
                    var containerName = dto.ThreadId.ToString();
                    if (string.IsNullOrWhiteSpace(containerName))
                    {
                        throw new Exception($"{nameof(containerName)} is null or whitespace");
                    }

                    foreach (var attachment in attachments)
                    {
                        string blobName;

                        var attachmentParentDto = _attachmentCategorizer.CreateAttachmentDto(attachment.FileName);
                        attachmentParentDto.PostId        = postId;
                        attachmentParentDto.FileExtension = Path.GetExtension(attachment.FileName)?.TrimStart('.');
                        attachmentParentDto.FileName      = Path.GetFileNameWithoutExtension(attachment.FileName);
                        attachmentParentDto.Size          = attachment.Length;
                        attachmentParentDto.Hash          = _cryptoService.HashHex(attachment.OpenReadStream());

                        if (attachmentParentDto is AudioDto audioDto)
                        {
                            blobName = (await _audioService.CreateAsync(audioDto, entity =>
                            {
                                entity.Post = Context.Posts.FirstOrDefault(post => post.Id == postId);
                            })).ToString();
                        }
                        else if (attachmentParentDto is PictureDto pictureDto)
                        {
                            if (_attachmentCategorizer.IsPictureExtensionSupported(pictureDto.FileExtension))
                            {
                                // generate thumbnail if such file extension is supported
                                var image = Image.Load(attachment.OpenReadStream());
                                pictureDto.Width  = image.Width;
                                pictureDto.Height = image.Height;
                                blobName          = (await _pictureService.CreateAsync(pictureDto, entity =>
                                {
                                    entity.Post = Context.Posts.FirstOrDefault(post => post.Id == postId);
                                })).ToString();

                                var thumbnail = _thumbnailGenerator.GenerateThumbnail(
                                    image,
                                    _hikkabaConfiguration.ThumbnailsMaxWidth,
                                    _hikkabaConfiguration.ThumbnailsMaxHeight);
                                await _storageProvider.SaveBlobStreamAsync(
                                    containerName + Defaults.ThumbnailPostfix,
                                    blobName,
                                    thumbnail.Image);
                            }
                            else
                            {
                                // otherwise save the same image as thumbnail
                                blobName = (await _pictureService.CreateAsync(pictureDto, entity =>
                                {
                                    entity.Post = Context.Posts.FirstOrDefault(post => post.Id == postId);
                                })).ToString();
                                await _storageProvider.SaveBlobStreamAsync(
                                    containerName + Defaults.ThumbnailPostfix,
                                    blobName,
                                    attachment.OpenReadStream());
                            }
                        }
                        else if (attachmentParentDto is VideoDto videoDto)
                        {
                            blobName = (await _videoService.CreateAsync(videoDto, entity =>
                            {
                                entity.Post = Context.Posts.FirstOrDefault(post => post.Id == postId);
                            })).ToString();
                        }
                        else if (attachmentParentDto is DocumentDto documentDto)
                        {
                            blobName = (await _documentService.CreateAsync(documentDto, entity =>
                            {
                                entity.Post = Context.Posts.FirstOrDefault(post => post.Id == postId);
                            })).ToString();
                        }
                        else
                        {
                            throw new Exception($"Unknown attachment type: {attachmentParentDto.GetType().Name}");
                        }
                        if (string.IsNullOrWhiteSpace(blobName))
                        {
                            throw new Exception($"{nameof(blobName)} is null or whitespace");
                        }
                        await _storageProvider.SaveBlobStreamAsync(containerName, blobName, attachment.OpenReadStream());
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex + $" Post {postId} will be deleted.");
                    await DeleteAsync(postId);

                    throw;
                }

                return(postId);
            }
        }