Exemplo n.º 1
0
        public async Task <ActionResult <User> > Signup([FromBody] User user)
        {
            if (user == null)
            {
                return(BadRequest());
            }

            string error = CommitValidator.Validate(user);

            if (!string.IsNullOrEmpty(error))
            {
                throw new NotifyException(error);
            }

            Context.Add(user);
            user.Password = user.Password.Encrypt();
            RefreshToken refreshToken = GenerateRefreshToken();

            user.RefreshTokens.Add(refreshToken);
            user.RefreshToken = refreshToken.Token;
            CreateInitialDataForNewUser(user);
            user.UserId      = Guid.NewGuid();
            user.AccessToken = GenerateAccessToken(user.UserId);
            await Context.SaveChangesAsync();

            return(user);
        }
        public async Task <ActionResult <TEntity> > PostAsync([FromBody] TEntity entity)
        {
            if (entity == null)
            {
                return(await Task.FromResult(BadRequest()));
            }
            if (entity is IUserSpecificEntity userSpecificEntity)
            {
                userSpecificEntity.UserId = GetUserId();
            }
            string error = CommitValidator.Validate(entity);

            if (string.IsNullOrEmpty(error))
            {
                ActionResult <TEntity> result = await PostCoreAsync(entity);

                await Context.SaveChangesAsync();

                return(result);
            }
            else
            {
                throw new NotifyException(error);
            }
        }