public async Task <IActionResult> GetAll([FromQuery] VisitorParams visitorParams) { //Get loggedin Id var currentVisitorId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var currentVisitor = await _repo.GetVisitor(currentVisitorId); visitorParams.UserId = currentVisitorId; var visitors = await _repo.GetAll(visitorParams); var listToReturn = _mapper.Map <IEnumerable <VisitorForListDto> >(visitors); Response.AddPagination(visitors.CurrentPage, visitors.PageSize, visitors.TotalCount, visitors.TotalPages); return(Ok(listToReturn)); }
public async Task <IActionResult> AddPhoto(int visitorId, [FromForm] PhotoForCreationDto photoForCreationDto) { //Check if this userId in URL is that of logged in user in token if (visitorId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } //Get the visitor from DB var visitorFromRepo = await _repo.GetVisitor(visitorId); //Get file name coming from photoForCreationDto var file = photoForCreationDto.File; //Create the upload result that will hold the result coming from coudinary after upload in complete var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParam = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(250).Height(250).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParam); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!visitorFromRepo.Photos.Any(x => x.IsMain))//Getting error here { photo.IsMain = true; } visitorFromRepo.Photos.Add(photo);//Getting error here visitorFromRepo.ImageUrl = photo.Url; if (await _repo.SaveAll()) { var returnPhoto = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { visitorId = visitorId, id = photo.Id }, returnPhoto)); } return(BadRequest("Could not upload the photo!")); }