public async Task <IHttpActionResult> SetResult(IoTResultRequestModel result) { try { var model = new Result { UserId = result.UserId, CompetitionId = result.CompetitionId, Value = result.Result }; var newModel = await resultWriteService.AddAsync(model); var maxId = await chainReadService.GetMaxBlocksIdAsync(); var block = new HistoryBlock { Id = ++maxId, Changes = $"User result: {result.Result}", AuthorId = result.UserId, CreatedOn = DateTime.UtcNow.Date }; block.Hash = block.HashValues(); await chainWriteService.AddHistoryBlockToChainAsync(result.UserId, block); return(Ok()); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <bool> IsChainValidAsync(int chainId) { var chain = await chainReadRepository.GetByIdAsync(chainId); var blocks = await historyBlockReadRepository.GetBlocksByChainIdAsync(chainId); if (blocks?.Count > 0) { HistoryBlock previousBlock = null; HistoryBlock block = null; for (var i = 0; i < blocks.Count; i++) { block = blocks.ElementAt(i); if (block.Hash != block.HashValues()) { return(false); } if (previousBlock != null && previousBlock.Hash != block.PreviousHash) { return(false); } previousBlock = block; } } return(true); }
public async Task <IHttpActionResult> Registration(UserRegistrationRequestModel model) { if (!ModelState.IsValid) { return(BadRequest()); } try { var isUserExist = await userReadService.IsUserExistAsync(model.Email); if (isUserExist) { return(BadRequest("User is already registered")); } else { //registration var chain = new Chain { CreatedOn = DateTime.UtcNow }; var user = new User { Email = model.Email, Password = userWriteService.HashPassword(model.Password), Name = model.Name, Surname = model.Surname, Birthday = model.Birthday, LeagueId = model.LeagueId, RegistrationDate = DateTime.UtcNow, Chain = chain }; var newUser = await userWriteService.RegistrationAsync(user); //add history var maxId = await chainReadService.GetMaxBlocksIdAsync(); var block = new HistoryBlock { Id = ++maxId, Changes = "User registration", AuthorId = newUser.Id, CreatedOn = DateTime.UtcNow.Date }; block.Hash = block.HashValues(); var newBlock = await chainWriteService.AddHistoryBlockToChainAsync(newUser.Chain.Id, block); //sign in var identity = SignIn(newUser.Email, newUser.Role.Value, newUser.Id.ToString()); var token = GetUserToken(identity); return(Ok(new { token, newUser.Id, newUser.RoleId })); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <IHttpActionResult> Update(UserRequestModel model) { if (!ModelState.IsValid) { var errors = new StringBuilder(); foreach (var state in ModelState) { foreach (var error in state.Value.Errors) { errors.Append($"{error.ErrorMessage}\n"); } } return(BadRequest(errors.ToString())); } try { var userById = await userReadService.GetUserByIdAsync(model.Id); var userByEmail = await userReadService.GetUserByEmailAsync(model.Email); if (userById == null || (userByEmail != null && userById.Id != userByEmail.Id)) { return(BadRequest("User is not registered")); } else { var user = new User { Id = model.Id, Email = model.Email, Name = model.Name, Surname = model.Surname, Birthday = model.Birthday, Education = model.Education, Phone = model.Phone, City = model.City, Weight = model.Weight, Height = model.Height, LeagueId = model.LeagueId }; var updatedUser = await userWriteService.UpdateUserAsync(user); //add history var maxId = await chainReadService.GetMaxBlocksIdAsync(); var block = new HistoryBlock { Id = ++maxId, Changes = $"Update user. Height = {user.Height}, weight = {user.Weight}. Date: {DateTime.UtcNow.ToShortDateString()}", AuthorId = updatedUser.Id, CreatedOn = DateTime.UtcNow.Date }; block.Hash = block.HashValues(); var newBlock = await chainWriteService.AddHistoryBlockToChainAsync(updatedUser.Id, block); return(Ok()); } } catch (Exception ex) { return(Ok(ex.Message)); } }