public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { //this line of code is added for the message photo display var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDTO.RecipientId); if (recipient == null) { return(BadRequest("Colud not Found the user.")); } var message = _mapper.Map <Message>(messageForCreationDTO); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed. "); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _repo.GetUser(userId, false); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreationDto.RecipientId, true); if (recipient == null) { return(BadRequest("Could not found user...")); } var message = _mapper.Map <Message>(messageForCreationDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); // TODO: CreatedAtRoute() return(CreatedAtRoute("GetMessage", new { controller = "Messages", userId, id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on saved"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { var sender = await _datingRepo.GetUser(userId); // some random shit ? video 172 from course for blocking some automapper shit if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _datingRepo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { BadRequest("Could not find user"); } var message = _mapper.Map <Message>(messageForCreationDto); _datingRepo.Add(message); if (await _datingRepo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } return(BadRequest("Couldnt create message")); }
public async Task <IActionResult> CreateMessage(int userId, MessageCreateDto messageCreateDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageCreateDto.SenderId = userId; var receipient = await _repo.GetUser(messageCreateDto.RecipientId); if (receipient == null) { return(BadRequest("receipient not found")); } var message = _mapper.Map <Message>(messageCreateDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageReturnDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("fail to creat message"); }
public async Task <IActionResult> GetUser(int id) { var user = await _datingrepo.GetUser(id); var result = _mapper.Map <UserListDto>(user); return(Ok(result)); }
public async Task <IActionResult> GetUser(int id) { var user = await repo.GetUser(id); var userToReturn = mapper.Map <UserForDetailsDto>(user); return(Ok(userToReturn)); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreation) { // Checking the user id with params id if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId, true); var file = photoForCreation.File; // The result get from API ImageUploadResult uploadResult = new ImageUploadResult(); // Trying to upload file into Cloudinary if (file.Length > 0) { using (var stream = file.OpenReadStream()) { ImageUploadParams uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } // Saving info getting from cloud photoForCreation.Url = uploadResult.Uri.ToString(); photoForCreation.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreation); if (!userFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoFromRepo = await _repo.GetPhoto(photo.Id); var photos = _mapper.Map <PhotoForReturnDto>(photoFromRepo); // TODO: CreatedAtRoute(); return(Ok(photos)); } return(BadRequest("Could not add the photo")); }
public async Task<IActionResult> AddPhotoForUser(int userId, [FromForm]PhotoForCreationDto photoForCreationDto) { // authorization if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)){ return Unauthorized(); } // getting the user var userFromRepo = await _repo.GetUser(userId); // whatever the file we are trying to upload var file = photoForCreationDto.File; // clouindary methods var uploadResult = new ImageUploadResult(); if (file.Length > 0) { // file is not empty using (var stream = file.OpenReadStream()) { // using since we are using memory and can be disposed that is the file that is in memry // we are also transforming the photo for example if we send big photo // we are transforming the photo to crop to small one and then cropping around the face var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name , stream), // combing the filename and file stream for upload params Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; // we are uploading here and saving the result uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; // IMapper PhotosController.PublicId; var photo = _mapper.Map<Photo>(photoForCreationDto); // if user doesnt have photos we can set it as main photo if (!userFromRepo.Photos.Any(userFromRepo => userFromRepo.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { // we need to return that particular photo id so that only happens after save var photoToReturn = _mapper.Map<PhotoForReturnDto>(photo); // we are wrapping request qwith 201 and some other stuff as this reponse , GetPhoto in this case // the parameters for caretaedatroute are name of route , parameters dont know why wee are adding userid and object return CreatedAtRoute(nameof(GetPhoto), new { userId = userId, id = photo.Id }, photoToReturn); } return BadRequest("couldnt add the photo"); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var file = photoForCreationDto.File; var userFormrepo = await _repo.GetUser(userId); 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); } } photoForCreationDto.PublicId = uploadResult.PublicId; photoForCreationDto.Url = uploadResult.Uri.ToString(); //photoForCreationDto.Url = uploadResult.Uri.ToString(); // photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFormrepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFormrepo.Photos.Add(photo); if (await _repo.SaveAll()) { //need to return the header. //return Ok(); var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhoto(int userid, [FromForm] PhotoDto photoReceived) { if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var dbUser = await _datingrepo.GetUser(userid); var file = photoReceived.file; var objUpload = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.FileName, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; objUpload = _cloudinary.Upload(uploadParams); } } photoReceived.CreatedDate = DateTime.Now; photoReceived.Url = objUpload.Uri.ToString(); photoReceived.PublicID = objUpload.PublicId; var photo = _mapper.Map <Photos>(photoReceived); if (dbUser.Photos.Count <= 0) { photo.IsMain = true; } dbUser.Photos.Add(photo); if (await _datingrepo.SaveAll()) { var result = _mapper.Map <PhotoDto>(photo); return(CreatedAtRoute("getphoto", new { photoid = photo.Id }, result)); } return(BadRequest("Error uploading photo")); }
public async Task <IActionResult> getUser(double code) { var user = await _repo.GetUser(code); var UserToReturn = _mapper.Map <UserForDetailedDto>(user); return(Ok(UserToReturn)); }
public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var currentUser = await _repo.GetUser(currentUserId); userParams.UserId = currentUserId; if (string.IsNullOrEmpty(userParams.Gender)) { userParams.Gender = userParams.Gender == "male" ? "female" : "male"; } var users = await _repo.GetUsers(userParams); var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users); Response.AddPagation(users.CurrPage, users.PageSizes, users.TotalCount, users.totalPage); return(Ok(usersToReturn)); }
[HttpGet] // we are telling the controller we will get the parameters from query since its a get call no body in the request // paramerts can come like this api/users?pageNumber=2&pageSize=3 to the api like this public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromRepo = await _datingRepo.GetUser(currentUserId); userParams.UserId = currentUserId; if (string.IsNullOrEmpty(userParams.Gender)) { userParams.Gender = userFromRepo.Gender == "male" ? "female" : "male"; } var users = await _datingRepo.GetUsers(userParams); var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users); // mapper // static method we created earlier , we are sending the pagination informtaion in ther headers of the response. check postman Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(usersToReturn)); }
public async Task <IActionResult> CreateMessage(int userid, MessageCreateDto messageParams) { try { var sender = await _datingrepo.GetUser(userid); if (sender.UserID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageParams.SenderID = userid; var receiver = await _datingrepo.GetUser(messageParams.ReceiverID); if (receiver == null) { return(BadRequest("Receiving user does not exists")); } var message = _mapper.Map <Messages>(messageParams); _datingrepo.Add(message); if (await _datingrepo.SaveAll()) { var returnMessage = _mapper.Map <MessageReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userid = userid, messageid = message.MessageID }, returnMessage)); } throw new Exception("Error while creating message"); } catch (Exception ex) { throw new Exception("Error while creating message"); } }