public async Task <IActionResult> SetMainPhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("This is already the main photo")); } var currentMainPhoto = await _repo.GetMainPhotoForUser(userId); currentMainPhoto.IsMain = false; photoFromRepo.IsMain = true; if (await _repo.SaveAll()) { return(NoContent()); } return(BadRequest("Could not set photo to main")); }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { var userFull = await _userManager.FindByNameAsync(userForLoginDto.Username); var result = await _signInManager.CheckPasswordSignInAsync(userFull, userForLoginDto.Password, false); if (result.Succeeded) { var user = _mapper.Map <UserForPlanerDto>(userFull); if (string.IsNullOrEmpty(user.PhotoUrl)) { var userMainPhoto = await _repo.GetMainPhotoForUser(userFull.Id); if (userMainPhoto != null) { user.PhotoUrl = userMainPhoto.Url; } } return(Ok(new { token = GenerateJwtToken(userFull).Result, user })); } return(Unauthorized()); }