public ActionResult Get() { var result = _cityBusiness.GetAll(x => 1 == 1, x => x.Photos, x => x.User).ToList(); var mappedResult = _mapper.Map <List <CityListDto> >(result); return(Ok(mappedResult)); }
public IActionResult AddCityPhoto(int cityid, [FromForm] PhotoSaveDto model) { var city = _cityBusiness.GetAll(x => x.Id == cityid, x => x.Photos).FirstOrDefault(); if (city == null) { return(BadRequest("City not found")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != city.UserId) { return(Unauthorized()); } var file = model.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); } } model.Url = uploadResult.Uri.ToString(); model.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(model); photo.City = city; if (!city.Photos.Any(x => x.IsMain)) { photo.IsMain = true; } city.Photos.Add(photo); if (_photoBusiness.SaveAll()) { var photoDto = _mapper.Map <PhotoDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoDto)); } return(BadRequest("Not added photo")); }