public async Task <IActionResult> AddPhoto([FromRoute] int id, [FromRoute] int userid, PhotoForCreationDto photoDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userid); // var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var resto = await _context.Restaurants.FirstOrDefaultAsync(r => r.Id == id); var currentUserId = resto.UserId; if (currentUserId != userid) { return(Unauthorized()); } var file = photoDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; //var photo = _mapper.Map<ResPhoto>(photoDto); var photo = new ResPhoto(); photo.Url = uploadResult.Uri.ToString(); photo.PublicId = uploadResult.PublicId; photo.RestaurantId = id; photo.UserId = userid; _context.ResPhotos.Add(photo); resto.UrlPhoto = photo.Url; await _context.SaveChangesAsync(); return(Ok(photo)); }
public async Task <IActionResult> UpdatePhoto([FromRoute] int id, [FromBody] ResPhoto photo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != photo.Id) { return(BadRequest()); } _context.Entry(photo).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> DeletePhoto([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ResPhoto photo = await _context.ResPhotos.SingleOrDefaultAsync(r => r.Id == id); if (photo == null) { return(NotFound()); } _context.ResPhotos.Remove(photo); await _context.SaveChangesAsync(); return(Ok(photo)); }