public async Task <ProductModel> UploadPhotoAsync(Guid productId, ProductPostPhotoModel photo)
        {
            var entity = await repository.GetByIdAsync(productId);

            if (entity == null)
            {
                throw new NotFoundException("Product not found");
            }

            if (photo.Photo == null || photo.Photo.Length == 0)
            {
                throw new BadRequestException("Please select a file");
            }

            if (photo.Photo.Length > 1274000)
            {
                throw new BadRequestException("The file size is too much please upload an image under 1MB");
            }

            using (var ms = new MemoryStream())
            {
                photo.Photo.CopyTo(ms);
                var fileBytes = ms.ToArray();
                entity.Photo = Convert.ToBase64String(fileBytes);
                await repository.UpdateAsync(entity);

                var model = mapper.Map <ProductModel>(entity);
                return(model);
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UploadPhoto([FromForm][Bind("Photo")] ProductPostPhotoModel photo, Guid id)
        {
            var serviceRepsonse = await productService.UploadPhotoAsync(id, photo);

            return(Ok(serviceRepsonse));
        }