public async Task Update(int id, [FromBody] UserModel user)
        {
            var userDto = new UserDto
            {
                Id = id,
                FullName = user.FullName
            };

            await _usersService.UpdateUserAsync(userDto);
        }
 public async Task<HttpResponseMessage> Create([FromBody]UserModel user)
 {
     var userDto = new UserDto
     {
         Username = user.Username,
         FullName = user.FullName,
         Password = user.Password
     };
     var created = await _usersService.AddUserAsync(userDto);
     return Request.CreateResponse(HttpStatusCode.Created, new UserModel
     {
         UserId = created.Id,
         Rating = created.Rating
     });
 }
Exemple #3
0
        public async Task<UserDto> GetUserAsync(int userId)
        {
            User user = await _context.Users.Include(u => u.Predictions.Select(p => p.Match)).FirstOrDefaultAsync(u => u.Id == userId);
            if (user ==  null)
            {
                throw new NotFoundException();
            }

            UserDto userDto = new UserDto
            {
                Id = user.Id,
                FullName = user.FullName,
                Rating = user.Predictions.Count(p => p.Match.Winner == p.PredictedWinner && p.Match.StatusId == (int)MatchStatusesEnum.Finished) * 100D / user.Predictions.Count,
            };
            return userDto;
        }
Exemple #4
0
 public async Task<UserDto> GetUserAsync(string username, string password)
 {
     User user = await _context.Users.Include(u => u.Predictions.Select(p => p.Match)).FirstOrDefaultAsync(u => u.Username == username);
     if (user == null)
     {
         throw new NotFoundException();
     }
  
     byte[] submittedPasswordHash = PasswordHash.GenerateSaltedHash(password, user.PasswordSalt);
     var isValidPassword = user.PasswordHash.SequenceEqual(submittedPasswordHash);
     if (!isValidPassword)
     {
         throw new NotFoundException();
     }
     UserDto userDto = new UserDto
     {
         Id = user.Id,
         FullName = user.FullName,
         Rating = user.Predictions.Count(p => p.Match.Winner == p.PredictedWinner && p.Match.StatusId == (int)MatchStatusesEnum.Finished) * 100D / user.Predictions.Count,
     };
     return userDto;
 }
Exemple #5
0
        public async Task UpdateUserAsync(UserDto userDto)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userDto.Id);
            if (user == null)
            {
                throw new NotFoundException();
            }

            user.FullName = userDto.FullName;
            await _context.SaveChangesAsync();
        }
Exemple #6
0
        public async Task<UserDto> AddUserAsync(UserDto user)
        {
            if (_context.Users.Any(u => u.Username == user.Username))
            {
                throw new BadRequestException($"User with username {user.Username} already exists");
            }

            byte[] salt = PasswordHash.GenerateSalt();
            byte[] saltedPassword = PasswordHash.GenerateSaltedHash(user.Password, salt);

            var role = _context.Roles.FirstOrDefault(r => r.Id == (int)RolesEnum.User);

            var created = _context.Users.Add(new User
            {
                Username = user.Username,
                FullName = user.FullName,
                PasswordHash = saltedPassword,
                PasswordSalt = salt,
                AuthenticationToken = Guid.Empty,
                Role = role
            });
            await _context.SaveChangesAsync();
            return new UserDto
            {
                Id = created.Id,
                Rating = created.Rating
            };
        }