Exemple #1
0
        public async Task <IActionResult> AddPicture([FromForm] Dto.NewPortfolioPicture picture)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            BlobClient blobClient = _blobContainerClient.GetBlobClient(Guid.NewGuid() + Path.GetExtension(picture.File.FileName));

            PortfolioPicture portfolioPicture = new PortfolioPicture();

            portfolioPicture.Picture      = blobClient.Uri.ToString();
            portfolioPicture.RelookeuseId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var result = await _context.AddAsync(portfolioPicture);

            await using var stream = picture.File.OpenReadStream();


            Task uploadImageTask = blobClient.UploadAsync(stream);
            Task saveDatabase    = _context.SaveChangesAsync();

            await Task.WhenAll(uploadImageTask, saveDatabase);

            return(Created("api/portfolio/", _mapper.Map <Dto.PortfolioPicture>(result.Entity)));
        }
Exemple #2
0
        public async Task <IActionResult> DeletePicture(int id)
        {
            PortfolioPicture portfolioPicture = await _context.PortfolioPicture.FirstOrDefaultAsync(pp => pp.Id == id && pp.RelookeuseId == int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            if (portfolioPicture == null)
            {
                return(NotFound(id));
            }

            Task removeImageTask = _blobContainerClient.GetBlobClient(portfolioPicture.Picture.Split("/").Last()).DeleteIfExistsAsync();

            _context.Remove(portfolioPicture);
            Task removeInDbTasj = _context.SaveChangesAsync();
            await Task.WhenAll(removeImageTask, removeInDbTasj);

            return(NoContent());
        }