public async Task <IActionResult> AddPhotoToProfile(int userId, [FromForm] PhotoForCreationDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repo.GetUser(userId); if (user.ProfilePhoto != null) { Photo trashPhoto = await _repo.GetPhotoById(user.ProfilePhoto.Id); _repo.Delete(trashPhoto); } photoForCreationDto.Width = 500; photoForCreationDto.Height = 500; photoForCreationDto.UserId = userId; photoForCreationDto.IsMain = true; Photo photo = UploadPhotoToCloudinary(photoForCreationDto); _repo.Add(photo); if (await _repo.SaveAllAsync()) { PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo); return(Ok(photoForReturnDto)); } return(BadRequest("Adding photo to user's profile could not be possible")); }
public async Task <IActionResult> GetUser(int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repo.GetUser(userId); UserForReturnDto userForReturnDto = _mapper.Map <UserForReturnDto>(user); userForReturnDto.Purchase = _mapper.Map <PurchaseOrderDto>(user.CartOffers); // userForReturnDto.Purchase = _mapper.Map<PurchaseOrderDto>(user); userForReturnDto.Purchase.ClientId = user.Id; return(Ok(userForReturnDto)); }
public async Task GetUser_Success() { var user = await _dataContext.Users.FirstAsync(); var result = await _datingRepository.GetUser(user.Id, false); Assert.IsNotNull(result); Assert.AreEqual(user.Id, result.Id); }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { User userFromStore = await _userManager.FindByNameAsync(userForLoginDto.UserName); if (userFromStore == null) { return(Unauthorized()); } var result = await _signInManager.CheckPasswordSignInAsync(userFromStore, userForLoginDto.Password, false); if (result.Succeeded) { var userToReturn = _mapper.Map <UserForReturnDto>(await _repo.GetUser(userFromStore.Id)); return(Ok(new { token = GenerateJwtToken(userFromStore), user = userToReturn })); } return(Unauthorized()); }