public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] photoForCreateDto photoforrcreate) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoforrcreate.File; if (file != null && file.Length > 0) { photoforrcreate.Url = AddFolderAndImage(file); var photo = _mapper.Map <Photo>(photoforrcreate); if (!userFromRepo.Photos.Any(x => x.IsMain == true)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var phototoreturn = _mapper.Map <photoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, phototoreturn.Id)); } else { return(BadRequest("خطا اثناء تحميل الصورة")); } } else { return(BadRequest("خطا اثناء تحميل الصورة")); } }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId, true); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId, false); if (recipient == null) { return(BadRequest("لم يتم الوصول للمرسل اليه")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(Ok(messageToReturn)); //return CreatedAtRoute("GetMessage",new {controller = "Messages",id=message.Id},message); } return(BadRequest()); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreateDto photoForCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var UserFromRepo = await _repo.GetUser(userId, true); var file = photoForCreateDto.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") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreateDto.Url = uploadResult.Uri.ToString(); photoForCreateDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreateDto); if (!UserFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } UserFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { try { var PhotoToReturn = _mapper.Map <PhotoForReturnDto>(photo); //var x = CreatedAtRoute(routeName: nameof(GetPhoto), routeValues: new { id = photo.Id }, value: PhotoToReturn); //var x = CreatedAtAction(actionName: "GetPhoto", controllerName: "Photos", routeValues: new {id = photo.Id }, value: PhotoToReturn); var x = Created("http://localhost:5000/api/Users/" + userId + "/photos/" + photo.Id, value: PhotoToReturn); return(x); } catch (Exception e) { throw e; } } return(BadRequest("خطأ في رفع الصورة")); }
public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var UserFromRepo = await _repo.GetUser(id, true); _mapper.Map(userForUpdateDto, UserFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new System.Exception($"حدثت مشكلة في التعديل"); }