Ejemplo n.º 1
0
        public async Task <HistoryBlock> AddHistoryBlockToChainAsync(int chainId, HistoryBlock block)
        {
            if (await chainReadService.IsChainValidAsync(chainId))
            {
                var chain = await chainReadRepository.GetByIdAsync(chainId);

                var blocks = await historyBlockReadRepository.GetBlocksByChainIdAsync(chainId);

                block.ChainId = chainId;

                if (blocks?.Count > 0)
                {
                    var lastBlock = await historyBlockReadRepository.GetByIdAsync(chain.LastBlockId.Value);

                    block.PreviousHash = lastBlock.Hash;
                }

                var newBlock = await historyBlockWriteRepository.AddAsync(block);

                //reload chain
                chain = await chainReadRepository.GetByIdAsync(chainId);

                chain.LastBlockId = newBlock.Id;

                await chainWriteRepository.UpdateAsync(chain);

                return(newBlock);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static void HistoryAdd(int Id, string S)
        {
            HistoryBlock HP = HistMemory;

            if (HistMemory == null)
            {
                HistMemory        = new HistoryBlock();
                HistMemory.Id     = Id;
                HistMemory.String = S;
            }
            else
            {
                while (HP.Next != null)
                {
                    if ((HP.Id == Id) && (HP.String == S))
                    {
                        if (HP.Next != null)
                        {
                            HP.Next = HP.Next.Next;
                        }
                    }
                    if (HP.Next != null)
                    {
                        HP = HP.Next;
                    }
                }
                if ((HP.Id != Id) || (HP.String != S))
                {
                    HP.Next        = new HistoryBlock();
                    HP.Next.Id     = Id;
                    HP.Next.String = S;
                }
            }
        }
Ejemplo n.º 3
0
        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));
            }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public async Task <HistoryBlock> AddAsync(HistoryBlock model)
        {
            var result = dbContext.HistoryBlocks.Add(model);

            await dbContext.SaveChangesAsync();

            return(result);
        }
Ejemplo n.º 6
0
        public static int HistoryCount(int Id)
        {
            int          C  = 0;
            HistoryBlock HP = HistMemory;

            while (HP != null)
            {
                if (HP.Id == Id)
                {
                    C++;
                }
                HP = HP.Next;
            }
            return(C);
        }
Ejemplo n.º 7
0
        public static string HistoryStr(int Id, int Count)
        {
            HistoryBlock HP = HistMemory;

            while (Count > 0)
            {
                if (HP.Id == Id)
                {
                    Count--;
                }
                HP = HP.Next;
            }
            if (HP.Id == Id)
            {
                return(HP.String);
            }
            else
            {
                return("");
            }
        }
Ejemplo n.º 8
0
 public Task RemoveAsync(HistoryBlock model)
 {
     dbContext.HistoryBlocks.Remove(model);
     return(dbContext.SaveChangesAsync());
 }
        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));
            }
        }