protected override async Task HandleCore(CreateMatchRequest command) { var entity = Mapper.Map <MatchEntity>(command.Match); _db.Matches.Add(entity); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(CreateBoxerRequest command) { var entity = Mapper.Map <BoxerEntity>(command.Boxer); _db.Boxers.Add(entity); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(CreatePredictionRequest command) { var entity = Mapper.Map <PredictionEntity>(command.Prediction); _db.Predictions.Add(entity); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(FinishMatchRequest command) { var affectedPredictions = _db.Predictions.Where(p => p.MatchId == command.Id).Select(p => p.UserId).ToList(); var affectedUsers = _db.Users.ToList().Where(u => affectedPredictions.Contains(u.Id)).ToList(); var userIds = affectedUsers.Select(u => u.Id).ToList(); var userPredictions = _db.Predictions.ToList().Where(p => userIds.Contains(p.UserId)).ToList(); var userPredictionMatchIds = userPredictions.Select(p => p.MatchId).ToList(); var predictedMatches = _db.Matches .Where(m => userPredictionMatchIds.Contains(m.Id) && m.WinnerId != null) .ToList(); var matchEntity = await _db.Matches.FindAsync(command.Id).ConfigureAwait(false); if (matchEntity == null) { throw new NotFoundException(); } matchEntity.WinnerId = command.WinnerId; foreach (var user in affectedUsers) { var successfull = 0.0; var currentUserPredictions = userPredictions.Where(p => p.UserId == user.Id).ToList(); foreach (var prediction in currentUserPredictions) { var match = predictedMatches.FirstOrDefault(m => m.Id == prediction.MatchId); if (match != null) { // Ако е новия завършил мач не трябва да гледаме записа в базата if ((match.Id == matchEntity.Id && matchEntity.WinnerId == prediction.PredictedBoxerId) || (match.Id != matchEntity.Id && match.WinnerId == prediction.PredictedBoxerId)) { successfull++; } } } if (matchEntity.WinnerId == userPredictions.FirstOrDefault(p => p.MatchId == matchEntity.Id)?.PredictedBoxerId) { successfull++; } user.Rating = successfull / (double)currentUserPredictions.Count; } await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(DeleteUserRequest command) { var boxer = await _db.Users.FindAsync(command.Id).ConfigureAwait(false); if (boxer == null) { throw new NotFoundException(); } _db.Users.Remove(boxer); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(UpdateLoginRequest command) { var login = _db.Logins.Where(l => l.Id == command.Login.Id).FirstOrDefault(); if (login == null) { throw new NotFoundException(); } Mapper.Map(command.Login, login); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(UpdateMatchRequest command) { var match = await _db.Matches.FindAsync(command.Match.Id).ConfigureAwait(false); if (match == null) { throw new NotFoundException(); } Mapper.Map(command.Match, match); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(DeletePredictionRequest command) { var prediction = await _db.Predictions.FindAsync(command.Id).ConfigureAwait(false); if (prediction == null) { throw new NotFoundException(); } _db.Predictions.Remove(prediction); await _db.SaveChangesAsync().ConfigureAwait(false); }
protected override async Task HandleCore(UpdateBoxerRequest command) { var boxer = await _db.Boxers.FindAsync(command.Boxer.Id).ConfigureAwait(false); if (boxer == null) { throw new NotFoundException(); } Mapper.Map(command.Boxer, boxer); await _db.SaveChangesAsync().ConfigureAwait(false); }
public async Task <LoginDto> HandleAsync(LogUserLoginRequest command) { var salt = _db.Users.FirstOrDefault(u => u.Username == command.User.Username)?.PasswordSalt; if (salt == null) { throw new BadRequestException(); } var passwordHash = AuthorizationExtensions.CreatePasswordHash(command.User.Password, salt); var registeredUser = _db.Users.Where(u => u.Username == command.User.Username && u.Password == passwordHash).FirstOrDefault(); if (registeredUser == null) { throw new UnauthorizedAccessException(); } var currentLogin = _db.Logins.Where(l => l.UserId == registeredUser.Id).OrderByDescending(l => l.ExpirationDate).FirstOrDefault(); if (currentLogin != null && currentLogin.ExpirationDate.IsValidExpirationDate()) { currentLogin.ExpirationDate = DateTimeExtensions.CreateExpirationDate(); } else { var entity = new LoginEntity(); entity.UserId = registeredUser.Id; entity.AuthToken = Guid.NewGuid().ToString(); entity.ExpirationDate = DateTimeExtensions.CreateExpirationDate(); _db.Logins.Add(entity); } await _db.SaveChangesAsync().ConfigureAwait(false); var savedLogin = _db.Logins.Where(l => l.UserId == registeredUser.Id) .OrderByDescending(l => l.ExpirationDate) .Select(u => new { u.Id, u.AuthToken, u.UserId }).FirstOrDefault(); var loginDto = new LoginDto(); loginDto.Id = savedLogin.Id; loginDto.AuthToken = savedLogin.AuthToken; loginDto.UserId = savedLogin.UserId; return(loginDto); }
protected override async Task HandleCore(CancelMatchRequest command) { var predictions = _db.Predictions.Where(p => p.MatchId == command.Id).ToList(); var match = _db.Matches.Find(command.Id); if (match == null) { throw new NotFoundException(); } match.Time = DateTime.MaxValue; _db.Predictions.RemoveRange(predictions); await _db.SaveChangesAsync().ConfigureAwait(false); }
public static bool IsValidAdminToken(string token) { using (var context = new BoxingContext()) { var login = context.Logins.Where(l => l.AuthToken == token).FirstOrDefault(); if (login != null && login.ExpirationDate.IsValidExpirationDate()) { var user = context.Users.FirstOrDefault(u => u.Id == login.UserId); if (user != null && user.IsAdmin) { login.ExpirationDate = DateTimeExtensions.CreateExpirationDate(); context.SaveChangesAsync(); return(true); } } } return(false); }
public static bool IsValidToken(string token) { if (token == "logintoken" || token == "registertoken") { return(true); } using (var context = new BoxingContext()) { var login = context.Logins.Where(l => l.AuthToken == token).FirstOrDefault(); if (login != null && login.ExpirationDate.IsValidExpirationDate()) { login.ExpirationDate = DateTimeExtensions.CreateExpirationDate(); context.SaveChangesAsync(); return(true); } } return(false); }
public async Task <LoginDto> HandleAsync(RegisterUserRequest request) { var matchingUsers = _db.Users.Any(u => u.Username == request.User.Username); if (matchingUsers) { throw new BadRequestException(); } var userEntity = Mapper.Map <UserEntity>(request.User); var salt = AuthorizationExtensions.CreateSalt(); var passwordHash = AuthorizationExtensions.CreatePasswordHash(request.User.Password, salt); userEntity.Password = passwordHash; userEntity.PasswordSalt = salt; _db.Users.Add(userEntity); var loginEntity = new LoginEntity(); loginEntity.UserId = userEntity.Id; loginEntity.AuthToken = Guid.NewGuid().ToString(); loginEntity.ExpirationDate = DateTimeExtensions.CreateExpirationDate(); _db.Logins.Add(loginEntity); await _db.SaveChangesAsync().ConfigureAwait(false); var savedLogin = _db.Logins.Where(l => l.UserId == userEntity.Id).OrderByDescending(l => l.ExpirationDate).Select(u => new { u.Id, u.AuthToken }).FirstOrDefault(); var loginDto = new LoginDto(); loginDto.Id = savedLogin.Id; loginDto.AuthToken = savedLogin.AuthToken; return(loginDto); }