public async Task <IActionResult> AddLostDogComment([ModelBinder(BinderType = typeof(JsonModelBinder))][FromForm] UploadCommentDto comment, IFormFile picture, [FromRoute] int dogId) { comment.AuthorId = int.Parse(User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier)?.Value); comment.DogId = dogId; var serviceResponse = await lostDogService.AddLostDogComment(comment, picture); var controllerResponse = mapper.Map <ControllerResponse <GetCommentDto> >(serviceResponse); return(StatusCode(serviceResponse.StatusCode, controllerResponse)); }
public async Task <ServiceResponse <GetCommentDto> > AddLostDogComment(UploadCommentDto commentDto, IFormFile picture) { var comment = mapper.Map <LostDogComment>(commentDto); if (picture is not null) { var pictureValidationResult = securityService.IsPictureValid(picture); if (pictureValidationResult.Successful) { byte[] data; using (var ms = new MemoryStream()) { picture.CopyTo(ms); data = ms.ToArray(); } comment.Picture = new PictureComment() { FileName = picture.FileName, FileType = picture.ContentType, Data = data }; } else { return new ServiceResponse <GetCommentDto>() { Successful = false, StatusCode = StatusCodes.Status400BadRequest, Message = pictureValidationResult.Message, } }; } var repoResponse = await lostDogDataRepository.AddLostDogComment(comment); var serviceResponse = mapper.Map <ServiceResponse <GetCommentDto> >(repoResponse); if (!serviceResponse.Successful) { serviceResponse.StatusCode = StatusCodes.Status400BadRequest; } return(serviceResponse); }
public async void AddLostDogCommentSuccessfulForSuccessfulResponses() { var repo = new Mock <ILostDogRepository>(); var security = new Mock <ISecurityService>(); using var memoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4 }); var picture = new FormFile(memoryStream, 0, memoryStream.Length, "name", "filename") { Headers = new HeaderDictionary(), ContentType = "image/jpeg" }; var commentDto = new UploadCommentDto(); repo.Setup(o => o.AddLostDogComment(It.IsAny <LostDogComment>())).Returns(Task.FromResult(new RepositoryResponse <LostDogComment>())); security.Setup(s => s.IsPictureValid(It.IsAny <IFormFile>())).Returns(new ServiceResponse()); var service = new LostDogService(repo.Object, security.Object, mapper, logger); Assert.True((await service.AddLostDogComment(commentDto, picture)).Successful); }