Пример #1
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));
            }
        }
        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));
            }
        }