public async Task <IActionResult> UpdateUser(string id, [FromBody] UserForUpdateDto UserForUpdateDto) { if (id != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } PhotoForCreationDto photo = UserForUpdateDto.Photo; if (photo != null && photo.File != null) { var file = photo.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file) }; uploadResult = _cloudinary.Upload(uploadParams); } photo.Url = uploadResult.Uri.ToString(); photo.PublicId = uploadResult.PublicId; } User user = _mapper.Map <User>(UserForUpdateDto); User updatedUser = await _usersService.Update(id, user); UserForReturnDto updatedUserToReturn = _mapper.Map <UserForReturnDto>(updatedUser); return(Ok(updatedUserToReturn)); }
public async Task <IActionResult> Login([FromBody] UserForLoginDto loginDto) { var user = await _userManager.Users .SingleOrDefaultAsync(usr => usr.UserName == loginDto.Username.ToLower()); if (user == null) { return(Unauthorized("Invalid username.")); } var result = await _signInManager .CheckPasswordSignInAsync(user, loginDto.Password, false); if (!result.Succeeded) { return(Unauthorized("Invalid password.")); } var userForReturn = new UserForReturnDto { Id = user.Id, Username = user.UserName, Token = await _tokenService.CreateToken(user) }; return(Ok(userForReturn)); }
public async Task <IActionResult> UpdateAccountUser(int userId, UserForUpdateDto userForUpdateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } User user = await _repo.GetUser(userId); _mapper.Map <UserForUpdateDto, User>(userForUpdateDto, user); var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { return(BadRequest(result.Errors)); } if (userForUpdateDto.CurrentPassword != null && userForUpdateDto.CurrentPassword.Trim().Length > 0 && userForUpdateDto.NewPassword != null && userForUpdateDto.NewPassword.Trim().Length > 0) { result = await _userManager.ChangePasswordAsync(user, userForUpdateDto.CurrentPassword, userForUpdateDto.NewPassword); if (!result.Succeeded) { return(BadRequest(result.Errors)); } } UserForReturnDto userForReturnDto = _mapper.Map <UserForReturnDto>(user); return(Ok(userForReturnDto)); }
private static void LogoutCommand() { _currentUser = null; Client.DefaultRequestHeaders.Authorization = null; Console.WriteLine(); Console.WriteLine("Successfully logged out!"); Console.WriteLine(); }
private dynamic ExpandSingleItem(UserForReturnDto userDto) { var links = GetLinks(userDto.Id); var resourceToReturn = userDto.ToDynamic() as IDictionary <string, object>; resourceToReturn.Add("links", links); return(resourceToReturn); }
public async Task <IActionResult> Get(string id) { User user = await _usersService.GetOne(id); if (user == null) { return(NotFound()); } UserForReturnDto userToReturn = _mapper.Map <UserForReturnDto>(user); return(Ok(userToReturn)); }
public async Task <UserForReturnDto> Register(AppUser _user, string _password) { var UserForReturn = new UserForReturnDto(); var res = await _userManager.CreateAsync(_user, _password); if (res.Succeeded) { UserForReturn.user = _Mapper.Map <UserLiteDto>(_user); return(UserForReturn); } UserForReturn.ErrorList = res.Errors; return(UserForReturn); }
public async Task <IActionResult> Register(UserForRegisterDto userForRegisterDto) { User userToCreate = _mapper.Map <User>(userForRegisterDto); var result = await _userManager.CreateAsync(userToCreate, userForRegisterDto.Password); if (result.Succeeded) { UserForReturnDto userToReturn = _mapper.Map <UserForReturnDto>(userToCreate); return(Ok(userToReturn)); } return(BadRequest(result.Errors)); }
public async Task <IActionResult> IsAuthenticated() { // if (id != User.FindFirst(ClaimTypes.NameIdentifier).Value) // return Unauthorized(); User user = await _usersService.GetOne(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (user == null) { return(NotFound()); } UserForReturnDto userToReturn = _mapper.Map <UserForReturnDto>(user); return(Ok(userToReturn)); }
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)); }
private Dictionary <string, int> GetInventory(UserForReturnDto userData) { var inventory = new Dictionary <string, int>(); foreach (var pokemon in userData.PokeCollection) { if (!inventory.ContainsKey(pokemon.Name)) { inventory.Add(pokemon.Name, 1); } else { inventory[pokemon.Name] += 1; } } return(inventory); }
public async Task <CodeLabForReturnDto> GetOne(string id) { CodeLab codeLab = await _codeLabs.Find(Builders <CodeLab> .Filter.Eq(x => x.Id, id)).FirstOrDefaultAsync(); CodeLabForReturnDto codeLabToReturnDto = _mapper.Map <CodeLabForReturnDto>(codeLab); var applications = new List <ApplicationForReturnDto>(); foreach (Application application in codeLab.Applications) { User user = await _users.Find(Builders <User> .Filter.Eq(x => x.Id, application.ApplicantId)).FirstOrDefaultAsync(); UserForReturnDto userForReturn = _mapper.Map <UserForReturnDto>(user); ApplicationForReturnDto applicationForReturnDto = _mapper.Map <ApplicationForReturnDto>(application); applicationForReturnDto.Applicant = userForReturn; applications.Add(applicationForReturnDto); } codeLabToReturnDto.Applications = applications; return(codeLabToReturnDto); }
private static void LoginCommand() { if (!CheckLogoutStatus()) { return; } Console.WriteLine(); Console.Write("Type login: "******"Type password: "******"HTTP/1.1", RequestMethod = "POST", RequestBody = JsonConvert.SerializeObject(new UserForLoginDto() { Login = login, Password = password }), RequestPath = _baseUrl + "auth/login" }; var loginTask = Client.PostAsync(_baseUrl + "auth/login", new JsonContent(new UserForLoginDto { Login = login, Password = password })); while (!loginTask.IsCompletedSuccessfully) { } requestDto.ResponseTime = DateTime.Now; requestDto.ResponseCode = (int)loginTask.Result.StatusCode; var resultTask = loginTask.Result.Content.ReadAsStringAsync(); while (!resultTask.IsCompletedSuccessfully) { } if (loginTask.Result.StatusCode == HttpStatusCode.OK) { var loginResult = JsonConvert.DeserializeObject <UserForReturnDto>(resultTask.Result); _currentUser = loginResult; Console.WriteLine("Logged in <" + login + "> succesfully!"); } else { Console.WriteLine("Unable to login!"); } requestDto.ResponseBody = resultTask.Result; Client.PostAsync(_loggerUrl + "requests", new JsonContent(requestDto)); Console.WriteLine(); }
private async Task SendUserInventoryList(List <List <PokemonForReturnDto> > listOfCollections, UserForReturnDto userData, string username) { var uniqueCount = userData.PokeCollection.Select(x => x.Name).Distinct().Count(); var message = $":globe_with_meridians:` HELLO, {username.ToUpper()}. WELCOME TO YOUR POKEMON STORAGE UNIT. \nBELOW YOU CAN FIND A LIST OF ALL THE POKEMON YOU'VE CAUGHT...`\n\n"; message += $":ballot_box:`POKE STORAGE | Unique Pokemon Found: {uniqueCount} / 151`\n"; foreach (var collection in listOfCollections) { message += PokeCollectionUtil.ChunkToString(collection); await _discord.GetUser(userData.DiscordId).SendMessageAsync(message); message = ""; } }