public async Task <IServiceResult> AddPizzaPhotoAsync(PhotoToCreateDto newPhoto) { try { if (newPhoto.PhotoInBytes.Length > 0) { PizzaModel photoRecipent = (await _unitOfWork.Pizzas.GetByExpressionAsync(x => x.Id == newPhoto.PizzaId, i => i.Include(p => p.Photo))).SingleOrDefault(); // replace old photo if exists if (photoRecipent.Photo != null) { bool deletionResult = await DeletePhotoAsync(photoRecipent.Photo); if (deletionResult == false) { return(new ServiceResult(ResultType.Error, new List <string> { "Error during deletion of photo" })); } } var uploadParams = new ImageUploadParams() { File = new FileDescription($"Photo of {photoRecipent.Name} pizza", new MemoryStream(newPhoto.PhotoInBytes)) }; ImageUploadResult response = await _cloudinary.UploadAsync(uploadParams); if (response.StatusCode == System.Net.HttpStatusCode.OK) { PhotoModel photo = new PhotoModel { PizzaId = newPhoto.PizzaId, PublicId = response.PublicId, Url = response.Url.ToString(), Pizza = photoRecipent }; await _unitOfWork.Photos.CreateAsync(photo); await _unitOfWork.SaveChangesAsync(); return(new ServiceResult(ResultType.Correct)); } } return(new ServiceResult(ResultType.Error, new List <string> { "Error during deletion of photo" })); } catch (Exception e) { return(new ServiceResult(ResultType.Error, new List <string> { e.Message })); } }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoToCreateDto photoFToCreateDto) { // اختبار هل هو نفس اليوزر ام لا if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // جلب اليوزر var userFromRepo = await _repo.GetUser(userId); // تجهيز الملف المطلوب رفعه var file = photoFToCreateDto.File; // متغير لرفع الصورة var uploadResult = new ImageUploadResult(); // لو الملف موجود if (file != null && 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") }; // النتيجة القادمة من ال Cloudinary uploadResult = _cloudinary.Upload(uploadParams); } } // ربط الصورة المرفوعة بال Api photoFToCreateDto.Url = uploadResult.Uri.ToString(); photoFToCreateDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoFToCreateDto); // لو الصورة غير موجوده يجعلها الرئيسية if (!userFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } // اضافة الصورة userFromRepo.Photos.Add(photo); // الحفظ if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoToReturnDto>(photo); // ربطها بالصور الخاصة بالعضو return(CreatedAtRoute("GetPhoto", new Photo { Id = photo.Id }, photoToReturn)); } // حال حدوث اي خطأ return(BadRequest("خطأ في اضافة الصورة")); }
public async Task <IActionResult> AddPizzaPhotoAsync(PhotoToCreateDto photoToCreate) { IServiceResult result = await _photoService.AddPizzaPhotoAsync(photoToCreate); if (result.Result == ResultType.Correct) { return(Ok()); } else { return(BadRequest(result.Errors)); } }
public async Task <IActionResult> AddPhoto(int userId, [FromForm] PhotoToCreateDto photoDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); 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), Transformation = new Transformation() .Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoDto); if (!user.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoToReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("There was a problem adding the photo.")); }
public async Task <IActionResult> CreateUserPhoto(int userId, [FromForm] PhotoToCreateDto photoToCreateDto) { var userFromRepo = await _repository.GetUser(userId); if (userFromRepo == null) { return(BadRequest("User does not exist")); } var file = photoToCreateDto.File; photoToCreateDto.UserId = userId; var photoUploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var imageUploadParams = new ImageUploadParams() { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; photoUploadResult = _cloudinary.Upload(imageUploadParams); } } photoToCreateDto.Url = photoUploadResult.Uri.ToString(); photoToCreateDto.PublicId = photoUploadResult.PublicId; var photoForUpload = _mapper.Map <Photo>(photoToCreateDto); _repository.Add(photoForUpload); if (await _repository.SaveAll()) { var photoToReturn = _mapper.Map <PhotoToReturnDto>(photoForUpload); return(CreatedAtRoute("GetPhotoForUser", new { id = photoToReturn.Id, userId = userFromRepo.Id }, photoToReturn)); } return(BadRequest("An Error occurred while uploading the photo")); }
public async Task <IActionResult> CreateGalleryPhoto(int bookingId, int userId, [FromForm] PhotoToCreateDto photoToCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repository.GetUser(userId); if (userFromRepo == null || userFromRepo.UserRole != UserRole.Admin) { return(Unauthorized()); } var bookingFromRepo = await _repository.GetBooking(bookingId); if (bookingFromRepo == null) { return(BadRequest("Booking does not exist")); } var file = photoToCreateDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var imageUploadParams = new ImageUploadParams() { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Width(600).Height(600).Crop("fill") }; uploadResult = _cloudinary.Upload(imageUploadParams); } } photoToCreateDto.Url = uploadResult.Uri.ToString(); photoToCreateDto.PublicId = uploadResult.PublicId; if (bookingFromRepo.GalleryPictures.Count == 0) { photoToCreateDto.IsMain = true; } var fileToUpload = _mapper.Map <GalleryPicture>(photoToCreateDto); bookingFromRepo.GalleryPictures.Add(fileToUpload); if (await _repository.SaveAll()) { var photoToReturn = _mapper.Map <PhotoToReturnDto>(fileToUpload); return(CreatedAtRoute("GetPhoto", new { id = photoToReturn.Id, bookingId = bookingId }, photoToReturn)); } return(BadRequest("An Error occurred while uploading the photo")); }