public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto) { 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("Could not find 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("Message save failed."); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { // Check token ID if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // Get the user from the Repo var userFromRepo = await _repo.GetUser(userId); // Need a var for the File var file = photoForCreationDto.File; // Need to store var uploadResult = new ImageUploadResult(); if (file.Length > 0) { // Filestream so we must dispose file in memory once we finish calling this method. 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.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; // Mapping from the Dto var photo = _mapper.Map <Photo>(photoForCreationDto); // For the Main photo if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { // Return from HTTP Post // Created a get request and modify the IRinkRepository var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("We could not add the photo.")); //next create the component for SPA }
public async Task <IActionResult> getUsers([FromQuery] UserParams userParams) { // For Filtering var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var userFromRepo = await _repo.GetUser(currentUserId); userParams.UserId = currentUserId; if (string.IsNullOrEmpty(userParams.PlayerPosition)) { userParams.PlayerPosition = userFromRepo.PlayerPosition == "Forward" ? "Defence" : "Forward"; } var users = await _repo.GetUsers(userParams); var usersToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users); // Passing this information in the response header Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages); return(Ok(usersToReturn)); }