public async Task <IActionResult> AddPhotosForUser(int userID, [FromForm] PhotosDTO photosDTO) { if (userID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await userRepo.GetUser(userID); var file = photosDTO.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photosDTO.Url = uploadResult.Uri.ToString(); photosDTO.PublicId = uploadResult.PublicId; var photo = mapper.Map <Photo>(photosDTO); if (!user.Photos.Any(u => u.isMain)) { photo.isMain = true; } user.Photos.Add(photo); if (await userRepo.SaveAll()) { var photoToReturn = mapper.Map <PhotoForReturnDTO>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id, userId = userID }, photoToReturn)); } return(BadRequest("Could not add photo :(")); }
public async Task OnGetAsync() { var posts = await pcc.GetAllPhotosWCFAsync(); foreach (var item in posts) { bool containsProp = false; bool containsPropValue = false; // Trebuia folosit AutoMapper. Transform Post in PostDTO PhotosDTO pd = new PhotosDTO(); pd.PhotoId = item.PhotoId; pd.Path = item.Path; pd.Data_creare = item.Data_creare; pd.Descriere = item.Descriere; pd.Deleted = item.Deleted; foreach (var cc in item.Proprietates) { ProprietatiDTO cdto = new ProprietatiDTO(); cdto.ProprietateId = cc.ProprietateId; cdto.NumeProprietate = cc.NumeProprietate; cdto.ValoareProprietate = cc.ValoareProprietate; cdto.PhotoPhotoId = cc.PhotoPhotoId; pd.Proprietates.Add(cdto); if (!string.IsNullOrEmpty(PhotoProp) && cdto.NumeProprietate.Equals(PhotoProp)) { containsProp = true; } if (!string.IsNullOrEmpty(SearchString) && cdto.ValoareProprietate.Equals(SearchString)) { containsPropValue = true; } } //search by prop and value if (!string.IsNullOrEmpty(SearchString) && !string.IsNullOrEmpty(PhotoProp)) { if (containsProp && containsPropValue) { Photos.Add(pd); } } else { // search by nothing if (string.IsNullOrEmpty(SearchString) && string.IsNullOrEmpty(PhotoProp)) { Photos.Add(pd); } else { if (!string.IsNullOrEmpty(PhotoProp)) { if (containsProp) { Photos.Add(pd); } } else if (containsPropValue) { Photos.Add(pd); } } } ViewData["PhotoFound"] = Photos.Count().ToString(); ///search by prop list var Props = new List <string>(); var proplst = await pcc.GetAllProprietatiWCFAsync(); foreach (var pr in proplst) { Props.Add(pr.NumeProprietate); } Props.Sort(); Props = Props.Distinct().ToList(); ProprietatiList = new SelectList(Props); } }