public async Task <IActionResult> CreateAlbum(Album album) { CloudinaryManager cm = new CloudinaryManager(); var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); album.UserId = userId; if (album.Image != null) { string urlImage = cm.UploadImage(album.Image); if (urlImage != string.Empty) { album.Image = urlImage; } else { return(BadRequest(new { message = "An error has ocur when uploading the image!" })); } } _playAppRepository.Add(album); if (await _playAppRepository.SaveAll()) { return(Ok(new { message = "Album Created!", data = album })); } return(BadRequest(new { message = "Error when creating Album" })); }
public async Task <IActionResult> CreateSong(Song song) { _playAppRepository.Add(song); if (await _playAppRepository.SaveAll()) { return(Ok(new { data = song })); } return(BadRequest(new { message = "Error when creating the song" })); }
public async Task <IActionResult> Register(UserForRegister userForRegister) { byte[] passwordHash; byte[] passwordSalt; var userFound = await _authRepository.GetUserByUserName(userForRegister.Username); if (userFound == null) { userFound = await _authRepository.GetUserByEmail(userForRegister.Email); if (userFound != null) { return(BadRequest(new { message = "Email already exists!" })); } } else { return(BadRequest(new { message = "Username Already exists!" })); } Utilities.CreatePasswordHash(userForRegister.Password, out passwordHash, out passwordSalt); User user = _mapper.Map <User>(userForRegister); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _playAppRepository.Add(user); await _playAppRepository.SaveAll(); List <Claim> claims = new List <Claim>(); claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); claims.Add(new Claim(ClaimTypes.Name, user.Username)); var accessToken = GenerateToken(claims); var refreshToken = GenerateRefreshToken(); user.RefreshToken = refreshToken; await _authRepository.SaveRefreshToken(user); return(Ok(new { accessToken = accessToken.Token, expirationDate = accessToken.ExpirationDate.ToString("s"), refreshToken = refreshToken.Replace("+", "%2B") })); }